簡體   English   中英

在單擊按鈕時在Select2中添加附加選定值?

[英]Adding additonal Selected value in Select2 on button click?

場景是,我只需單擊一個按鈕即可將一個值(選定的值)附加到select2上。 如果單擊按鈕,發生的事情是我選擇的其他值消失/清除。

僅選擇了1個值,這是我的按鈕功能中的值。 當直接在select2文本框中鍵入內容時,我可以選擇多個值,但是如果我單擊該按鈕,它的值不會添加到select2中。

如何在單擊按鈕上在select2中已選擇的數據上附加值或將附加值推入? 每次單擊按鈕時,應將一個新值添加到select2個所選值中。

我希望下面的代碼以及對我正在尋找的內容的描述對您有所幫助。 謝謝。

我正在使用Northwind數據庫進行測試。 (Robert King在“員工表”下)

<input type="button" onclick="Passvalue();"/>
<input type="text" id="eq" name="eq" style="width: 200px;" />

<script>
    $(function () {       
        $("#eq").select2({
            minimumInputLength: 3,
            multiple: true,
            ajax: {
                url: '/Employee/GetAllEmployees/',
                dataType: 'json',
                type: "GET",
                data: function (searhTerm) {               
                    return { query: searhTerm };
                },
                results: 
                    function (data) {                
                        return { results: data};
                    },                             
            },
            initSelection: function (element, callback) {
                var id=$(element).val();  //element value will be 'Robert';
                if (id!=="") {
                    $.ajax('/Employee/GetAllEmployees/', {
                        data: {
                            query: id
                        },
                        dataType: "json",
                        type: "GET",
                    }).done(function(data) { callback(data); });
                }               
                },
            createSearchChoice: function (term) {          
                return {id: term, text: term + ' (new)', title: term };    
            },

            formatResult: FormatContact,
            formatSelection: FormatContactSelection,
            escapeMarkup: function(m) {
                return m;
            }
        });
    });

    function FormatContact(contact) {

        return contact.text + "&nbsp;(" + contact.title + ")";
    }

    function FormatContactSelection(contact) {
        return " &nbsp; &nbsp;"+ contact.text;
    }

    function Passvalue() { 
        var test2 = "Robert"; //just an example, value 'Robert' to be passed on select2 for query     
        $('#eq').select2("val", [test2]);    
    }
</script>

我的動作控制器:

public ActionResult GetAllEmployees(string query)
            {
                var db = new Employee().GetAllEmployees(query).
                    ToList();        
                return Json(db, JsonRequestBehavior.AllowGet);
            }

BL:

public IQueryable<Object> GetAllEmployees(string search)
        {
            var ctx = new NorthwindEntities();
            var dbQuery =
            (from i in ctx.Employees
             where i.FirstName.Contains(search) || i.LastName.Contains(search)
             select new
                    {
                        id = i.EmployeeID,
                        text = i.FirstName + " " + i.LastName,
                        title = i.Title
                    });
           return dbQuery;
        }

不要使用“ val”,而要使用“ data”。 像這樣

this.$("#yourSelector").select2("data", [{ id: 1, text: "Some Text" },{ id: 2, text: "Some Other Text" }]);

所以像這樣的事情對你有用...

var existingData = this.$("#yourSelector").select2("data");
existingData.push({ id: 11, text: "Some Text" });
this.$("#yourSelector").select2("data", existingData);

PS:我尚未測試以上代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM