yii Select2分页显示正在加载更多结果...

xoshrz7s  于 2022-11-09  发布在  其他
关注(0)|答案(1)|浏览(335)

当我搜索用户名为“a”它只获取前5个用户名,并显示“正在加载更多的结果...”但它没有加载,当我调试它,页面参数没有增加它只是停留在1的地方,因为术语参数是正确的工作.下面是我的代码:

function getCommonAutoComplete(urlStr) {
  $(".item-dropdown").select2({
    placeholder: "[Select Item]",
    allowClear: true,
    ajax: {
      url: urlStr,
      dataType: "json",
      delay: 250,
      casesensitive: false,
      data: function(params) {
        return {
          q: params.term, // search term
          page: params.page || 1
        };
      },
      processResults: function(data, params) {
        var resData = [];
        data.forEach(function(value) {
          resData.push(value);
        });
        var page = params.page || 1;
        return {
          results: $.map(resData, function(item) {
            return {
              text: "[" + item.item_code + "] " + item.ItemName,
              id: item.id
            };
          }),
          pagination: {
            more: page * 5 <= data[0].total_count
          }
        };
      },
      cache: true
    },
    minimumInputLength: 1
  });
}

下面是我的服务器端代码,这里是控制器

public function actionSearchItem(){
  $string=$_GET['q'];
  $page= $_GET['page'];
  $data=$this->fin->getItemAjax($string,$page);
  echo CJSON::encode($data);exit;
}

这是我的模型

public function getItemAjax($string,$page){
    $resultCount = 5;
    $end = ($page - 1) * $resultCount;       
    $start = $end + $resultCount;
    $sql="SELECT item_name as ItemName,item_code,id from wp_item where item_name like '%$string%' LIMIT {$end},{$start}";
    $result = Yii::app()->db->createCommand($sql)->queryAll();
    foreach ($result as $itemKey => $ajaxValue) {
        $data[] = ['id'=>$ajaxValue['id'], 'ItemName'=>$ajaxValue['ItemName'],'item_code'=>$ajaxValue['item_code'], 'total_count'=>count($result)];
    }
    return $data;
}

这是我的Json数据

[
  { id: "2", name: "Tracy Moen DVM", total_count: 5 },
  { id: "3", name: "Miss Zena Swift Jr.", total_count: 5 },
  { id: "4", name: "Gail Kunde", total_count: 5 },
  { id: "5", name: "Edna Langworth", total_count: 5 },
  { id: "7", name: "Meta Weimann", total_count: 5 }
];

该问题的屏幕快照如下:

mcdcgff0

mcdcgff01#

您正在以错误的方式处理分页-这不是“start”和“end”,而是“limit”和“offset”。此外,total_count以错误的方式计算,您的查询容易受到SQL注入的攻击。您应该尝试以下操作:

public function getItemAjax($string, $page) {
    $resultCount = 5;
    $offset = ($page - 1) * $resultCount;       
    $limit = $resultCount;

    $sql = "SELECT item_name as ItemName, item_code, id from wp_item where item_name like '%:string%' LIMIT {$offset}, {$limit}";
    $result = Yii::app()->db->createCommand($sql)->queryAll(true, ['string' => $string]);

    $countSql = "SELECT count(*) from wp_item where item_name like '%:string%'"
    $totalCount = Yii::app()->db->createCommand($countSql)->queryScalar(['string' => $string]);

    foreach ($result as $itemKey => $ajaxValue) {
        $data[] = [
            'id' => $ajaxValue['id'], 
            'ItemName' => $ajaxValue['ItemName'],
            'item_code' => $ajaxValue['item_code'], 
            'total_count' => $totalCount,
        ];
    }
    return $data;
}

相关问题