在我的模型中,我将 idProperty 作为使用计算函数构造的复合键(也尝试了转换函数)。但是,无论您将什么字段设置为 idProperty,计算/转换函数都不会触发。文档中是否涵盖了这一点?另外,我该如何规避这个问题?我想我可以监听商店的负载并手动设置属性,但这很烦人。另外,我这样做是因为我想在刷新时保留网格之前的选择。
在下面的示例中,您将看到 console.log 不会触发,因此,当我单击“刷新”按钮时,我的选择将不会保留。这是example :
Ext.application({
name : 'Fiddle',
launch : function() {
Ext.define('MyModel', {
extend: 'Ext.data.Model',
idProperty: 'composite',
fields: [{
name: 'composite',
type: 'string',
calculate: function(data) {
console.log(data.name + data.num);
return ata.name + data.num;
}
}, {
name: 'name',
type: 'string'
}, {
name: 'num',
type: 'int'
}]
});
var store = Ext.create('Ext.data.Store', {
model: 'MyModel',
autoLoad: true,
proxy: {
type: 'ajax',
url: 'data1.json'
}
});
function onClickRefreshButton() {
store.load();
}
var button = Ext.create('Ext.button.Button', {
text: 'Refresh',
renderTo: Ext.getBody(),
listeners: {
click: onClickRefreshButton
}
});
var grid = Ext.create('Ext.grid.Panel', {
store: store,
renderTo: Ext.getBody(),
height: 300,
width: 300,
title: 'My Grid',
columns: [{
text: 'Name',
dataIndex: 'name'
}, {
text: 'Num',
dataIndex: 'num'
}]
});
}
});
最佳答案
我认为你可以使用 transform
在您的阅读器中运行,例如:
reader: {
type: 'json',
transform: {
fn: function(res) {
return res.map(function(item) {
item.composite = item.name + item.num;
return item;
});
}
}
}
If a transform function is set, it will be invoked just before readRecords executes. It is passed the raw (deserialized) data object. The transform function returns a data object, which can be a modified version of the original data object, or a completely new data object.
关于javascript - ExtJS:关联的 idProperty 的转换函数未触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34187998/