select2.js使用

3916 ワード







に値を付ける
var aothorityIds = aothorityId.split(",");//値を割り当てる文字列
console.log(aothorityIds);
var aothorityNames = aothorityName.split(",");//配列に変換
console.log(aothorityNames);
var objJson = [];//構築データ
for(var i=0;iobjJson.push(jQuery.parseJSON('{"id":"'+aothorityIds[i]+'", "text":"'+aothorityNames[i]+'"}'));//json文字列を構築し配列objJsonに格納
}
$("#authority").select2("data", objJson);//ドロップダウン・ボックスに値を割り当てる
console.log(JSON.stringify(objJson));//jsonデータを文字列に変換

<div class="form-group">
										<label class="control-label col-md-4">  </label>
										
										<div class="col-md-8">
											<select id="appCityCode" name="appCityCode" class="form-control select2" multiple="multiple" >
												<option></option>
											</select>
										</div>
									</div>

//select    (matcher)
var ComponentsDropdowns = function () {

    var handleSelect2 = function () {
        $('#appCityCode').select2({
            placeholder: "     ",
            allowClear: true,
            matcher: function(term, text, opt) {
            	return text.toUpperCase().indexOf(term.toUpperCase())>=0
                || opt.attr("shortNm").toUpperCase().indexOf(term.toUpperCase())>=0;
        	}
        });

        function format(state) {
            if (!state.id) return state.text; // optgroup
            return "<img class='flag' src='" + Metronic.getGlobalImgPath() + "flags/" + state.id.toLowerCase() + ".png'/>&nbsp;&nbsp;" + state.text;
        }
      
        function movieFormatResult(movie) {
            var markup = "<table class='movie-result'><tr>";
            if (movie.posters !== undefined && movie.posters.thumbnail !== undefined) {
                markup += "<td valign='top'><img src='" + movie.posters.thumbnail + "'/></td>";
            }
            markup += "<td valign='top'><h5>" + movie.title + "</h5>";
            if (movie.critics_consensus !== undefined) {
                markup += "<div class='movie-synopsis'>" + movie.critics_consensus + "</div>";
            } else if (movie.synopsis !== undefined) {
                markup += "<div class='movie-synopsis'>" + movie.synopsis + "</div>";
            }
            markup += "</td></tr></table>"
            return markup;
        }

        function movieFormatSelection(movie) {
            return movie.title;
        }

    }   

    return {
        init: function () {            
            handleSelect2();
            saleSelect2();
            citySelect2();
        }
    };
}();

参考サイト
http://ivaynberg.github.io/select2/#minimum