[CLOSED] Is there a way to load labels in a form panel using data from a store

  1. #1

    [CLOSED] Is there a way to load labels in a form panel using data from a store

    Hi,

    I've searched around a bit and must be missing it (also hard to search as "label" brings up lots of noise!) but I am wondering if there is a way to have readonly form data that is not a disabled text field but just regular html, but works with form items much like text fields etc (i.e. has a DataIndex property and can be updated via a Store, or a form.LoadRecord() or .setValues() etc)?

    Here's a very contrived example, as I don't know how to explain better :)

    In this example, I have a bunch of text fields. One of them (e.g. the record id field), instead of being a textfield I want as just a label with a non text field value but keeping it aligned with the other fields, with its own label property, data index etc. (That is something like a div or span instead of an input type=text. I don't want input type text that is readonly=true and disabled=true, but just regular html text that is updated when the store is updated.)

    <%@ Page Language="C#" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>Loading form using nested JSON</title>
    
        <ext:ResourcePlaceHolder ID="ResourcePlaceHolder1" Mode="Script" runat="server" />
    
        <script type="text/javascript">
            Ext.ns("FormExample");
            
            FormExample.sampleData1 = {
                success: true,
                data: {
                    id: '0001',
                    name: 'Joe',
                    address: {
                        firstLine: '10 Acacia Ave',
                        city: 'London',
                        country: 'UK',
                        postalCode: 'who cares'
                    },
                    contact: {
                        phone: [ '07123123122', '02081111111' ],
                        email: [ 'test1@test.com', 'test2@test.com']
                    }
                }
            };
    
            FormExample.sampleData2 = {
                success: true,
                data: {
                    id: '0002',
                    name: 'Blogs',
                    address: {
                        firstLine: '11 Acacia Ave',
                        city: 'Birmingham',
                        country: 'UK',
                        postalCode: 'who cares2'
                    },
                    contact: {
                        phone: [ '07111111111', '02082222222' ],
                        email: [ 'test3@test.com', 'test4@test.com']
                    }
                }
            };
    
            FormExample.loadData = function(data) {
                Store1.loadData(data);
    
                Ext.getCmp('FormPanel1').getForm().loadRecord(Store1.getAt(0));
            };
                
        </script>
    
    </head>
    <body>
        <ext:ResourceManager ID="ResourceManager1" runat="server" />
    
        <form id="HtmlForm" runat="server">
    
            <ext:Viewport runat="server" Layout="BorderLayout">
                <Bin>
                    <ext:Store ID="Store1" IDMode="Explicit" runat="server">
                        <Reader>
                            <ext:JsonReader IDProperty="id" Root="data" SuccessProperty="success">
                                <Fields>
                                    <ext:RecordField Name="id" />
                                    <ext:RecordField Name="name" />
                                    <ext:RecordField Name="AddressLine" Mapping="address.firstLine" />
                                    <ext:RecordField Name="AddressCity" Mapping="address.city" />
                                    <ext:RecordField Name="AddressCountry" Mapping="address.country" />
                                    <ext:RecordField Name="AddressPostalCode" Mapping="address.postalCode" />
                                </Fields>
                            </ext:JsonReader>
                        </Reader>
                    </ext:Store>
                </Bin>
                <Items>
                    <ext:Panel runat="server" region="West" Width="200">
                        <Items>
                            <ext:Button Text="Load data 1" runat="server">
                                <Listeners>
                                    <Click Handler="FormExample.loadData(FormExample.sampleData1);" />
                                </Listeners>
                            </ext:Button>
                            <ext:Button Text="Load data 2" runat="server">
                                <Listeners>
                                    <Click Handler="FormExample.loadData(FormExample.sampleData2);" />
                                </Listeners>
                            </ext:Button>
                        </Items>
                    </ext:Panel>
                    <ext:FormPanel ID="FormPanel1" IDMode="Explicit" runat="server" region="Center">
                        <Items>
                                <ext:TextField runat="server" ID="RecordId" DataIndex="id" FieldLabel="Id" />
                                <ext:TextField runat="server" ID="Name" DataIndex="name" FieldLabel="Name" />
                                <ext:TextField runat="server" ID="AddressLine" DataIndex="AddressLine" FieldLabel="Line" />
                                <ext:TextField runat="server" ID="AddressCity" DataIndex="AddressCity" FieldLabel="City" />
                                <ext:TextField runat="server" ID="AddressCountry" DataIndex="AddressCountry" FieldLabel="Country" />
                        </Items>
                    </ext:FormPanel>
                </Items>
            </ext:Viewport>
    
        </form>
    </body>
    </html>
    I guess my worst case option is to put a class on certain text fields and use a bunch of CSS to get rid of all the padding and borders etc! But I'd like to avoid that. I know I can use XTemplates but I was hoping ext:Label had a DataIndex property (though looking at the code and docs I can understand why it doesn't).

    If nothing exists would another option be to create a custom control? (If so, any pointers or guidelines on doing that the right way you could link me to as I imagine there'd be a number client side form related considerations as well?)

    Thanks!
    Last edited by Daniil; Jul 15, 2011 at 1:03 PM. Reason: [CLOSED]
  2. #2
    Hi @anup,

    Please use <ext:DisplayField>.

    Here is some (fake) example.

    Example

    <%@ Page Language="C#" %>
    
    <%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
    
    <script runat="server">
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!X.IsAjaxRequest)
            {
                this.Store1.DataSource = new object[] {
                    new 
                    {
                        df1 = "new text1",
                        df2 = "new text2"   
                    }    
                };
                this.Store1.DataBind();
            }
        }
    </script>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>Ext.Net Example</title>
    </head>
    <body>
        <form runat="server">
            <ext:ResourceManager runat="server" />
            <ext:Store ID="Store1" runat="server">
                <Reader>
                    <ext:JsonReader>
                        <Fields>
                            <ext:RecordField Name="df1" />
                            <ext:RecordField Name="df2" />
                        </Fields>
                    </ext:JsonReader>
                </Reader>
            </ext:Store>
            <ext:FormPanel ID="FormPanel1" runat="server">
                <Items>
                    <ext:DisplayField 
                        runat="server" 
                        FieldLabel="DisplayField 1" 
                        Text="Initial text 1" 
                        DataIndex="df1" />
                    <ext:DisplayField 
                        runat="server" 
                        FieldLabel="DisplayField 2" 
                        Text="Initial text 2" 
                        DataIndex="df2" />
                </Items>
            </ext:FormPanel>
            <ext:Button runat="server" Text="Load">
                <Listeners>
                    <Click Handler="FormPanel1.getForm().loadRecord(Store1.getAt(0));" />
                </Listeners>
            </ext:Button>
        </form>
    </body>
    </html>
  3. #3
    Awesome! Thanks!

    And embarrassing, now that it is so obvious - I kept search for labels etc, and 4 books I had on ExtJs didn't mention it (though maybe also coz I didn't know to look for it - now i google for extjs displayfield, it is everywhere :)
  4. #4
    Maybe a small example using DisplayField can be added to the Examples Explorer as well?
  5. #5
    I agree such example would be good. We will consider it, thanks.
  6. #6
    Many thanks. (You can mark as solved, too)
  7. #7
    We extended this example
    https://examples1.ext.net/#/GridPane.../Form_Details/
    to demonstrate that DisplayField can be used with .DataIndex as well as other Fields. Now ID field is a DisplayField in the FormPanel.

    For now the update is available in SVN only, the online version will be updated soon.

    Thanks for the suggestion.

Similar Threads

  1. [MVC + Razor] How to load data on the form?
    By Natalie in forum 2.x Help
    Replies: 4
    Last Post: Sep 18, 2012, 10:24 AM
  2. Store data does not load in panel's field.
    By ankit in forum 1.x Help
    Replies: 0
    Last Post: May 25, 2010, 8:44 AM
  3. how to load data to grid panel in page load
    By andylaiyongsing in forum 1.x Help
    Replies: 1
    Last Post: Apr 16, 2010, 10:27 AM
  4. Replies: 3
    Last Post: Feb 25, 2010, 10:25 AM
  5. [CLOSED] Form Panel with Labels
    By ashton.lamont in forum 1.x Legacy Premium Help
    Replies: 5
    Last Post: Nov 17, 2009, 11:38 AM

Posting Permissions