2016-11-17 141 views
0

虽然可以使用createSearchChoice将新值添加到Select2(3.5)元素,但如果它不在列表中,如何获得显示的新值?我可以在Select2列表中不添加值,但是如何显示不在列表中的默认值?显示Select2值不在列表中

$("#select").select2({ 
    placeholder: "Who is the invoice from?", 
    minimumInputLength: 2, 
    quietMillis: 300, 
    allowClear : true, 
    ajax: { 
     url: "accountsDataStore.xsp", 
     dataType: 'json', 
     data: function (term, page) { 
      return { 
       q: term, // search term 
       page_limit: 10 
      }; 
     }, 
     results: function (data, page) { return data; } 
    }, 

    // extra code to allow entering value not in list 
    id: function(object) { return object.text; }, 

    //Allow manually entered text in drop down. 
    createSearchChoice:function(term, data) { 
         if ($(data).filter(function() { 
            return this.text.localeCompare(term)===0; 
           }).length===0) { 
             return {id:term, text:term}; 
         } 
    }, 

    initSelection: function(element, callback) { 
         //sets a default value (if a value was selected previously) 
         //get the existing value 
         var id = $(element).val(); 

         //if a value was selected: perform an Ajax call to retrieve the text label  
         if (id !== "") { 
          $.ajax( 
           "accountsDataStore.xsp", { 
            data: { id: id }, 
            dataType: "json" 
           }).done(function(data) { 
             // ** TODO if value not found, display current element value -- HOW?? ** 
             callback(data); }); 
         } 
        } 

    }).on('change', function (evt) { 
           // Do something after value changes 
           } 
); 

initSelection,如果找不到默认值,然后如何显示当前值?

<input type="hidden" 
     id="view:_id1:_id4:callback1:inputName" 
     name="view:_id1:_id4:callback1:inputName" 
     aria-required="true" 
     value="ACME Company" 
     tabindex="-1" 
     class="select2-offscreen"> 

在形式中,值被设置,它只是没有在出现一个选定值的<span>显示。我们只是以某种方式将它添加到列表中?

谢谢。

回答

0

我个人通过正确使用视图控制器(viewScoped bean)来处理允许值不在列表中的select2元素,如下所示。

用下面的代码我做3两件事:

<xp:comboBox id="comboDemo" value="#{ctrl.comboValue}" validator="#{ctrl.validateCombo}"> 
    <xp:selectItems value="#{ctrl.comboChoices}" /> 
</xp:comboBox> 
  1. 我绑定值的组合
  2. 我要确保已创建和选择客户端的任何新的选项被接受
  3. 我加载默认选择为组合框

默认的选择是建立保持考虑当前的微博价值,除非它是null

public List<SelectItem> getComboChoices() { 
    if (comboChoices == null) { 
     comboChoices = new ArrayList<SelectItem>(); 

     // Complete with your own logic here 
     if (StringUtil.isNotEmpty(comboValue)) { 
      comboChoices.add(new SelectItem(comboValue, comboValue)); 
     } 
    } 

    return comboChoices; 
} 

一旦选择了一个新值,我需要确保它将被接受回到服务器。我利用验证阶段潜入新的价值。

public void validateCombo(FacesContext facesContext, UIComponent component, Object value) { 
    boolean isNewValue = true; 

    for (SelectItem item : comboChoices) { 
     if (value.equals(item.getValue())) { 
      isNewValue = false; 
      break; 
     } 
    } 

    if (isNewValue) { 
     String newValue = (String) value; 
     comboChoices.add(new SelectItem(newValue, newValue)); 
    } 
} 

以上是我所做的一个简单的版本。你可以让它更聪明,但我希望这个例子足够清晰。