The Ext.NET team is thrilled to announce the official release of Ext.NET 4, including the new Spreadsheet Selection Model, Triton Theme and Advanced Color Picker!

What Kind of Datasource does Ext.Store accept ??

  1. #1

    What Kind of Datasource does Ext.Store accept ??

    I would like use combox for drop-down list.
    ASPX Code
    <ext:Store ID="StockRoomListStore" runat="server">
            <Reader>
                <ext:JsonReader>
                    <Fields>
                        <ext:RecordField Name="StockroomID" Type="String">
                        </ext:RecordField>
                        <ext:RecordField Name="StockChinName" Type="String">
                        </ext:RecordField>
                    </Fields>
                </ext:JsonReader>
            </Reader>
        </ext:Store>
    
    <ext:ComboBox ID="cmbStockRoomOut" runat="server" StoreID="StockRoomListStore" DataIndex="StockRoomID_Out"
                             Note="StockRoomOut" Mode="Local" ListWidth="160px" DisplayField="StockChinName"
                              ValueField="StockroomID" ItemSelector="tr.Stockroomlist-item">
      <Template ID="Template5" runat="server">
        <Html>
          <tpl for=".">
        <tpl if="[xindex] == 1">
        <table class="StockRoomlist">
        </tpl>
            <tr class="Stockroomlist-item">
        <td style="padding:6px 0px;">{StockChinName}</td>
        </tr>
        <tpl if="[xcount-xindex]==0">
        </table>
        </tpl>
        </tpl>
            </Html>
            </Template>
            <Triggers>
               <ext:FieldTrigger Icon="Clear" HideTrigger="true" />
            </Triggers>
            <Listeners>
               <BeforeQuery Handler="this.triggers[0][ this.getRawValue().toString().length == 0 ? 'hide' : 'show']();" />
               <TriggerClick Handler="if (index == 0) { this.focus().clearValue(); trigger.hide();}" />
               <Select Handler="{this.triggers[0].show();}" />
             </Listeners>
    </ext:ComboBox>
    Code Behind:
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!X.IsAjaxRequest) 
        { 
        StockRoomListStore.DataSource = GetStockroomList();
        StockRoomListStore.DataBind();
        }
    }
    private List<JsonObject> GetStockroomList()
    {
    
        DbCommand sql = _manageLogic.StockRoomList();///Get StockRoomList from SQL
        DataTable sourcetbl = sql.FillTable();
        if (sourcetbl == null || sourcetbl.Rows.Count == 0)
            return null;
        string json = _manageLogic.GetJSON(sourcetbl); ///Transfer DataTable Structure into JSON 
        List<JsonObject> list = JSON.Deserialize<List<JsonObject>>(json);
        
        return list;
                
    }
    public string GetJSON(DataTable dt)
    {
        StringBuilder sb = new StringBuilder();
        ///sb.Append("{\"totalCount\":" + dt.Rows.Count + ",\"data\":");
        sb.Append("[");
        try
        {
            if (dt.Rows.Count > 0)
            {
                Hashtable ht = new Hashtable();
                for (int i = 0; i < dt.Columns.Count; i++)
                {
                    ht.Add(i, dt.Columns[i].ColumnName);
                }
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    sb.Append("{");
                    for (int j = 0; j < dt.Columns.Count; j++)
                    {
                        sb.Append(string.Format("\"{0}\":\"{1}\",",
                        ht[j], dt.Rows[i][j].ToString()));
                    }
                    sb.Remove(sb.ToString().LastIndexOf(","), 1);
                    sb.Append("},");
                }
                sb.Remove(sb.ToString().LastIndexOf(","), 1);
                ht.Clear();
                ht = null;
            }
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }
        finally
        {
            ///sb.Append("]}");
            sb.Append("]");
        }
        return sb.ToString();
    }
    But my combobox shosw nothing. I wonder if my datasource type was wrong or other reason ????
  2. #2
    Hi,

    You should not need to convert your DataTable to a JSON string and back into a List of objects.

    You should be able to bind your .DataSource directly to the DataTable object.

    Maybe the following sample can help,

    http://examples1.ext.net/#/GridPanel...ata/DataTable/
    Geoffrey McGill
    Founder & CEO
  3. #3
    Quote Originally Posted by geoffrey.mcgill View Post
    Hi,

    You should not need to convert your DataTable to a JSON string and back into a List of objects.

    You should be able to bind your .DataSource directly to the DataTable object.

    Maybe the following sample can help,

    http://examples1.ext.net/#/GridPanel...ata/DataTable/
    Hello ,geoffrey

    Thanks for replying.

    I do that convertion (DataTable to a JSON string and back into a List of objects), because the previous post
    How to Transfer DataTable for store of gridpanel ?
    http://forums.ext.net/showthread.php...with-DataTable

    And then I try to find out a automated method for convertion.
    (The function should accept in any DataTable, and return a DataTable that Store can accept)
    How to Transfer DataTable for store of gridpanel ?
    http://forums.ext.net/showthread.php...9111#post79111

    If I bind .DataSource directly to the DataTable object. My comboBox can't display anything. If I do like first post, it works!!!!!

    I don't know why...Could you tell me ???

    Thank you very much.!!!
  4. #4
    Quote Originally Posted by kkp0633 View Post
    I would like use combox for drop-down list.
    ASPX Code
    <ext:Store ID="StockRoomListStore" runat="server">
            <Reader>
                <ext:JsonReader>
                    <Fields>
                        <ext:RecordField Name="StockroomID" Type="String">
                        </ext:RecordField>
                        <ext:RecordField Name="StockChinName" Type="String">
                        </ext:RecordField>
                    </Fields>
                </ext:JsonReader>
            </Reader>
        </ext:Store>
    
    <ext:ComboBox ID="cmbStockRoomOut" runat="server" StoreID="StockRoomListStore" DataIndex="StockRoomID_Out"
                             Note="StockRoomOut" Mode="Local" ListWidth="160px" DisplayField="StockChinName"
                              ValueField="StockroomID" ItemSelector="tr.Stockroomlist-item">
      <Template ID="Template5" runat="server">
        <Html>
          <tpl for=".">
        <tpl if="[xindex] == 1">
        <table class="StockRoomlist">
        </tpl>
            <tr class="Stockroomlist-item">
        <td style="padding:6px 0px;">{StockChinName}</td>
        </tr>
        <tpl if="[xcount-xindex]==0">
        </table>
        </tpl>
        </tpl>
            </Html>
            </Template>
            <Triggers>
               <ext:FieldTrigger Icon="Clear" HideTrigger="true" />
            </Triggers>
            <Listeners>
               <BeforeQuery Handler="this.triggers[0][ this.getRawValue().toString().length == 0 ? 'hide' : 'show']();" />
               <TriggerClick Handler="if (index == 0) { this.focus().clearValue(); trigger.hide();}" />
               <Select Handler="{this.triggers[0].show();}" />
             </Listeners>
    </ext:ComboBox>
    Code Behind:
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!X.IsAjaxRequest) 
        { 
        StockRoomListStore.DataSource = GetStockroomList();
        StockRoomListStore.DataBind();
        }
    }
    private List<JsonObject> GetStockroomList()
    {
    
        DbCommand sql = _manageLogic.StockRoomList();///Get StockRoomList from SQL
        DataTable sourcetbl = sql.FillTable();
        if (sourcetbl == null || sourcetbl.Rows.Count == 0)
            return null;
        string json = _manageLogic.GetJSON(sourcetbl); ///Transfer DataTable Structure into JSON 
        List<JsonObject> list = JSON.Deserialize<List<JsonObject>>(json);
        
        return list;
                
    }
    public string GetJSON(DataTable dt)
    {
        StringBuilder sb = new StringBuilder();
        ///sb.Append("{\"totalCount\":" + dt.Rows.Count + ",\"data\":");
        sb.Append("[");
        try
        {
            if (dt.Rows.Count > 0)
            {
                Hashtable ht = new Hashtable();
                for (int i = 0; i < dt.Columns.Count; i++)
                {
                    ht.Add(i, dt.Columns[i].ColumnName);
                }
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    sb.Append("{");
                    for (int j = 0; j < dt.Columns.Count; j++)
                    {
                        sb.Append(string.Format("\"{0}\":\"{1}\",",
                        ht[j], dt.Rows[i][j].ToString()));
                    }
                    sb.Remove(sb.ToString().LastIndexOf(","), 1);
                    sb.Append("},");
                }
                sb.Remove(sb.ToString().LastIndexOf(","), 1);
                ht.Clear();
                ht = null;
            }
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }
        finally
        {
            ///sb.Append("]}");
            sb.Append("]");
        }
        return sb.ToString();
    }
    But my combobox shosw nothing. I wonder if my datasource type was wrong or other reason ????
    Please forgive my foolishness. I know where goes wrong.It is the "key mapping" problem.

    It means all columnName you query from DB must consistent with the definition in RecordField of Store and any Controller using Store.

    In my example(Please Notice Bold Font):

    If the query result column is "STOCKROOMID","STOCKCHINNAME" from DB. It means:

    <ext:ComboBox ID="cmbStockRoomOut" runat="server" StoreID="StockRoomListStore" DataIndex="StockRoomID_Out"
                             Note="StockRoomOut" Mode="Local" ListWidth="160px" DisplayField="STOCKCHINNAME"
                              ValueField="STOCKROOMID" >
    <ext:Store ID="StockRoomListStore" runat="server">
            <Reader>
                <ext:JsonReader>
                    <Fields>
                        <ext:RecordField Name="STOCKROOMID" Type="String">
                        <ext:RecordField Name="STOCKCHINNAME" Type="String">
                    </Fields>
                </ext:JsonReader>
            </Reader>
        </ext:Store>
    Oh...I am so stupid.....Orz.....But after all, this is solved. I hope my experience can save others' time.

Similar Threads

  1. Replies: 5
    Last Post: Feb 01, 2012, 11:54 AM
  2. Replies: 6
    Last Post: Apr 24, 2011, 10:09 AM
  3. Replies: 2
    Last Post: Jan 10, 2011, 8:16 PM
  4. Store and clear datasource
    By glenh in forum 1.x Help
    Replies: 0
    Last Post: Oct 20, 2010, 5:18 AM
  5. why the DataSource of the store is null
    By animalisme in forum 1.x Help
    Replies: 2
    Last Post: Jun 22, 2009, 3:04 AM

Tags for this Thread

Posting Permissions

Subscribe now to get the latest Ext.NET happenings in our newsletter.