php - 在 Kartik Select2 小部件中向下滚动时加载越来越多的数据

标签 php yii2 jquery-select2 yii-extensions kartik-v

我正在开发库存管理系统,有大量产品,客户希望在 Select2 小部件中显示产品,就像 kartik 小部件一样,并且他希望在向下滚动时表示越来越多的数据,是吗?可用或可以使用此小部件完成,或者我必须创建自己的? 这是我的代码

<?php
$url = Url::to ( [ 'products-list' ] );
echo Select2::widget ( [
    'name' => 'state_10' ,
    'options' => [
        'id' => 'select_product' ,
        'placeholder' => 'Select Product...' ,
        'multiple' => false ,
        'class' => ''
    ] ,
    'pluginOptions' => [
        'allowClear' => true ,
        'minimumInputLength' => 1 ,
        'ajax' => [
            'url' => $url ,
            'dataType' => 'json' ,
            'data' => new JsExpression ( 'function(params) { return {q:params.term}; }' )
        ] ,
        'escapeMarkup' => new JsExpression ( 'function (markup) { return markup; }' ) ,
        'templateResult' => new JsExpression ( 'function(product) { console.log(product);return product.text; }' ) ,
        'templateSelection' => new JsExpression ( 'function (subject) { return subject.text; }' ) ,
    ] ,
] );
?>

我的 Controller 中有这个操作,一切都运行良好,但我需要开始在 select2 中显示一些产品,然后当滚动时开始变得越来越多

Controller 操作:

    public function actionProductsList($q = null, $id = null) {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $out = ['results' => ['id' => '', 'text' => '', 'qtyLeft' => '', 'serialNumbers' => '']];
        if (!is_null($q)) {
            $query = new Query;
            $query->select(['product_id as id', new Expression("CONCAT(product_name,' -- ',product_qty_left) AS text, product_qty_left as qtyLeft, s.serial_number as serialNumbers")])
                    ->from('product')
                    ->join('LEFT JOIN', 'serial_number s', 's.r_product_id = product.product_id')
                    ->where(['like', 'product_name', $q]);
//                    ->limit(20);
            $command = $query->createCommand();
            $data = $command->queryAll();
            $out['results'] = array_values($data);
        } elseif ($id > 0) {
            $out['results'] = ['id' => $id, 'text' => AppProduct::find()->where(['product_id' => $id])->one()->product_name];
        }
        return $out;
    }

最佳答案

根据使用分页的文档,您必须告诉 Select2 通过覆盖 ajax.data 设置向请求添加任何必要的分页参数。要检索的当前页面存储在 params.page 属性中。

因此您需要将Select2更改为以下内容

$url = Url::to ( [ 'products-list' ] );
echo Select2::widget ( [
    'name' => 'state_10' ,
    'options' => [
        'id' => 'select_product' ,
        'placeholder' => 'Select Product...' ,
        'multiple' => false ,
        'class' => ''
    ] ,
    'pluginOptions' => [
        'allowClear' => true ,
        'minimumInputLength' => 1 ,
        'ajax' => [
            'url' => $url ,
            'dataType' => 'json' ,
            'data' => new JsExpression ( 'function(params) { return {q:params.term, page:params.page || 1}; }' )
        ] ,
        'escapeMarkup' => new JsExpression ( 'function (markup) { return markup; }' ) ,
        'templateResult' => new JsExpression ( 'function(product) { console.log(product);return product.text; }' ) ,
        'templateSelection' => new JsExpression ( 'function (subject) { return subject.text; }' ) ,
    ] ,
] );

Select2 将期望响应中包含 pagination.more 值。 more 的值应该是 truefalse,它告诉 Select2 是否还有更多页面的结果可供检索:

{
  "results": [
    {
      "id": 1,
      "text": "Option 1"
    },
    {
      "id": 2,
      "text": "Option 2"
    }
  ],
  "pagination": {
    "more": true
  }
}

因此请修改您的服务器端代码以在查询中包含 $page 参数并包含 limitoffset。我当前使用 5 条记录作为限制,您可以更改它。

public function actionProductsList($page, $q = null , $id = null ) {
        $limit=5;
        $offset=($page-1)*$limit;

        Yii::$app->response->format = Response::FORMAT_JSON;
        $out = [ 'results' => [ 'id' => '' , 'text' => '' , 'qtyLeft' => '' , 'serialNumbers' => '' ] ];
        if ( !is_null ( $q ) ) {
            $query = new \yii\db\Query;
            $query->select ( [ 'product_id as id' , new Expression ( "CONCAT(product_name,' -- ',product_qty_left) AS text, product_qty_left as qtyLeft, s.serial_number as serialNumbers" ) ] )
                    ->from ( 'product' )
                    ->join ( 'LEFT JOIN' , 'serial_number s' , 's.r_product_id = product.product_id' )
                    ->where ( [ 'like' , 'product_name' , $q ] )
                    ->offset ( $offset )
                    ->limit ( $limit );
            $command = $query->createCommand ();
            $data = $command->queryAll ();
            $out['results'] = array_values ( $data );
            $out['pagination'] = [ 'more' => !empty($data)?true:false ];
        } elseif ( $id > 0 ) {
            $out['results'] = [ 'id' => $id , 'text' => AppProduct::find ()->where ( [ 'product_id' => $id ] )->one ()->product_name ];
        }
        return $out;
    }

更新

我在上面的代码中发现并更改了一些修复

  • 修复偏移量$offset=($page-1)*$limit;,而不是像->offset($page)那样使用$page 使用 $offset,如 ->offset($offset)
  • 正确配置更多结果,如果没有更多结果,则返回 false,否则即使没有更多结果,它也会不断向服务器发送 ajax 调用,因此将其更改为 $out['pagination'] = [ 'more' = > !empty($data)?true:false ];

关于php - 在 Kartik Select2 小部件中向下滚动时加载越来越多的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51406633/

相关文章:

php - 递归地倍数 foreach 或 RecursiveIteratorIterator

php - 异步功能不工作Android

php - yii2 pdf 生成不工作

yii2 - Yii 2 ActiveForm 表单字段如何在复选框列表中实现 "select all"选项?

jquery - Select2 下拉删除所需的边框颜色?

jquery - 如何在每个 select2 选择框周围设置边框?

php - 迷你购物车更改未生效 - Magento 2

php - 使用 PHP 检查数据库的时间间隔

php - 在 yii2 中使用缓存时呈现动态 csrf 隐藏输入

jquery - 如何使用 jquery 选择 select2 选择框的选项?