7

select2検索ボックスの代わりに使用しています。

ここでは、このような国の値をロードするために使用しています

$("#countries").select2({
    multiple: true,
    tags:["India", "Japan", "Australia","Singapore"],
    tokenSeparators: [","]
});

保存ボタンを押すと、サーバーに適切に送信されます。ここで問題は、サーバーに保存した後に国フィールドを変更したい場合、保存された値を国フィールドにロードする方法です。

これは、サーバーからデータを取得する方法です

$.getJSON('/dataprovider?data=fetchCountriesForm', function(opts) {

    //the opts here contains the json values of the countries.

    //what code should be written here to load the values in $('#countries).select2();

    //user can add some more countries or can remove the previously added countries. 

}
4

2 に答える 2

8

セクションのドキュメントを参照してくださいLoading Remote Data

次のような例が見つかります。

$("#e6").select2({
    placeholder: "Search for a movie",
    minimumInputLength: 1,
    ajax: { 
           // instead of writing the function to execute the request we use Select2's convenient helper
          url: "http://api.rottentomatoes.com/api/public/v1.0/movies.json"
          // Other stuffs
          }
});

また、次のようにすることもできます。

$.getJSON('/dataprovider?data=fetchCountriesForm', function(opts){
    $("#countries").select2({
        data: opts
    });
})

データJSONは次のような形式である必要があります。

[{id:0,text:'enhancement'},
 {id:1,text:'bug'},
 {id:2,text:'duplicate'},
 {id:3,text:'invalid'},
 {id:4,text:'wontfix'}
]
于 2013-04-15T08:20:54.150 に答える
4

このhttp://ivaynberg.github.io/select2/#event_ext_changeリンクを使用し、トリガー関数を使用して値をロードします

$.getJSON('/dataprovider?data=fetchCountriesForm', function(opts) {
        parent.parent.parent.showLoader();
        if (opts) {
            $("#countries").val(opts).trigger("change");
        } 

このトリックは、選択ボックスに値をロードします。

于 2013-04-15T10:56:41.760 に答える