2014-10-09 92 views
0

我在網格內有2個組合框。第二個組合框值將根據第一個組合框進行更改。

例如組合有3個項目:美國,歐洲,亞洲
如果在第一個組合框中選擇了歐洲,那麼在第二個組合框中,歐洲不會再出現。
當已經選擇時隱藏組合框的值extjs

我不知道我用它的ExtJS的版本,

但這裏的代碼:

我的組合STORE

​​


我的組合INSIDE GRID

var set_approval_dtl = Ext.create('Ext.Window', { 
title: title_approval2, width: 850, height: 395, rowdblclick: true, forceFit: true, 
closeAction: "hide", store: ms_set_approval_dtl_store, 
defaults: { 
    sortable: true, resizable: false 
}, 
items: [ 
    {xtype: "form", items: [ 
      {layout: 'column', columnWidth: .5, itemId: 'set_approve', defaults: {border: false}, 
       items: [{xtype: "panel", itemId: "set_approve_panel", height: 330, defaultType: 'textfield', margin: '0 10px 0 10px', 
         defaults: {labelWidth: 120, width: 850, maxLength: 200}, 
         items: [ 
          {xtype: "grid", itemId: "grid_items", width: 782, height: 280, margin: '0 10px 10px 10px', autoScroll: true, 
           plugins: Ext.create('Ext.grid.plugin.CellEditing', {clicksToEdit: 1, pluginId: 'rowEditing'}), 
           store: ms_set_approval_dtl_store, stripeRows: true, defaultType: "gridcolumn", 
           viewConfig: {forceFit: true}, 
           columns: [ 
            {header: grid18j, width: 150, dataIndex: 'nm_act', align: 'center'}, 
            {header: subtitle_approval3, width: 126, dataIndex: 'level1', align: 'center', 
             editor: {xtype: "combobox", name: "cdgr", itemId: "cdgr1", typeAhead: true, editable: false, triggerAction: "all", forceSelection: true, 
              emptyText: grid8k, store: cb_group, valueField: "id", displayField: "nm", 
              listeners: { 
               expand: function(field, options, val) { 
                if (Ext.typeOf(field.getPicker().loadMask) !== "boolean") { 
                 field.getPicker().loadMask.hide(); 
                } 
               }, 
               select: function(value) { 
                var obj = this.lastSelection[0].data; 
                return obj.nm; 
                this.lastSelection[0].hide; 
                cb_group.removeAt(0); 
               } 
              }}, 
             renderer: function(val) { 
              var index = cb_group.findExact('id', val); 
              if (index !== -1) { 
               var rs = cb_group.getAt(index).data; 
               return rs.nm; 
              } 
             } 
            }, 
            {header: subtitle_approval4, width: 126, dataIndex: 'level2', align: 'center', itemId: "level2", 
             editor: {xtype: "combobox", name: "cdgr", itemId: "cdgr2", typeAhead: true, editable: false, triggerAction: "all", forceSelection: true, 
              emptyText: grid8k, store: cb_group, valueField: "id", displayField: "nm", 
              listeners: { 
               expand: function(field, options) { 
                if (Ext.typeOf(field.getPicker().loadMask) !== "boolean") { 
                 field.getPicker().loadMask.hide(); 
                } 
               } 
              } 
             }, 
             select: function(value) { 
              var obj = this.lastSelection[0].data; 
              return obj.nm; 
             }, 
             renderer: function(val) { 
              var index = cb_group.findExact('id', val); 
              if (index !== -1) { 
               var rs = cb_group.getAt(index).data; 
               return rs.nm; 
              } 
             } 
            }] 
          }]} 
       ]}]} 
]}); 


我試過this.lastSelection [0] .hide;cb_group.removeAt(0);在第一個組合。但它根本不起作用。我不知道爲什麼我的選擇監聽器不工作。
請分享一些解決方案。謝謝

回答

-1

您將需要兩個商店,每個組合框一個,兩個商店都填充相同的數據。

然後你會做什麼:

combo1.on('select',function(combo, newVal) { 
    combo2.getStore().filterBy(function(rec){ 
     return rec.get("value")!=newVal; 
    }) 
}); 
+0

所以我必須爲每個組合框2個商店? 以及我在哪裏放置你上面提到的功能?外部var set_approval_dtl? – 2014-10-09 07:13:48

+0

兩個組合框中的每一個的一個商店總共是兩個商店。您可以將我的函數放入'listeners:{select:function(combo,newVal)...',因爲在組件上使用偵聽器對象與使用'component.on()'相同。 – Alexander 2014-10-09 07:37:12

+0

哦,所以每個組合框有不同的商店,但數據是相同的? 我不能只調用組合框名稱。它在螢火蟲上出現錯誤,說我的組合名稱沒有定義。 – 2014-10-09 07:41:31

0

您可以使用XTemplates來管理這種行爲有一個商店和兩個組合框。

首先,你必須爲你的組合框的項目列表中創建一個XTemplate:

// displayfield = displayfield configured in your combobox 
var template = Ext.create('Ext.XTemplate', 
    '<tpl for=".">', 
    ' <tpl if="[Ext.getCmp(\'combobox1\').getValue()] != id && [Ext.getCmp(\'combobox2\').getValue()] != id">', 
    ' <div class="x-boundlist-item">{label}</div>', 
    ' <tpl else>', 
    ' <tpl if="id == null || id == \'\'">', 
    '  <div class="x-boundlist-item">{label}</div>', 
    ' <tpl else>', 
    '  <div class="x-boundlist-item" style="font-size:0px; height:0px;"></div>', 
    ' </tpl>', 
    ' </tpl>', 
    '</tpl>' 
); 

的XTemplate包含一些語句,如果特定的值在組合框的一個已經被選擇進行檢查。如果不是,則該條目將出現在下拉列表中,否則將被隱藏。要使其工作,你必須設置在你的組合框模板和一些偵聽器添加到他們:

// Combobox 1 
{ 
    xtype: 'combo', 
    id: 'combobox1', 
    store: 'your_store', 
    tpl: template, 
    displayField: 'label', 
    valueField: 'id', 
    listeners: { 
    beforeSelect: function (combo, record, index, eOpts) 
    { 
     // Check if the selected value is already selected in combobox2 
     var cbx2value = !!Ext.getCmp('combobox2').getValue() ? Ext.getCmp('combobox2').getValue() : ''; 

     if (cbx2value != record.get('id') && cbx2value != record.get('id')) { 
      return true; // selected entry will be selected successfully 
     } else { 
      return false; // selected entry will not be selected 
     } 
    }, 
    change: function() 
    { 
     // Get the picker (list of items) of the other combobox and refresh it's template state 
     var cbx2picker = Ext.getCmp('combobox2').getPicker(); 
     cbx2picker.refresh(); 
    } 
} 

// Combobox 2 
{ 
    xtype: 'combo', 
    id: 'combobox2', 
    store: 'your_store', 
    tpl: template, 
    displayField: 'label', 
    valueField: 'id', 
    listeners: { 
    beforeSelect: function (combo, record, index, eOpts) 
    { 
     // Check if the selected value is already selected in combobox2 
     var cbx1value = !!Ext.getCmp('combobox1').getValue() ? Ext.getCmp('combobox1').getValue() : ''; 

     if (cbx1value != record.get('id') && cbx1value != record.get('id')) { 
      return true; // selected entry will be selected successfully 
     } else { 
      return false; // selected entry will not be selected 
     } 
    }, 
    change: function() 
    { 
     // Get the picker (list of items) of the other combobox and refresh it's template state 
     var cbx1picker = Ext.getCmp('combobox1').getPicker(); 
     cbx1picker.refresh(); 
    } 
} 

這不是最終的解決辦法,但我的項目之一,它的工作就像一個魅力。我儘可能簡化了示例,使解決方案更加清晰。

+0

感謝您的幫助!但似乎有些東西在我身上不起作用。我的螢火蟲說Ext.getCmp(...)是不確定的。你能解釋一下嗎? – 2014-10-13 02:28:07

+0

您嘗試呼叫的組件之一不存在於給定的ID中。您還可以下載快速顯示javascript錯誤的「web developer」插件。 – Tyr 2014-10-21 17:08:03