按特定 id 过滤 Ext.data.Store 会返回多个结果
Posted
技术标签:
【中文标题】按特定 id 过滤 Ext.data.Store 会返回多个结果【英文标题】:Filtering a Ext.data.Store by a particular id returns multiple results 【发布时间】:2011-03-04 14:42:31 【问题描述】:我正在尝试按节目 ID 过滤我的试镜列表,但是当节目 ID 为“1”时,所有节目 ID 为“1x”的试镜都将被返回。下面是我的代码。
试镜模特:
Ext.regModel("Audition",
fields: [
name: "id", type: "integer",
name: "show_id", type: "integer",
name: "date", type: "string",
name: "details", type: "string"
],
belongsTo: 'Show',
proxy:
type: 'ajax',
url : 'app/data/auditions.json',
reader:
type: 'json',
root: 'auditions',
id : 'id'
,
);
app.stores.auditions = new Ext.data.Store(
autoLoad: true,
model: 'Audition',
sorters: 'id'
);
显示型号:
app.models.Show = Ext.regModel("app.models.Show",
fields: [
name: "id", type: "integer",
name: "title", type: "string",
name: "description", type: "string",
name: "opening_night", type: "string",
name: "schedule", type: "string",
name: "producer", type: "string",
name: "director", type: "string",
name: "status", type: "string",
name: "poster", type: "string",
name: "year", type: "string"
],
hasMany: [ model: 'Audition', name: 'auditions',
model: 'Role', name: 'roles' ],
proxy:
type: 'ajax',
url : 'app/data/shows.json',
reader:
type: 'json',
root: 'shows',
id : 'id'
,
);
app.stores.shows = new Ext.data.Store(
autoLoad: true,
model: "app.models.Show",
sorters: 'title',
getGroupString : function(record)
return record.get('title')[0];
);
这是每个模型的 json 数据示例
试镜:
"auditions": [
"id" : "1",
"show_id" : "1",
"date" : "March 1, 2011",
"details" : "Details go here."
,
"id" : "2",
"show_id" : "2",
"date" : "March 2, 2011",
"details" : "Details go here."
,
"id" : "3",
"show_id" : "12",
"date" : "March 3, 2011",
"details" : "Details go here."
]
演出:
"shows": [
"id" : 1,
"title" : "The Diary of Anne Frank",
"status" : "Coming Soon",
"description" : "Show description goes here.",
"opening_night" : "2010-10-08",
"schedule" : "October 8-24, 2010",
"producer" : "Hello World",
"director" : "Hello World",
"year" : "2010",
"poster" : "thediaryofannefrank.jpg"
,
"id" : "2",
"title" : "It’s a Wonderful Life",
"status" : "Coming Soon",
"description" : "Show description goes here.",
"opening_night" : "2010-11-26",
"schedule" : "November 26 - December 19, 2010",
"producer" : "Hello World",
"director" : "Hello World",
"year" : "2010",
"poster" : "itsawonderfullife.jpg"
]
在每个节目的视图模板中,我按 show_id 过滤用于试镜的数据存储
app.stores.auditions.filter('show_id', show.id);
this.showAuditionsList = new Ext.List(
itemTpl: 'date',
store: app.stores.auditions
);
所以使用上面的代码,如果我在安妮·弗兰克的日记的节目页面上并且我想查看该节目的所有试镜 - 代码将返回试镜 1 和 3 - 即使试镜 3 有一个 show_id 12 个。
在我的测试中,我发现过滤器将保留所有 show_id 为“1”和“1X”(以 1 开头的任何内容)的试镜。有没有更好的方法来过滤商店,或者更好的是,查询商店以返回所有试镜?
谢谢!
【问题讨论】:
【参考方案1】:你也试过
app.stores.auditions.filter(
property: 'show_id',
value: show.id,
exactMatch: true
);
或
app.stores.auditions.filterBy(function(record, id)
return store.id = id;
, this);
或者,如果您只是想获取该特定记录;
var record = app.stores.auditions.getAt(app.stores.auditions.findExact('store_id', store.id));
【讨论】:
“exactMatch”属性成功了!非常感谢!!【参考方案2】:我发现要传递一个对象来过滤它必须实例化一个新的过滤器,如下所示:
store.filter(new Ext.util.Filter(property:"foo", value:"bar", exactMatch:true);
【讨论】:
以上是关于按特定 id 过滤 Ext.data.Store 会返回多个结果的主要内容,如果未能解决你的问题,请参考以下文章
在 Ext.data.Store 上使用 storeId 不会生成唯一实例