Javascript 我有10号的页面。每次ajax获取数据10。但select2无法重新排列选项组。这就是问题所在。获取页面数据后,如何重新排列选项组?请参阅我的代码,不要使用params.page,使用变量current_user,我在此处尝试并工作…您的代码未满足我的要求

Javascript 我有10号的页面。每次ajax获取数据10。但select2无法重新排列选项组。这就是问题所在。获取页面数据后,如何重新排列选项组?请参阅我的代码,不要使用params.page,使用变量current_user,我在此处尝试并工作…您的代码未满足我的要求,javascript,jquery-select2,jquery-select2-4,Javascript,Jquery Select2,Jquery Select2 4,我有10号的页面。每次ajax获取数据10。但select2无法重新排列选项组。这就是问题所在。获取页面数据后,如何重新排列选项组?请参阅我的代码,不要使用params.page,使用变量current_user,我在此处尝试并工作…您的代码未满足我的要求,我需要每页10个数据,然后重新排列select2组选项。此解决方案对我不起作用。你能解释一下为什么它会起作用吗? <div class="container"> <form id="frm"> <h1


我有10号的页面。每次ajax获取数据10。但select2无法重新排列选项组。这就是问题所在。获取页面数据后,如何重新排列选项组?请参阅我的代码,不要使用params.page,使用变量current_user,我在此处尝试并工作…您的代码未满足我的要求,我需要每页10个数据,然后重新排列select2组选项。此解决方案对我不起作用。你能解释一下为什么它会起作用吗?
<div class="container">
  <form id="frm">
    <h1>Solution 1</h1>
    <div class="row">
      <div class="col-4">
        <div class="form-group">
          <label for="tagInput">Get data by ajax calling</label>
          <select class="form-control" id="pictures_tag_input">
</select>
          <small class="form-text text-muted"><p class="text-info">Infinite Scroll</p></small>
        </div>
      </div>
    </div>
  </form>
</div>
$(document).ready(function() {
  // solution 1
  //example.com/articles?page[number]=3&page[size]=1
  http: $("#pictures_tag_input").select2({
    placeholder: "Search for options",
    ajax: {
      url: "https://jsonplaceholder.typicode.com/users/1/todos",
      dataType: "json",
      global: false,
      cache: true,
      delay: 250,
      minimumInputLength: 2,
      data: function(params) {
        // console.log(params.page || 1);
        return {
          q: params.term, // search term
          _page: params.page || 1,
          _limit: 10 // page size
        };
      },
      processResults: function(data, params) {
        params.page = params.page || 1;
        var datx = getNestedChildren(data);
        // console.log(datx);

        return {
          results: datx,
          pagination: {
            more: true
          }
        };
      } //end of process results
    } // end of ajax
  });

  function getNestedChildren(list) {
    var roots = [];
    for (i = 0; i < list.length; i += 1) {
      node = list[i];

      if (roots.length === 0) {
        var obj = {
          text: "User " + node.userId,
          children: [{ id: node.id, text: node.title }]
        };
        roots.push(obj);
      } else {
        var obj = {
          text: "User " + node.userId,
          children: [{ id: node.id, text: node.title }]
        };
        var rootArray = $.map(roots, function(val, i) {
          var vl = "User " + node.userId;
          if (val.text === vl) return val;
          else return undefined;
        });
        if (rootArray.length > 0) {
          var obj1 = {
            id: node.id,
            text: node.title
          };
          rootArray[0].children.push(obj1);
        } else {
          roots.push(obj);
        }
      }
    }
    return roots;
  }
});
curent_user = 1;
$(document).ready(function() {
  http: $("#pictures_tag_input").select2({
    placeholder: "Search for options",
    ajax: {
      url: "https://jsonplaceholder.typicode.com/users/1/todos",
      dataType: "json",
      global: false,
      cache: false,
      minimumInputLength: 2,
      data: function(params) {
       console.log("params",params || 1);
        return {
          q: params.term, // search term
          _page: curent_user,
          _limit: 10 // page size
        };
      },

      processResults: function(data, params) {
        curent_user += 2;
        var datx = getNestedChildren(data);
        console.log("data: ", data);

        return {
          results: datx,
          pagination: {
            more: true
          }
        };
      } //end of process results
    } // end of ajax
  });

  function getNestedChildren(list) {
    var roots = [];
    for (i = 0; i < list.length; i += 1) {
      node = list[i];

      if (roots.length === 0) {
        var obj = {
          text: "User " + node.userId,
          children: [{ id: node.id, text: node.title }]
        };
        roots.push(obj);
      } else {
        var obj = {
          text: "User " + node.userId,
          children: [{ id: node.id, text: node.title }]
        };
        var rootArray = $.map(roots, function(val, i) {
          var vl = "User " + node.userId;
          if (val.text === vl) return val;
          else return undefined;
        });
        if (rootArray.length > 0) {
          var obj1 = {
            id: node.id,
            text: node.title
          };
          rootArray[0].children.push(obj1);
        } else {
          roots.push(obj);
        }
      }
    }
    return roots;
  }
});
templateResult: function(data) {
 if (typeof data.children != 'undefined') {
  $(".select2-results__group").each(function() {
   if (data.text == $(this).text()) {
    return data.text = '';
   }
  });
 }
 return data.text;
}
processResults: function( json, params ){

    setTimeout( function() {

        var $prevOptions = false;
        var $prevGroup = false;

        // loop
        $('.select2-results__option[role="group"]').each(function(){

            // vars
            var $options = $(this).children('ul');
            var $group = $(this).children('strong');

            // compare to previous
            if( $prevGroup && $prevGroup.text() === $group.text() ) {
                $prevOptions.append( $options.children() );
                $(this).remove();
                return;
            }

            // update vars
            $prevOptions = $options;
            $prevGroup = $group;

        });

    }, 1 );

    return json;

}
$(document).ready(function() {
    $("#pictures_tag_input").select2({
        placeholder: "Search for options",
        templateResult: formatOptions,
        ajax: {
            url: "https://jsonplaceholder.typicode.com/users/1/todos",
            dataType: "json",
            global: false,
            cache: true,
            delay: 250,
            minimumInputLength: 2,
            data: function(params) {
                return {
                    q: params.term,
                    _page: params.page || 1,
                    _limit: 10
                };
            },
            processResults: function(data, params) {
                params.page = params.page || 1;

                return {
                    results: data,
                    pagination: {
                        more: true
                    }
                };
            } //end of process results

        } // end of ajax
    });

    function formatOptions(item, container, $el) {
        // optgroups section
        if (item.children && item.children.length > 0) {
            // don't format the repeated optgroups!
            if ($(".select2-results__group").text() === item.text) {
                return;
            }

            if ($('[aria-label="' + item.text + '"]').length > 0) {
                return;
            }

            // the first occasion of the given optgroup
            return $el;
        }

        // options section
        // here you can implement your own logic
        // if you want to customise the output of the options
        $el.addClass('something-special-result result');

        return $el;
    }

});