2014-09-05 107 views
4

我有一个网格,并希望使用组合框作为网格单元格编辑器。编辑组合框的值应取决于我的数据记录多个字段,所以我尝试设置组合框的值在网格的beforeEdit监听器,像这样:如何在ExtJS网格编辑器中正确设置组合框值?

beforeEdit: function (editor, e, options) { 
    var field = e.field; 
    var combo = e.grid.columns[e.colIdx].getEditor(e.record); 
    var force = e.record.get('forced'); 
    switch (force) { 
     case 'Y': 
      combo.setValue("Force active"); 
      break; 
     case 'N': 
      combo.setValue("Force inactive"); 
      break; 
     default: 
      combo.setValue("Default"); 
      break;        
    } 
} 

我的组合框像这样定义的,所以它包含每个在我的beforeEdit处理显示可能的选项:

editor: { 
    xtype: 'combobox', 
    forceSelection: true, 
    editable: false, 
    triggerAction: 'all', 
    allowBlank: false, 
    store: [ 'Default', 'Force active', 'Force inactive' ] 
} 

我的问题是,虽然正确的条目在下拉列表中选择,组合框的文本部分保持为空。

enter image description here

我怎样才能说服编辑组合框也显示在组合的文本框部分的价值?

这里有一个煎茶捣鼓这个便签:https://fiddle.sencha.com/#fiddle/9vd

回答

5

你应该能够完成你正在寻找通过使用编辑器中的emptyText配置做什么组合框组件,like so

beforeEdit: function (editor, e, options) { 
    var field = e.field; 
    var combo = e.grid.columns[e.colIdx].getEditor(e.record); 
    var force = e.record.get('forced'); 
    switch (force) { 
     case 'Y': 
      combo.setValue("Force active"); 
      break; 
     case 'N': 
      combo.setValue("Force inactive"); 
      break; 
     default: 
      combo.setValue("Default"); 
      break;       
    } 
    combo.emptyText = combo.getValue(); 
} 
0

你需要添加dataIndex:“calculated_active”,第二列

+0

谢谢。我同意我在创建小提琴时缺少了该配置选项,但似乎并未改变下拉文本值为空的事实。我还希望组合框的行为像一个HTML选择(没有自由格式的文本输入),所以我更新了我的小提琴以包含这些配置。现在,您可以更好地看到单击可编辑单元格时只显示空白的编辑器文本。 – 2014-09-05 17:52:46

+0

forceSelection是真的..所以当你点击单元格时,组合框试图找到预存的值ex:'Y'在它的存储女巫中没有值等于Y,所以当你点击它时显示一个空值。因此,要么删除forceSelection:true,要么将选定的值包含在组合框中的calculated_active列中 – I3i0 2014-09-05 22:39:39

+0

我不明白。我的问题的重点是我想显示与dataIndexed字段中的值不同的下拉列表。这是我练习的要点:试图在beforeEdit中用setValue设置组合框的值。显示编辑组合之前。我不希望该数据索引字段的值显示在我的组合框中。 – 2014-09-05 23:28:35

3

您可以设置在列渲染器显示值你想:(这里是一个fiddle

{ 
        text: 'Active?', 
        dataIndex: 'calculated_active', 
        editor: { 
         xtype: 'combobox', 
         forceSelection: true, 
         editable: false, 
         triggerAction: 'all', 
         allowBlank: false, 
         valueField:'value', 
         displayField:'descr', 
         store: Ext.create('Ext.data.Store',{ 
          fields:['descr','value'], 
          data:[{ 
           descr:'Default', 
           value:'' 
          },{ 
           descr:'Force active', 
           value:'Y' 
          },{ 
           descr:'Force inactive', 
           value:'N' 
          }] 
         }) 
        }, 
        flex: 1, 
        renderer:function(value,metaData,record){ 
         switch(value){ 
          case 'Y': 
          return "Force active"; 
         case 'N': 
          return "Force inactive"; 
         default: 
          return "Default"; 
         } 
        } 
       }