如果可能的话,我想在绑定(bind)中使用 WPF 转换器之类的东西。在 ExtJs 示例中,我看到三种类型:
- “{字段}”
- “你好{field}”
- “{!field}”
这个例子工作正常。
Ext.create('Ext.grid.Panel', {
store: store,
columns: [
{
xtype: 'widgetcolumn',
width: 50,
align: 'left',
widget: {
xtype: 'label',
bind: {
text: "{record.Text}",
}
}
},
]
});
但我需要更多。在飞行中改变一些东西或计算。例如,根据记录中的某些信息更改标签的样式,或者格式化和组合来自许多字段的信息。我想要这样的想法:
Ext.create('Ext.grid.Panel', {
store: store,
columns: [
{
xtype: 'widgetcolumn',
width: 50,
align: 'left',
widget: {
xtype: 'label',
bind: {
text: "{calculateText(record)}",
style: {
color: "{calculateColor(record)}"
}
}
}
},
]
});
我阅读了公式并尝试:
Ext.create('Ext.grid.Panel', {
store: store,
columns: [
{
xtype: 'widgetcolumn',
width: 50,
align: 'left',
widget: {
xtype: 'label',
viewModel:{
formulas: {
myText: function(get){
return get('record').Text + '$';
},
}
},
bind: {
text: "{myText}",
}
}
},
]
});
还有这个
Ext.create('Ext.grid.Panel', {
store: store,
columns: [
{
xtype: 'widgetcolumn',
width: 50,
align: 'left',
widget: {
xtype: 'label',
viewModel:{
formulas: {
myText: {
bind: {
text: "{record.Text}",
},
get: function(data){
return data.text + '$';
},
},
}
},
bind: {
text: "{myText}",
}
}
},
]
});
但是当我尝试将我的 ViewModel 添加到当前上下文时 - 带有“record”的父 ViewModel 松散且无法正常工作。 我做错了什么,我该怎么做?
最佳答案
我使用 rowViewModel 配置解决了我的问题。示例:
Ext.create('Ext.grid.Panel', {
store: store,
rowViewModel: {
formulas: {
myText: function(get){
return get('record.Text') + '$';
},
}
},
columns: [
{
xtype: 'widgetcolumn',
width: 50,
align: 'left',
widget: {
xtype: 'label',
bind: {
text: "{myText}",
}
}
},
]
});
关于javascript - ExtJs 与转换器的绑定(bind)(公式),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41871822/