【问题标题】:kartik\Select2 in GridView Filter show id instead of nameGridView 过滤器中的 kartik\Select2 显示 id 而不是名称
【发布时间】:2018-02-22 15:28:10
【问题描述】:

我有一个 Gridview 并且在下拉列表中我决定使用 ajax 实现一个 select2 下拉列表,页面加载时间过长,在实现 ajax 之后性能提高了很多并且可以正常工作 ajax 搜索。 我的问题是,当我从列表中选择一个名称时,页面会重新加载并从我选择的那个 id 获取数据,但是在过滤器标题中,它显示 id 而不是显示名称。 下面的代码仅供用户使用,但组织和消息的列也有同样的问题。

Filters ID's instead of name

查看/index.php

 $formatJs = <<< 'JS'
 var id_user = function (id_user) {
 return id_user.id ? id_user.id : id_user.text;  
 }
 JS;
 $this->registerJs($formatJs, \yii\web\View::POS_HEAD);
 ... 
  [
        'attribute' => 'id_user',
        'label' => Yii::t("app", "User"),
        'value' => function ($model) {
            return Notification::findOne($model->id)->idUser->name;
        },
        'filterType' => GridView::FILTER_SELECT2,
        'filterWidgetOptions' => ['options' => ['placeholder' => ''], 'pluginOptions' => ['allowClear' => true, 'minimumInputLength' => 3,
            'ajax' => [
                'url' => Url::to(['user-list']),
                'dataType' => 'json',
                'data' => new JsExpression('function(params){return{q:params.term};}'),
                'cache' => true,
            ],
            'escapeMarkup' => new JsExpression('function (markup) { return markup; }'),
            'templateSelection' => new JsExpression('id_user'),]
        ],
    ],

控制器.php

public function actionUserList($q = null, $id = null) {
    Yii::$app->response->format = Response::FORMAT_JSON;
    $out = ['results' => ['id' => '', 'text' => '']];
    if (!is_null($q)) {
        $query = new Query;
        $query->select('id, name AS text')->from('tbl_user')->where(['like', 'name', $q])->limit(10);
        $command = $query->createCommand();
        $data = $command->queryAll();
        $out['results'] = array_values($data);
    } elseif ($id > 0) {
        $out['results'] = ['id' => $id, 'text' => User::find($id)->name];
    }
    return $out;
}

【问题讨论】:

  • 您需要为将数据提供给select2 的操作user-list 添加代码。
  • @MuhammadOmerAslam 完成。此方法与其他列类似,但调用对应的模型。

标签: javascript yii2 jquery-select2 kartik-v


【解决方案1】:

我知道答案很晚,但我自己遇到了这样的问题。也许该解决方案对某人有用。

为了显示值而不是标识符,您需要使用initValueText

首先,我们需要从选定的标识符中获取用户。

$filteredUserId = ArrayHelper::getValue(Yii::$app->getRequest()->getQueryParam('UserSearch'), 'user_id');
$filteredUser = $filteredUserId ? User::findOne($filteredUserId) : null;

由于某种原因,我的过滤器格式略有不同,可能是版本不同,或者两个选项都有效。我将从我的代码中举一个例子:

[
    'attribute' => 'user_id',                                
    'filterType' => GridView::FILTER_SELECT2,
    'filterWidgetOptions' => [                                   
        'initValueText' => $filteredUser ? $filteredUser->getName() : null,
        'pluginOptions' => [
            'allowClear' => true,
            'closeOnSelect' => true,
            'minimumInputLength' => 1,
            'ajax' => [
                'url' => Url::to(['user/user-list']),
                'dataType' => 'json',
            ],
        ],
    ],
    'filterInputOptions' => ['placeholder' => '...'],
    'value' => function ($model) {
        return $model->user() ? $model->user()->getName() : null;
    },
],

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-09-01
    • 2020-04-14
    • 1970-01-01
    • 2021-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-09
    相关资源
    最近更新 更多