3 This file is part of Ext JS 4
5 Copyright (c) 2011 Sencha Inc
7 Contact: http://www.sencha.com/contact
9 GNU General Public License Usage
10 This file may be used under the terms of the GNU General Public License version 3.0 as published by the Free Software Foundation and appearing in the file LICENSE included in the packaging of this file. Please review the following information to ensure the GNU General Public License version 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
12 If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
16 * @class Ext.ux.form.MultiSelect
17 * @extends Ext.form.field.Base
18 * A control that allows selection and form submission of multiple list items.
21 * 2008-06-19 bpm Original code contributed by Toby Stuart (with contributions from Robert Williams)
22 * 2008-06-19 bpm Docs and demo code clean up
25 * Create a new MultiSelect
26 * @param {Object} config Configuration options
29 Ext.define('Ext.ux.form.MultiSelect', {
30 extend: 'Ext.form.field.Base',
31 alternateClassName: 'Ext.ux.Multiselect',
32 alias: ['widget.multiselect', 'widget.multiselectfield'],
36 'Ext.ux.layout.component.form.MultiSelect',
42 * @cfg {String} listTitle An optional title to be displayed at the top of the selection list.
46 * @cfg {String/Array} dragGroup The ddgroup name(s) for the MultiSelect DragZone (defaults to undefined).
50 * @cfg {String/Array} dropGroup The ddgroup name(s) for the MultiSelect DropZone (defaults to undefined).
54 * @cfg {Boolean} ddReorder Whether the items in the MultiSelect list are drag/drop reorderable (defaults to false).
59 * @cfg {Object/Array} tbar An optional toolbar to be inserted at the top of the control's selection list.
60 * This can be a {@link Ext.toolbar.Toolbar} object, a toolbar config, or an array of buttons/button configs
61 * to be added to the toolbar. See {@link Ext.panel.Panel#tbar}.
65 * @cfg {String} appendOnly True if the list should only allow append drops when drag/drop is enabled
66 * (use for lists which are sorted, defaults to false).
71 * @cfg {String} displayField Name of the desired display field in the dataset (defaults to 'text').
76 * @cfg {String} valueField Name of the desired value field in the dataset (defaults to the
77 * value of {@link #displayField}).
81 * @cfg {Boolean} allowBlank False to require at least one item in the list to be selected, true to allow no
82 * selection (defaults to true).
87 * @cfg {Number} minSelections Minimum number of selections allowed (defaults to 0).
92 * @cfg {Number} maxSelections Maximum number of selections allowed (defaults to Number.MAX_VALUE).
94 maxSelections: Number.MAX_VALUE,
97 * @cfg {String} blankText Default text displayed when the control contains no items (defaults to 'This field is required')
99 blankText: 'This field is required',
102 * @cfg {String} minSelectionsText Validation message displayed when {@link #minSelections} is not met (defaults to 'Minimum {0}
103 * item(s) required'). The {0} token will be replaced by the value of {@link #minSelections}.
105 minSelectionsText: 'Minimum {0} item(s) required',
108 * @cfg {String} maxSelectionsText Validation message displayed when {@link #maxSelections} is not met (defaults to 'Maximum {0}
109 * item(s) allowed'). The {0} token will be replaced by the value of {@link #maxSelections}.
111 maxSelectionsText: 'Maximum {0} item(s) allowed',
114 * @cfg {String} delimiter The string used to delimit the selected values when {@link #getSubmitValue submitting}
115 * the field as part of a form. Defaults to ','. If you wish to have the selected values submitted as separate
116 * parameters rather than a single delimited parameter, set this to <tt>null</tt>.
121 * @cfg {Ext.data.Store/Array} store The data source to which this MultiSelect is bound (defaults to <tt>undefined</tt>).
122 * Acceptable values for this property are:
123 * <div class="mdetail-params"><ul>
124 * <li><b>any {@link Ext.data.Store Store} subclass</b></li>
125 * <li><b>an Array</b> : Arrays will be converted to a {@link Ext.data.ArrayStore} internally.
126 * <div class="mdetail-params"><ul>
127 * <li><b>1-dimensional array</b> : (e.g., <tt>['Foo','Bar']</tt>)<div class="sub-desc">
128 * A 1-dimensional array will automatically be expanded (each array item will be the combo
129 * {@link #valueField value} and {@link #displayField text})</div></li>
130 * <li><b>2-dimensional array</b> : (e.g., <tt>[['f','Foo'],['b','Bar']]</tt>)<div class="sub-desc">
131 * For a multi-dimensional array, the value in index 0 of each item will be assumed to be the combo
132 * {@link #valueField value}, while the value at index 1 is assumed to be the combo {@link #displayField text}.
133 * </div></li></ul></div></li></ul></div>
136 componentLayout: 'multiselectfield',
138 fieldBodyCls: Ext.baseCSSPrefix + 'form-multiselect-body',
142 initComponent: function(){
145 me.bindStore(me.store, true);
146 if (me.store.autoCreated) {
147 me.valueField = me.displayField = 'field1';
148 if (!me.store.expanded) {
149 me.displayField = 'field2';
153 if (!Ext.isDefined(me.valueField)) {
154 me.valueField = me.displayField;
160 bindStore: function(store, initial) {
163 boundList = me.boundList;
165 if (oldStore && !initial && oldStore !== store && oldStore.autoDestroy) {
169 me.store = store ? Ext.data.StoreManager.lookup(store) : null;
171 boundList.bindStore(store || null);
177 onRender: function(ct, position) {
179 panel, boundList, selModel;
181 me.callParent(arguments);
183 boundList = me.boundList = Ext.create('Ext.view.BoundList', {
186 displayField: me.displayField,
190 selModel = boundList.getSelectionModel();
192 selectionChange: me.onSelectionChange,
196 panel = me.panel = Ext.create('Ext.panel.Panel', {
204 // Must set upward link after first render
207 // Set selection to current value
208 me.setRawValue(me.rawValue);
211 // No content generated via template, it's all added components
212 getSubTplMarkup: function() {
217 afterRender: function() {
221 if (me.ddReorder && !me.dragGroup && !me.dropGroup){
222 me.dragGroup = me.dropGroup = 'MultiselectDD-' + Ext.id();
225 if (me.draggable || me.dragGroup){
226 me.dragZone = Ext.create('Ext.view.DragZone', {
228 ddGroup: me.dragGroup,
229 dragText: '{0} Item{1}'
232 if (me.droppable || me.dropGroup){
233 me.dropZone = Ext.create('Ext.view.DropZone', {
235 ddGroup: me.dropGroup,
236 handleNodeDrop: function(data, dropRecord, position) {
237 var view = this.view,
238 store = view.getStore(),
239 records = data.records,
242 // remove the Models from the source Store
243 data.view.store.remove(records);
245 index = store.indexOf(dropRecord);
246 if (position === 'after') {
249 store.insert(index, records);
250 view.getSelectionModel().select(records);
256 onSelectionChange: function() {
261 * Clears any values currently selected.
263 clearValue: function() {
268 * Return the value(s) to be submitted for this field. The returned value depends on the {@link #delimiter}
269 * config: If it is set to a String value (like the default ',') then this will return the selected values
270 * joined by the delimiter. If it is set to <tt>null</tt> then the values will be returned as an Array.
272 getSubmitValue: function() {
274 delimiter = me.delimiter,
276 return Ext.isString(delimiter) ? val.join(delimiter) : val;
280 getRawValue: function() {
282 boundList = me.boundList;
284 me.rawValue = Ext.Array.map(boundList.getSelectionModel().getSelection(), function(model) {
285 return model.get(me.valueField);
292 setRawValue: function(value) {
294 boundList = me.boundList,
297 value = Ext.Array.from(value);
302 Ext.Array.forEach(value, function(val) {
304 model = me.store.findRecord(me.valueField, val, undef, undef, true, true);
309 boundList.getSelectionModel().select(models, false, true);
316 valueToRaw: function(value) {
320 // compare array values
321 isEqual: function(v1, v2) {
322 var fromArray = Ext.Array.from,
329 if (len !== v2.length) {
333 for(i = 0; i < len; i++) {
334 if (v2[i] !== v1[i]) {
342 getErrors : function(value) {
344 format = Ext.String.format,
345 errors = me.callParent(arguments),
348 value = Ext.Array.from(value || me.getValue());
349 numSelected = value.length;
351 if (!me.allowBlank && numSelected < 1) {
352 errors.push(me.blankText);
354 if (numSelected < this.minSelections) {
355 errors.push(format(me.minSelectionsText, me.minSelections));
357 if (numSelected > this.maxSelections) {
358 errors.push(format(me.maxSelectionsText, me.maxSelections));
364 onDisable: function() {
366 this.disabled = true;
367 this.updateReadOnly();
370 onEnable: function() {
372 this.disabled = false;
373 this.updateReadOnly();
376 setReadOnly: function(readOnly) {
377 this.readOnly = readOnly;
378 this.updateReadOnly();
382 * @private Lock or unlock the BoundList's selection model to match the current disabled/readonly state
384 updateReadOnly: function() {
386 boundList = me.boundList,
387 readOnly = me.readOnly || me.disabled;
389 boundList.getSelectionModel().setLocked(readOnly);
393 onDestroy: function(){
394 Ext.destroyMembers(this, 'panel', 'boundList', 'dragZone', 'dropZone');