如何有条件地过滤商店?
How Do I Conditionally Filter a store?
我是 ExtJS 的新手,我的商店有问题。我在弹出窗口中有两个组合框,每个组合框都设置为一个商店。
items: [
{
xtype: 'combobox', itemId: 'facility', name: 'facilityId', fieldLabel: 'Facility', displayField: 'name', valueField: 'id', queryMode: 'local', allowOnlyWhitespace: false, forceSelection: true,
store: {
model: 'Common.model.Facility', autoDestroy: true, sorters: 'name'
},
listeners: {
change: function (field, newValue) { this.up('window').filterLocations(); }
}
},
{
xtype: 'combobox', fieldLabel: 'Location', name: 'locationId', displayField: 'name', valueField: 'id', queryMode: 'local', allowOnlyWhitespace: false, forceSelection: true,
store: {
model: 'Common.model.PrimaryLocation', autoDestroy: true, sorters: 'name'
},
// Strange hack from Sencha to get around filter-on-load issue http://www.sencha.com/forum/showthread.php?80079-Problem-to-filter-a-combobox-s-store-at-store-s-load-event
triggerAction: 'all', lastQuery: ''
}
],
这里(我认为)在另一个代码文件中引用了它(我现在只 post 我关心的那个):
// Load comboboxes
window.getFacilityId().getStore().load({
scope: this,
callback: function (records, operation, success) {
if (!success) {
Ext.log({ level: 'error', msg: 'Error loading facility list', dump: operation });
var text = (operation.getError() ? operation.getError().response.responseText : operation.getResponse().responseText);
var msg = Ext.decode(text).message;
Ext.Msg.show({ title: 'Error', msg: 'Error loading facility data.<br>' + msg, buttons: Ext.Msg.OK, icon: Ext.Msg.ERROR });
}
else {
// Select first entry if only one
if (records.length == 1)
window.getFacilityId().setValue(records[0].get('id'));
}
}
});
表单上有一个字段调用它具有我需要对其进行比较的值。如果该值为 null 或 0,那么我真的不需要做任何事情,因为它会显示数据库中的所有设施。
但是,如果它不是 null 或 0,那么我需要过滤商店以仅显示列表中与我正在比较的值相匹配的单个设施。
如何使这个过滤器起作用?为简单起见,假设设施商店只有 id 和 name 作为字段,并假设我将其与之比较的值是 facilityId。
类似于:
if (facilityId == null)
// What?
Thank you.
在商店中使用 load
方法之前,如果满足您的条件,请设置过滤器:
window.getFacilityId().getStore().setFilters({
property: 'id',
operator: '=',
value: facilityId
});
我是 ExtJS 的新手,我的商店有问题。我在弹出窗口中有两个组合框,每个组合框都设置为一个商店。
items: [
{
xtype: 'combobox', itemId: 'facility', name: 'facilityId', fieldLabel: 'Facility', displayField: 'name', valueField: 'id', queryMode: 'local', allowOnlyWhitespace: false, forceSelection: true,
store: {
model: 'Common.model.Facility', autoDestroy: true, sorters: 'name'
},
listeners: {
change: function (field, newValue) { this.up('window').filterLocations(); }
}
},
{
xtype: 'combobox', fieldLabel: 'Location', name: 'locationId', displayField: 'name', valueField: 'id', queryMode: 'local', allowOnlyWhitespace: false, forceSelection: true,
store: {
model: 'Common.model.PrimaryLocation', autoDestroy: true, sorters: 'name'
},
// Strange hack from Sencha to get around filter-on-load issue http://www.sencha.com/forum/showthread.php?80079-Problem-to-filter-a-combobox-s-store-at-store-s-load-event
triggerAction: 'all', lastQuery: ''
}
],
这里(我认为)在另一个代码文件中引用了它(我现在只 post 我关心的那个):
// Load comboboxes
window.getFacilityId().getStore().load({
scope: this,
callback: function (records, operation, success) {
if (!success) {
Ext.log({ level: 'error', msg: 'Error loading facility list', dump: operation });
var text = (operation.getError() ? operation.getError().response.responseText : operation.getResponse().responseText);
var msg = Ext.decode(text).message;
Ext.Msg.show({ title: 'Error', msg: 'Error loading facility data.<br>' + msg, buttons: Ext.Msg.OK, icon: Ext.Msg.ERROR });
}
else {
// Select first entry if only one
if (records.length == 1)
window.getFacilityId().setValue(records[0].get('id'));
}
}
});
表单上有一个字段调用它具有我需要对其进行比较的值。如果该值为 null 或 0,那么我真的不需要做任何事情,因为它会显示数据库中的所有设施。 但是,如果它不是 null 或 0,那么我需要过滤商店以仅显示列表中与我正在比较的值相匹配的单个设施。
如何使这个过滤器起作用?为简单起见,假设设施商店只有 id 和 name 作为字段,并假设我将其与之比较的值是 facilityId。
类似于:
if (facilityId == null)
// What?
Thank you.
在商店中使用 load
方法之前,如果满足您的条件,请设置过滤器:
window.getFacilityId().getStore().setFilters({
property: 'id',
operator: '=',
value: facilityId
});