我有一个多选组合框,其显示值我需要按照特定模式设置"选择[x]",其中x对应于所选项目的数量。不幸的是,在尝试使用多种不同的方法之后,我无法找到实现所需结果的方法。使用fieldSubTpl配置或覆盖valueToRaw()方法似乎是最有前途的方法。但是,我无法让他们中的任何一个工作。
我找到了一个ExtJS 6 here所需行为的工作示例。如果您选择多个值,组合框将显示" x值已选中"而不是简单地连接选定的值。
如何在ExtJS 4.1.1中获得相同的结果?
答案 0 :(得分:1)
您需要对displayTpl
使用combo
配置。
在 FIDDLE 中,我在 ExtJS 4.x 中创建了相同的示例,如您在 ExtJS 6.x 中提供的那样。它工作正常。希望这能指导您或帮助您实现所需的解决方案。
Ext.create('Ext.form.Panel', {
renderTo: Ext.getBody(),
title: 'Multiple Seletion Example in ExtJS 4.x for combo box',
items: [{
xtype: 'combo',
margin: 20,
fieldLabel: 'Character Name',
store: Ext.create('Ext.data.Store', {
fields: ['id', 'name'],
data: [{
id: 0,
name: 'John Snow'
}, {
id: 1,
name: 'Tyrion Lannister'
}, {
id: 2,
name: 'Morgan Dexter'
}, {
id: 3,
name: 'Lannister'
}, {
id: 4,
name: 'Silicon Vally'
}]
}),
displayField: 'name',
/*
If you use using this then initially you will see {0 values selected}
displayTpl: new Ext.XTemplate('{[values instanceof Array ? values.length === 1 ? values[0]["' + combo.displayField + '"] : values.length + " values selected" : values]}'),
*/
valueField: 'id',
queryMode: 'local',
multiSelect: true,
filterPickList: true,
listeners: {
render: function (combo) {
//Use propery {displayTpl}
//The template to be used to display selected records inside the text field. An array of the selected records' data will be passed to the template.
combo.displayTpl = new Ext.XTemplate('{[values instanceof Array ? values.length === 1 ? values[0]["' + combo.displayField + '"] : values.length + " values selected" : values]}');
/*
you can also use like below function
combo.displayTpl = new Ext.XTemplate('{[this.getDisplayField(values)]}', {
getDisplayField: function (values) {
if (Ext.isArray(values)) {
var len = values.length;
return len == 1 ? values[0].name : (len + ' values selected');
}
return values;
}
});*/
}
}
}]
});