Select2

<select multiple="multiple" class="selectCategory"></select>
    /*
    set - $(".select2-selection__rendered").prepend(
            '<li class="select2-selection__choice" title="">'
            + '<span class="select2-selection__choice__remove" role="presentation">×</span>'
            + '<span id="35130077">wahaha</span>'
            + '</li>');

    get - $('.select2-selection__choice').each;
    */
    function initSelect2ForCategory() {
        $(".selectCategory").select2({
          ajax: {
            url: "https://api.github.com/search/repositories",
            dataType: 'json',
            data: function (params) {
              return {
                q: params.term, // search term
                page: params.page
              };
            },
            processResults: function (data, page) {
              return {
                results: data.items
              };
            },
            cache: true
          },
          escapeMarkup: function (markup) { return markup; }, 
          minimumInputLength: 1,
          templateResult: formatRepo, 
          templateSelection: formatRepoSelection 
        });
    }
    
    function formatRepo (repo) {
       if (repo.loading) return repo.text;
       return repo.full_name;    
   }

    function formatRepoSelection (repo) {
      return '<span id="' + repo.id + '">' + repo.full_name + '</span>';
    }

 

posted @ 2015-09-08 20:38  webglcn  阅读(792)  评论(0编辑  收藏  举报