2013-04-08 52 views
0

我爲multiselect創建了一個自定義xtype,但我無法理解需要執行哪些更改以將值保存爲字符串數組逗號分隔的字符串。將值保存爲字符串數組而不是逗號分隔的字符串在自定義xtype中CQ5

目前,它存儲的值如下

物業行業 類型字符串 政府,醫療

相反,我要保存的信息如下

屬性行業類型的String [] 價值政府,醫療

任何建議,指針高度讚賞。

CQ.Ext.form.Multiselect = CQ.Ext.extend(CQ.Ext.form.Field, { 
store:null, 
storeUrl:'', 
displayField:'text', 
valueField:'value', 
allowBlank:true, 
minLength:0, 
blankText:CQ.Ext.form.TextField.prototype.blankText, 
copy:false, 
allowDup:false, 
allowTrash:false, 
legend:null, 
focusClass:undefined, 
delimiter:',', 
view:null, 
dragGroup:null, 
dropGroup:null, 
tbar:null, 
appendOnly:false, 
sortField:null, 
sortDir:'ASC', 
defaultAutoCreate : {tag: "div"}, 


initComponent: function(){ 
    CQ.Ext.form.Multiselect.superclass.initComponent.call(this); 
    this.addEvents({ 
     'dblclick' : true, 
     'click' : true, 
     'change' : true, 
     'drop' : true 
    });  
}, 
onRender: function(ct, position){ 
    var fs, cls, tpl; 
    CQ.Ext.form.Multiselect.superclass.onRender.call(this, ct, position); 

    cls = 'ux-mselect'; 

    fs = new CQ.Ext.form.FieldSet({ 
     renderTo:this.el, 
     title:this.legend, 
     height:this.height, 
     width:this.width, 
     style:"padding:1px;", 
     tbar:this.tbar 
    }); 
    if(!this.legend){ 
    //fs.el.down('.'+fs.headerCls).remove(); 
    fs.body.addClass(cls); 
    } 
    tpl = '<tpl for="."><div class="' + cls + '-item'; 
    if(CQ.Ext.isIE || CQ.Ext.isIE7 || CQ.Ext.isOpera)tpl+='" unselectable=on'; 
    else tpl+=' x-unselectable"'; 
    tpl+='>{' + this.displayField + '}</div></tpl>'; 


    this.store = new CQ.Ext.data.JsonStore({ 
      autoload:true, 
      url: CQ.HTTP.externalize(this.storeUrl), 
      fields:['value','text'] 

    }); 

    this.store.load(); 


    this.view = new CQ.Ext.ux.DDView({ 
     multiSelect: true, store: this.store, selectedClass: cls+"-selected", tpl:tpl, 
     allowDup:this.allowDup, copy: this.copy, allowTrash: this.allowTrash, 
     dragGroup: this.dragGroup, dropGroup: this.dropGroup, itemSelector:"."+cls+"-item", 
     isFormField:false, applyTo:fs.body, appendOnly:this.appendOnly, 
     sortField:this.sortField, sortDir:this.sortDir 
    }); 

    fs.add(this.view); 

    this.view.on('click', this.onViewClick, this); 
    this.view.on('beforeClick', this.onViewBeforeClick, this); 
    this.view.on('dblclick', this.onViewDblClick, this); 
    this.view.on('drop', function(ddView, n, dd, e, data){ 
     return this.fireEvent("drop", ddView, n, dd, e, data); 
    }, this); 

    this.hiddenName = this.name; 
    var hiddenTag={tag: "input", type: "hidden", value: "", name:this.name}; 
    if (this.isFormField) { 
     this.hiddenField = this.el.createChild(hiddenTag); 
    } else { 
     this.hiddenField = CQ.Ext.get(document.body).createChild(hiddenTag); 
    } 
    fs.doLayout(); 
}, 

initValue:CQ.Ext.emptyFn, 

onViewClick: function(vw, index, node, e) { 
    var arrayIndex = this.preClickSelections.indexOf(index); 
    if (arrayIndex != -1) 
    { 
     this.preClickSelections.splice(arrayIndex, 1); 
     this.view.clearSelections(true); 
     this.view.select(this.preClickSelections); 
    } 
    this.fireEvent('change', this, this.getValue(), this.hiddenField.dom.value); 
    this.hiddenField.dom.value = this.getValue(); 
    this.fireEvent('click', this, e); 
    this.validate();  
}, 

onViewBeforeClick: function(vw, index, node, e) { 
    this.preClickSelections = this.view.getSelectedIndexes(); 
    if (this.disabled) {return false;} 
}, 

onViewDblClick : function(vw, index, node, e) { 
    return this.fireEvent('dblclick', vw, index, node, e); 
}, 

getValue: function(valueField){ 
    var returnArray = []; 
    var selectionsArray = this.view.getSelectedIndexes(); 
    if (selectionsArray.length == 0) {return '';} 
    for (var i=0; i<selectionsArray.length; i++) { 
     returnArray.push(this.store.getAt(selectionsArray[i]).get(((valueField != null)? valueField : this.valueField))); 
    } 
    return returnArray; 
}, 

setValue: function(values) { 
    var index; 
    var selections = []; 
    this.view.clearSelections(); 
    this.hiddenField.dom.value = ''; 

    if (!values || (values == '')) { return; } 

    if (!(values instanceof Array)) { values = values.split(this.delimiter); } 
    for (var i=0; i<values.length; i++) { 
     index = this.view.store.indexOf(this.view.store.query(this.valueField, 
      new RegExp('^' + values[i] + '$', "i")).itemAt(0)); 
     selections.push(index); 
    } 
    this.view.select(selections); 
    this.hiddenField.dom.value = values; 
    for (var i=0; i<values.length; i++) { 
    this.listOfIndustries=values[i]; 
    alert(values[i]); 
    } 
    this.validate(); 
}, 

getRawValue: function(valueField) { 
    var tmp = this.getValue(valueField); 

    if (!tmp) { 

     tmp = []; 
    } 

    return tmp; 
}, 

setRawValue: function(values){ 
    setValue(values); 
}, 

validateValue : function(value){ 
    if (value.length < 1) { // if it has no value 
     if (this.allowBlank) { 
      this.clearInvalid(); 
      return true; 
     } else { 
      this.markInvalid(this.blankText); 
      return false; 
     } 
    } 
    if (value.length < this.minLength) { 
     this.markInvalid(String.format(this.minLengthText, this.minLength)); 
     return false; 
    } 
    if (value.length > this.maxLength) { 
     this.markInvalid(String.format(this.maxLengthText, this.maxLength)); 
     return false; 
    } 
    return true; 
} 
}); 

CQ.Ext.reg("industriesmultiselect", CQ.Ext.form.Multiselect); 

Envionment CQ 5.5

回答

1

簡短的回答:
而不是使用一個隱藏字段來存儲你的價值觀,你需要使用多種基礎input元素,每個具有相同name屬性爲Sling Post Servlet將輸出解釋爲多值屬性。有關動態添加新字段的示例,請參閱/libs/cq/ui/widgets/source/widgets/form/MultiField.js的多字段控件的setValueaddItem方法。

更詳細的解釋:
它看起來像你的getValue做你所期望的,但問題是,這種方法是沒有得到所謂的以提供被提交的值。如果您在標準對話框中使用此小部件,則父級表單面板將在DOM層次結構中提交在其下面的輸入元素中指定的值。

換句話說,您需要將多個值應用於DOM元素。

CQ.Ext.form.Field,你只擴展定義了一個潛在的輸入元素,你想在setValue與你的價值觀陣列設置:

this.hiddenField.dom.value = values; 

onViewClick

this.hiddenField.dom.value = this.getValue(); 

由於hiddenField是一個'hidden'類型的輸入標籤,它包含一個字符串值,當你試圖以這種方式設置它時,實際上存儲了調用的結果在你的values數組上。這就是爲什麼你最終得到一串逗號分隔值被提交。

如果您希望此小工具與標準表單提交基礎結構配合使用,則需要維護一整套隱藏字段。或者,您可以在適當的地方實現您自己的提交事件偵聽器,並使用Ext或jQuery將您的數組的AJAX請求(直接從getValue())作爲參數之一發布。

相關問題