2012-05-30 189 views
2

我想在ExtJS 4.1中創建一個ItemSelector,但它們似乎沒有兩個列表的工作「標題」屬性。這正是我想要的:Ext JS 4 ItemSelector示例

http://dev.sencha.com/deploy/ext-4.0.0/examples/multiselect/multiselect-demo.html

(觀察兩個標題:「可用」和「選擇」)

,這裏是「同樣的例子」適用於4.1版本:

http://dev.sencha.com/deploy/ext-4.1.0-gpl/examples/multiselect/multiselect-demo.html

這就是爲什麼我已經下載ExtJS的4.1和更早的版本(4.0.7)的添加ItemSelector和多選文件。然後我複製了第一個示例中的幾乎所有內容,但沒有完成任何操作,因此我沒有提供任何錯誤消息。

請告訴我應該如何做這樣一個ItemSelector工作(最好使用ext JS 4.1,因爲它是最新版本,並且從一個已棄用的版本開始似乎是錯誤的,但在這一點上,任何工作解決方案都會這樣做,因爲我用完了想法)。

這裏是我的html文件:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 

插入標題這裏

<!-- ExtJS --> 
<link rel="stylesheet" type="text/css" href="./resources/css/ext-all.css" /> 
<script type="text/javascript" src="bootstrap.js"></script> 

<!-- Shared --> 
<!--  <link rel="stylesheet" type="text/css" href="../shared/example.css" /> --> 

<!-- Example --> 
<script type="text/javascript" src="app.js"></script> 
<link rel="stylesheet" type="text/css" href="./resources/css/ItemSelector.css" /> 

和app.js文件:

Ext.Loader.setConfig({enabled: true}); 
//Ext.Loader.setPath('Ext.ux', './ux'); 
Ext.require([ 
    'Ext.form.Panel', 
    'Ext.ux.form.MultiSelect', 
    'Ext.ux.form.ItemSelector' 
]); 

Ext.onReady(function(){ 
console.log("ready"); 

/* 
* Ext.ux.form.MultiSelect Example Code 
*/ 
var msForm = Ext.widget('form', { 
    title: 'MultiSelect Test', 
    width: 400, 
    bodyPadding: 10, 
    renderTo: 'multiselect', 
    items:[{ 
     anchor: '100%', 
     xtype: 'multiselect', 
     msgTarget: 'side', 
     fieldLabel: 'Multiselect', 
     name: 'multiselect', 

     allowBlank: false, 
     // minSelections: 2, 
     // maxSelections: 3, 

     store: [[123,'One Hundred Twenty Three'], 
       ['1', 'One'], ['2', 'Two'], ['3', 'Three'], ['4', 'Four'], ['5', 'Five'], 
       ['6', 'Six'], ['7', 'Seven'], ['8', 'Eight'], ['9', 'Nine']], 

     value: ['3', '4', '6'], 

     ddReorder: true 
    }], 

    tbar:[{ 
     text: 'Options', 
     menu: [{ 
      text: 'Set value (2,3)', 
      handler: function(){ 
       msForm.getForm().findField('multiselect').setValue(['2', '3']); 
      } 
     },{ 
      text: 'Toggle enabled', 
      handler: function(){ 
       var m = msForm.getForm().findField('multiselect'); 
       if (!m.disabled) { 
        m.disable(); 
       } else { 
        m.enable(); 
       } 
      } 
     },{ 
      text: 'Toggle delimiter', 
      handler: function() { 
       var m = msForm.getForm().findField('multiselect'); 
       if (m.delimiter) { 
        m.delimiter = null; 
        Ext.Msg.alert('Delimiter Changed', 'The delimiter is now set to <b>null</b>. Click Save to ' + 
            'see that values are now submitted as separate parameters.'); 
       } else { 
        m.delimiter = ','; 
        Ext.Msg.alert('Delimiter Changed', 'The delimiter is now set to <b>","</b>. Click Save to ' + 
            'see that values are now submitted as a single parameter separated by the delimiter.'); 
       } 
      } 
     }] 
    }], 

    buttons: [{ 
     text: 'Clear', 
     handler: function(){ 
      var field = msForm.getForm().findField('multiselect'); 
      if (!field.readOnly && !field.disabled) { 
       field.clearValue(); 
      } 
     } 
    }, { 
     text: 'Reset', 
     handler: function() { 
      msForm.getForm().reset(); 
     } 
    }, { 
     text: 'Save', 
     handler: function(){ 
      if(msForm.getForm().isValid()){ 
       Ext.Msg.alert('Submitted Values', 'The following will be sent to the server: <br />'+ 
        msForm.getForm().getValues(true)); 
      } 
     } 
    }] 
}); 

console.log(msForm); 


var ds = Ext.create('Ext.data.ArrayStore', { 
    data: [[123,'One Hundred Twenty Three'], 
     ['1', 'One'], ['2', 'Two'], ['3', 'Three'], ['4', 'Four'], ['5', 'Five'], 
     ['6', 'Six'], ['7', 'Seven'], ['8', 'Eight'], ['9', 'Nine']], 
    fields: ['value','text'], 
    sortInfo: { 
     field: 'value', 
     direction: 'ASC' 
    } 
}); 

/* 
* Ext.ux.form.ItemSelector Example Code 
*/ 
var isForm = Ext.widget('form', { 
    title: 'ItemSelector Test', 
    width: 700, 
    bodyPadding: 10, 
    renderTo: 'itemselector', 

    tbar:[{ 
     text: 'Options', 
     menu: [{ 
      text: 'Set value (2,3)', 
      handler: function(){ 
       isForm.getForm().findField('itemselector').setValue(['2', '3']); 
      } 
     },{ 
      text: 'Toggle enabled', 
      handler: function(){ 
       var m = isForm.getForm().findField('itemselector'); 
       if (!m.disabled) { 
        m.disable(); 
       } else { 
        m.enable(); 
       } 
      } 
     },{ 
      text: 'Toggle delimiter', 
      handler: function() { 
       var m = isForm.getForm().findField('itemselector'); 
       if (m.delimiter) { 
        m.delimiter = null; 
        Ext.Msg.alert('Delimiter Changed', 'The delimiter is now set to <b>null</b>. Click Save to ' + 
            'see that values are now submitted as separate parameters.'); 
       } else { 
        m.delimiter = ','; 
        Ext.Msg.alert('Delimiter Changed', 'The delimiter is now set to <b>","</b>. Click Save to ' + 
            'see that values are now submitted as a single parameter separated by the delimiter.'); 
       } 
      } 
     }] 
    }], 

    items:[{ 
     xtype: 'itemselector', 
     name: 'itemselector', 
     anchor: '100%', 
     fieldLabel: 'ItemSelector', 
     imagePath: '../ux/images/', 

     store: ds, 
     displayField: 'text', 
     valueField: 'value', 
     value: ['3', '4', '6'], 

     allowBlank: false, 
     // minSelections: 2, 
     // maxSelections: 3, 
     msgTarget: 'side' 
    }], 

    buttons: [{ 
     text: 'Clear', 
     handler: function(){ 
      var field = isForm.getForm().findField('itemselector'); 
      if (!field.readOnly && !field.disabled) { 
       field.clearValue(); 
      } 
     } 
    }, { 
     text: 'Reset', 
     handler: function() { 
      isForm.getForm().reset(); 
     } 
    }, { 
     text: 'Save', 
     handler: function(){ 
      if(isForm.getForm().isValid()){ 
       Ext.Msg.alert('Submitted Values', 'The following will be sent to the server: <br />'+ 
        isForm.getForm().getValues(true)); 
      } 
     } 
    }] 
}); 

}); 

最後,這是我的文件夾結構:

-app.js 
-bootstrap.js 
-ext-all-debug.js 
-NewFile.html 
-ux 
    -form 
     -ItemSelector.js 
     -MultiSelector.js 
     -layout 
       -ItemSelector.js 
       -MultiSelect.js 
-resources 
    -css 
     -ext-all.css 
     -ItemSelector.css 
+0

要解決這個問題,需要對一些已經存在問題的代碼進行一些非常討厭的重寫。這是爲了學習的目的還是爲了生產所需的修復? – pllee

+0

@pllee我需要這樣一個小部件來製作。此代碼是從Sencha演示和教程複製的。我認爲這是正確的。我對Ext JS非常陌生,我所做的所有編碼都是在我面前的文檔。這就是爲什麼我無法確定任何事情的原因。任何提示將有所幫助。 – Dragos

回答

0

爲了得到它的工作,你可以把這些覆蓋在你的應用代碼執行。 (這需要運行在4.1使它運行在4.0使用Ext.override)

Ext.define('Ext.ux.form.override.MultiSelect', { 
    override : 'Ext.ux.form.MultiSelect', 

    setupItems : function() { 
     var me = this; 
     me.boundList = Ext.create('Ext.view.BoundList', { 
      deferInitialRefresh : false, 
      multiSelect : true, 
      store : me.store, 
      displayField : me.displayField, 
      disabled : me.disabled 
     }); 
     me.boundList.getSelectionModel().on('selectionchange', me.onSelectChange, me); 
     //START OVERRIDE 
     this.selectedPanel = new Ext.panel.Panel({ 
      bodyStyle : 'border: 0;', 
      layout : 'fit', 
      title : me.title, 
      tbar : me.tbar, 
      items : me.boundList 
     }); 

     return { 
      xtype : 'container', 
      layout : 'fit', 
      items : this.selectedPanel 
     }; 
    } 
}); 

Ext.define('Ext.ux.form.override.ItemSelector', { 
    override : 'Ext.ux.form.ItemSelector', 

    fromTitle : 'Available', 
    toTitle : 'Selected', 

    setupItems : function() { 
     var items = this.callParent(); 

     this.fromField.selectedPanel.setTitle(this.fromTitle); 
     this.toField.selectedPanel.setTitle(this.toTitle); 

     return items; 
    } 
}) 

我會建議不要在生產中使用此組件。正如你已經知道它不被Ext支持,它很容易改變。此外,代碼非常糟糕,兩個組件之間的耦合也不夠緊密。

1

這是一個工作示例MultiSelect & ItemSelector - 4.1

請確保您的Ext.ux.form.ItemSelectorExt.ux.form.MultiSelect版本與extjs 4.1源中的相同(可在extjs-4.1.1/examples/ux/form目錄中找到)。

我有同樣的問題和更新MultiSelect.jsItemSelector.js解決了我的問題。

3
items:[{ 
    xtype: 'itemselector', 
    name: 'itemselector', 
    anchor: '100%', 
    fieldLabel: 'ItemSelector', 
    imagePath: '../ux/images/', 

    store: ds, 
    displayField: 'text', 
    valueField: 'value', 
    value: ['3', '4', '6'], 

    allowBlank: false, 
    // minSelections: 2, 
    // maxSelections: 3, 
    fromTitle : 'Available' 
    toTitle : 'Selected' 
    msgTarget: 'side' 
}], 
0

有一些版本的Extjs與Itemselector有bug。這很容易克服。
1.轉到您的Itemselector.js文件並查找setupItems函數。
2.此函數使用createList()函數來創建FromField和ToField。
3.函數重載:創建另一個函數,就像createList()並傳遞一個參數(這是你想要設置的標題)(例如:createList(param))
4.在createList(param)函數中,只需添加標題:param
5.現在在您的setupItems函數中用createList('Avaiable')和createList('Selected')替換createList()。
6.你已完成。

例如代碼:

createList: function(param){ 
    var me = this; 
    return Ext.create('Ext.ux.form.MultiSelect', { 
      submitValue: false, 
      flex: 1, 
      dragGroup: me.ddGroup, 
      dropGroup: me.ddGroup, 
      store: { 
       model: me.store.model, 
        data: [] 
      }, 
      title: param, 
      displayField: me.displayField, 
      disabled: me.disabled, 
      listeners: { 
       boundList: { 
         scope: me, 
         itemdblclick: me.onItemDblClick, 
         drop: me.syncValue 
      } 
      } 
    }); 
}, 

setupItems: function() { 
     var me = this; 

     me.fromField = me.createList('Available'); 
     me.toField = me.createList('Selected'); 

     // add everything to the from field at the start 
     me.fromField.store.add(me.store.getRange()); 

     return { 
      layout: { 
       type: 'hbox', 
       align: 'stretch' 
      }, 
      items: [ 
       me.fromField, 
       { 
        xtype: 'container', 
        margins: '0 4', 
        width: 22, 
        layout: { 
         type: 'vbox', 
         pack: 'center' 
        }, 
        items: me.createButtons() 
       }, 
       me.toField 
      ] 
     }; 
    }, 

感謝。