ExtJS 在数据存储中搜索值

发布于 2024-10-27 21:41:52 字数 805 浏览 1 评论 0原文

如何在数据存储中搜索特定记录?我尝试过 find() 和 findBy(),但都返回 -1。

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

我有一个带有客户列表的组合框。我想要的是能够为该组合设置默认值。我使用 setValue 正确设置了值,甚至可以使用 setRawValue 设置显示值,但我似乎无法根据 clientID 查询数据存储并获取要在 setRawValue 中使用的公司名称。

这有道理吗?

这是回答以下问题的数据存储代码(抱歉,它不允许我将其粘贴到那里)

var frmClientStore = 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'}
      ])
});

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 frmClientStore = 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'}
      ])
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

还如梦归 2024-11-03 21:41:52

您的配置存在几个问题。首先,读取的 id 属性应该是 idProperty 属性。商店的 id 属性应为 storeId 属性(id 已弃用)。然后,当您在代码中引用 clientStore 时,您的变量被称为 frmClientStore (可能是拼写错误)。

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'}
      ])
});

最后:当您尝试从中检索记录时,您确定您的商店已加载吗?

尝试

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

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'));
    }
});
携余温的黄昏 2024-11-03 21:41:52

您列出的方式应该是正确的。但是,我应该指出,字段名称区分大小写。它也有可能正在搜索一个字符串(如您所列出的)而不是一个数字(这可能是您想要的)。

假设“ClientID”是正确的字段名称,您应该尝试以下操作:

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

编辑

另外,我刚刚注意到您的 JsonReader 看起来有些不对劲。难道不应该更像这样吗:

//...
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'}
    ]
})

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'}
    ]
})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文