dongxieyi9115 2019-08-07 10:00
浏览 154

Select2 Pagination节目正在加载更多结果......

When i search for usernamed "a" it fetches only first 5 username and displays "Loading more results…" but its not loading, when i debugged it, the page parameter is not incremented it just stays 1 where as the term parameter is working correctly. Below is my code:

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
  });
}

Below is my server side code where this is controller

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

This is my model

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;
}

And here is my Json data

[
  { 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 }
];

Screeshot of the issue is below: enter image description here

  • 写回答

1条回答 默认 最新

  • douchen4534 2019-08-08 08:39
    关注

    You're handling pagination in a wrong way - this is not "start" and "end", but "limit" and "offset". Also total_count is calculated in a wrong way and you're query is vulnerable to SQL Injection. You should try something like this:

    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;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。