开发者

ExtJS Search Datastore for value

开发者 https://www.devze.com 2023-02-21 02:08 出处:网络
How do I search a datastore for a specific record?I\'ve tried find() and findBy(), but both return -1.

How do I search a datastore for a specific record? I've tried find() and findBy(), but both return -1.

var index = clientStore.find('ClientID', '37');

I have a combo box with a list of clients. What I want is to be able to set a default value on that combo. I have the value setting correctly using setValue and I can even set the display value using setRawValue, but I can't seem to query the datastore based on the clientID and get the company name to use in setRawValue.

Does that make sense?

Here's the datastore code in response to the questions below (sorry it wouldn't let me paste it there)

var frmCli开发者_JS百科entStore = new Ext.data.Store({
    id: 'frmClientStore',
    proxy: new Ext.data.HttpProxy({
        url: 'url here', 
        method: 'POST'
    }),
    reader: new Ext.data.JsonReader({
        root: 'rows',
        id: 'recordID'
    },[
        {name: 'recordID', type: 'int', mapping: 'recordID'},
        {name: 'ClientID', type: 'int', mapping: 'clientID'},
        {name: 'CompanyName', type: 'string', mapping: 'companyName'}
      ])
});


There are several problems with your configuration. First of all the id-property of the read should be the idProperty-property. The the id-property of the store should be the storeId-property (id is deprecated). And then your variable is called frmClientStore while you're referencing clientStore in your code (might be a typo).

var frmClientStore = new Ext.data.Store({
    storeId: 'frmClientStore',
    proxy: new Ext.data.HttpProxy({
        url: 'url here', 
        method: 'POST'
    }),
    reader: new Ext.data.JsonReader({
        root: 'rows',
        idProperty: 'recordID'
    },[
        {name: 'recordID', type: 'int', mapping: 'recordID'},
        {name: 'ClientID', type: 'int', mapping: 'clientID'},
        {name: 'CompanyName', type: 'string', mapping: 'companyName'}
      ])
});

And finally: are you sure, your store has been loaded when you try to retrieve records from it?

Try

frmClientStore.load({
    callback: function(rs) {
        console.log(rs);
        console.log(this.find('ClientID', '37'));
    }
});


The way that you've listed should be correct. However, I should point out that the field name is case sensitive. It is also possible that it is searching for a string (as you've listed) instead of a number (which may be what you want).

Supposing that 'ClientID' is the correct field name, you should try the following:

var index = clientStore.find('ClientID', 37);

EDIT

Also, I've just noticed that something looks off about your JsonReader. Shouldn't it be more like this:

//...
reader: new Ext.data.JsonReader({
    root: 'rows',
    id: 'recordID'
    fields: [ //list fields here as part of the JsonReader
        {name: 'recordID', type: 'int', mapping: 'recordID'},
        {name: 'ClientID', type: 'int', mapping: 'clientID'},
        {name: 'CompanyName', type: 'string', mapping: 'companyName'}
    ]
})
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号