2016-11-07 15:21:43 3543次浏览 2条回答 1 悬赏 25 金钱

Ajax搜索这边出了点问题.希望大神帮助一下

echo Select2::widget([
                        'name' => 'putaway_time',
                        'data' => $data,
                        'options' => ['multiple' => false, 'placeholder' => '请选择...'],
                        'pluginOptions' => [
                            'allowClear' => true,
                            'language' => [
                                'errorLoading' => new JsExpression("function () { return 'Waiting...'; }"),
                            ],
                            'ajax' => [
                                'url' => Url::to(['book/search']),
                                'dataType' => 'json',
                                'data' => new JsExpression('function(params) { return {putaway_time:params.term}; }')
                            ],
                            'escapeMarkup' => new JsExpression('function (markup) { return markup; }'),
                            'templateResult' => new JsExpression('function(res) { return res.text; }'),
                            'templateSelection' => new JsExpression('function (res) { return res.text; }'),
                        ],
                    ]);

Controller

public function actionSearch($putaway_time = null)
    {

        \Yii::$app->response->format = Response::FORMAT_JSON;
        $out = ['results' => ['book_id' => '', 'putaway_time' => '']];
        if (!is_null($putaway_time)) {
            $data = EditorPickContent::find()
                ->select('book_id, putaway_time as text')
                ->andFilterWhere(['like', 'putaway_time', $putaway_time])
                ->limit(15)
                ->asArray()
                ->all();
        }
        $out['results'] = array_values($data);

        return $out;
    }
  • 回答于 2016-11-09 21:10 举报

    select内容为固定格式 id text 不能用book_id

    1 条回复
    回复于 2016-11-10 15:52 回复

    我感觉应该不是这里的问题,现在就是能搜索,但是没有候选选项。而且搜索出来的选项还不能点击

  • 回答于 2017-03-10 17:17 举报

    改成ajax后默认的data怎么不显示了呢?

您需要登录后才可以回答。登录 | 立即注册
飒雅
助理

飒雅 北京

注册时间:2016-06-12
最后登录:2019-02-25
在线时长:11小时38分
  • 粉丝6
  • 金钱5
  • 威望0
  • 积分115

热门问题