我终于成功了。我创建了两个解决方案 - 用于网格内的本地和远程下拉搜索。最后,我决定使用本地搜索(我可以添加country_id
to my cities
ExtJS 中的查询和过滤),但可以使其适用于远程搜索 - 如果有人需要,我也可以准备该解决方案。
LOCAL SOLUTION
我必须过滤cityCombo
使用 beforeQuery 事件,使用来自的 idcountryCombo
在同一行。这是代码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');
}
}
},
Now our countryCombo
拥有在城市发生变化时重置城市所需的所有信息。这是全部countryCombo
code:
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
and cityDropdownStore
,每次都会查询数据库而不是使用过滤器。如果您有太多城市需要进行本地过滤,则这种方法是必要的。我应该提到我并没有在我的应用程序中真正使用城市和国家,我只是创建这个示例来简化事情。
我对最终结果感到非常高兴 - 它提供了网格的所有优点以及通常仅在表单中可用的条件下拉菜单。