我终于成功了。我创建了两个解决方案 - 用于网格内的本地和远程下拉搜索。最后,我决定使用本地搜索(我可以将country_id 添加到我的cities 查询中并在ExtJS 中进行过滤),但它可以用于远程搜索——如果有人需要,我可以准备解决办法。
LOCAL SOLUTION
我必须使用 beforeQuery 事件过滤 cityCombo,使用同一行中 countryCombo 的 id。这是cityCombo的代码:
var cityCombo = new Ext.form.ComboBox({
triggerAction: 'all',
mode: 'local',
lastQuery: '', // to make sure the filter in the store is not cleared the first time the ComboBox trigger is used
store: cityDropdownStore,
displayField: 'city',
valueField: 'city_id',
listeners: {
beforeQuery: function(query) {
currentRowId = myGrid.getSelectionModel().getSelected().data.country_id;
this.store.clearFilter();
this.store.filter( { property: 'country_id', value: currentRowId, exactMatch: true } );
}
}
});
如您所见,当双击网格内的cityCombo 时,我在当前行中得到country_id 并使用该值过滤cityStore。这需要cityStore 具有这些字段:id、country_id、city
仍然存在一个问题:当用户更改countryCombo 时,城市字段应该更改/警告用户它对于当前国家/地区不正确。解决方案很复杂...您可能知道,您无法获得对组合框的 parentGrid 的引用(否则您可以调用 countryCombo --> parentGrid --> currentRecord --> cityCombo --> change it)。
我尝试在网格本身上监听 rowchange 事件,但如果用户在更改 countryCombo 后直接单击另一行,则会更改错误行的城市。
解决方案有点先进:我必须将当前行的引用从网格的 beforeedit 事件中复制到 cityCombo。这是网格的监听器:
listeners: {
beforeedit: function(e) {
// reference to the currently clicked cell
var ed = e.grid.getColumnModel().getCellEditor(e.column, e.row);
if (ed && ed.field) {
// copy these references to the current editor (countryCombo in our case)
Ext.copyTo(ed.field, e, 'grid,record,field,row,column');
}
}
},
现在我们的countryCombo 拥有在城市发生变化时重置城市所需的所有信息。这是整个countryCombo 代码:
var countryCombo = new Ext.form.ComboBox({
id: 'skupina_combo',
typeAhead: true,
triggerAction: 'all',
lazyRender: true,
mode: 'local',
store: countryDropdownStore,
displayField: 'country',
valueField: 'country_id',
listeners: {
change: function(t, n, o) { // t is the reference to the combo
// I have t.record available only because I copied it in the beforeEdit event from grid
t.record.set('city_id', '0');
}
}
});
单元格渲染器对我过滤他们的商店没有问题,所以我只需要一个商店来进行渲染和组合框编辑(cityStore)。
远程解决方案需要我为城市创建两个商店 - cityRendererStore 和 cityDropdownStore,它们每次都查询数据库而不是使用过滤器。如果您有太多城市需要在本地进行过滤,那么这种方法是必要的。我应该提到我并没有在我的应用程序中真正使用城市和国家,我只是创建了这个示例来简化事情。
我对最终结果感到非常高兴 - 它提供了网格的所有好处以及通常仅在表单中可用的条件下拉菜单。