我正在尝试在一个模型中有一个类似于value (maxValue)
的计算字段,其中maxValue
是当前加载的所有其他记录中的最大值(考虑网格的当前页面)。
型号:
Ext.define('MyApp.model.Example', {
extend: 'Ext.data.Model',
fields: [
{name: 'id'},
{name: 'value'},
{name: 'calculated_value', convert: function(value, record){
//how to access all the records here, not just the current one?
}}
]
});
发布于 2017-02-21 20:06:58
模型不知道记录,它只表示单个记录,convert方法的目的是允许您转换值,或者将其他字段组合成单个值(注意,除非您定义“依赖”来引用其他字段,否则在这种情况下,只有在加载数据时才会调用转换,而不是当它所依赖的字段发生更改时调用转换)。
当您创建网格时,网格使用存储,存储包含记录的集合,这将是执行此操作的地方。
在你的存储配置中,你可以为“datachanged”添加一个监听器,它将在存储中添加或删除记录时触发,从这里你可以遍历存储中的所有记录,计算出最大值,并用它更新记录。
Ext.create('Ext.data.Store', {
model: 'Example',
proxy: {
type: 'ajax',
url : 'example.json',
reader: {
type: 'json'
}
},
listeners:{
datachanged:function(store){
var maxValue=store.max('value');
store.beginUpdate();
store.each(function(record){
record.set('calculated_value',maxValue);
});
store.endUpdate();
}
}
});
如果你是从服务器加载存储,那么你将实现一个阅读器,这可能是一个更好的地方。
Ext.create('Ext.data.Store', {
model: 'Example',
proxy: {
type: 'ajax',
url : 'example.json',
reader: {
type: 'json',
transform: {
fn: function(data) {
var maxValue=0;
Ext.each(data.items,function(item){
if(item.value>maxValue) maxValue=item.value;
});
Ext.each(data.items,function(item){
item.calculated_value=maxValue;
});
return data;
},
scope: this
}
}
},
});
同样值得澄清的是,如果你真的需要复制这个值,我假设你想要以某种方式在网格内引用,可能是在渲染器中,而不是只需在存储上设置该值:
Ext.create('Ext.data.Store', {
model: 'Example',
proxy: {
type: 'ajax',
url : 'example.json',
reader: {
type: 'json'
}
},
listeners:{
datachanged:function(store){
store.maxValue=store.max('value');
}
}
});
然后在您的网格列配置中,添加/更新一个渲染器(在本例中,我将值显示为maxValue的百分比):
{
dataIndex:'value',
renderer:function(value, metaData, record, rowIndex, colIndex, store, view){
return Math.round((100/store.maxValue)*value)+'%';
}
}
https://stackoverflow.com/questions/42374586
复制相似问题