【问题标题】:Select2 Ajax Laravel 5.2 - Paginated results not working?Select2 Ajax Laravel 5.2 - 分页结果不起作用?
【发布时间】:2018-01-17 02:30:11
【问题描述】:

我有以下 select2 可以通过分页加载远程数据:

$(".js-location-lookup").select2({
    ajax: {
        url: '/locations',
        dataType: "json",
        delay: 150,
        data: function (params) {
            return {
                term: params.term, 
                page: params.page
            };
        },
        processResults: function (data, params) {

            params.page = params.page || 1;

            return {
                results:  $.map(data.data, function (item) {
                    return {
                        id: item.id,
                        text: item.name

                    };
                }),
                pagination: {
                    more: (params.page * data.per_page) < data.total
                }

            };
        },
        cache: true
    },
    minimumInputLength: 2,
    placeholder: "-- Select --",
    allowClear: true
});

控制器动作:

 public function getLocations(Request $request)
{
    $term = $request->input('term');

    $data = Location::where('name', 'LIKE', '%'.$term.'%')->paginate(5);

    $data->appends(['term' => $term ]);

    return response()->json($data);

}

json:

{
   "total":22,
   "per_page":5,
   "current_page":1,
   "last_page":5,
   "next_page_url":"/locations?term=en&page=2",
   "prev_page_url":null,
   "from":1,
   "to":5,
    "data":[
        {"id":1,"name":"England"},
        {"id":13,"name":"London (Central)"},
        {"id":18,"name":"North East England"},
        {"id":23,"name":"North West England"},
        {"id":30,"name":"South East England"}
     ]
  }

只有第一页加载并显示文本load more results,但它不滚动或执行任何操作来显示下一组结果?

【问题讨论】:

  • 第二次请求page: params.page时的页码是多少?
  • @GovindSamrow 在下面看到我的回答。

标签: ajax pagination laravel-5.2 jquery-select2


【解决方案1】:

如果显示时每页的项目数小于下拉框打开时的高度,则选择小部件上的滚动条不会出现,并且会观察到上述行为,即您无法滚动加载下一组数据。

修复方法是将分页数增加到每页 10 个,以便它们超过下拉框的高度。

不确定您是否会将其归类为错误。

【讨论】:

    【解决方案2】:
    public function getdataforselect2(Request $request){
    
            if ($request->ajax()) {
    
                $term = trim($request->term);
                $posts = DB::table('channels')->select('id','name as text')
                    ->where('name', 'LIKE',  '%' . $term. '%')
                    ->orderBy('name', 'asc')->simplePaginate(10);
    
                $morePages=true;
              // $pagination_obj= json_encode($posts);
               if (empty($posts->nextPageUrl())){
                $morePages=false;
               }
                $results = array(
                  "results" => $posts->items(),
                  "pagination" => array(
                    "more" => $morePages
                  )
                );
    
                return \Response::json($results);
            }
        }
    
        <script type="text/javascript" src='//ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js'></script>
        <!--<![endif]-->
        <link href="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css" rel="stylesheet" />
    <script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script> <script >
    
    (function() {
    
      $("#channel_id").select2({
        placeholder: 'Channel...',
        // width: '350px',
        allowClear: true,
        ajax: {
            url: '/dataforselect2',
            dataType: 'json',
            delay: 250,
            data: function(params) {
                return {
                    term: params.term || '',
                    page: params.page || 1
                }
            },
            cache: true
        }
    });
    })();
    
        </script>
    
    

    代码here!

    【讨论】:

      【解决方案3】:

      将以下代码与最新的 select2 版本库一起使用。

      控制器代码:

      public function getLocations(Request $request)
      {
          if ($request->ajax()) {
              $page = $request->page;
              $resultCount = 5;
      
              $offset = ($page - 1) * $resultCount;
      
              $locations = Location::where('name', 'LIKE', '%' . $request->term. '%')
                                      ->orderBy('name')
                                      ->skip($offset)
                                      ->take($resultCount)
                                      ->selectRaw('id, name as text')
                                      ->get();
      
              $count = Count(Location::where('name', 'LIKE', '%' . $request->term. '%')
                                      ->orderBy('name')
                                      ->selectRaw('id, name as text')
                                      ->get());
      
              $endCount = $offset + $resultCount;
              $morePages = $count > $endCount;
      
              $results = array(
                    "results" => $locations,
                    "pagination" => array(
                        "more" => $morePages
                    )
                );
      
              return response()->json($results);
          }
          return response()->json('oops');
      }
      

      查看/JS 代码:

      $(document).ready(function() {
          $('.js-location-lookup').select2({
              delay : 200,
              ajax: {
                  url: '/locations',
                  dataType: 'json',
                  cache: true,
                  data: function(params) {
                    return {
                        term: params.term || '',
                        page: params.page || 1
                    }
                },
              }
          });
      });
      

      【讨论】:

        猜你喜欢
        • 2017-11-27
        • 2016-09-11
        • 2016-12-26
        • 2017-05-08
        • 2015-10-12
        • 2015-12-08
        • 2016-10-02
        • 2016-04-12
        • 2016-04-10
        相关资源
        最近更新 更多