2017-08-06 14:55:03 1604次浏览 1条回答 0 悬赏 10 金钱

use kartik\select2\Select2;
用这个插件的做下拉
这是view
<?php $form = ActiveForm::begin(); ?>
<?php $form->field($model, 'id')->hiddenInput(['value'=>''])->label(false); ?>

<div class="col-md-3">
    <?= $form->field($model, 'supplier_code')->widget(Select2::classname(), [
        'options' => ['placeholder' => '请输入供应商 ...'],
        'pluginOptions' => [

            'language' => [
                'errorLoading' => new JsExpression("function () { return 'Waiting...'; }"),
            ],
            '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(res) { return res.text; }'),
           'templateSelection' => new JsExpression('function (res) { return res.text; }'),
        ],
    ])->label('供应商');
    ?>
</div>

<div class="form-group col-md-3" style="margin-top: 24px;">
    <?= Html::submitButton('提交', ['class' => 'btn btn-primary']) ?>
    <?= Html::resetButton('重置', ['class' => 'btn btn-default']) ?>
</div>


<?php ActiveForm::end(); ?>
控制器部分
public function actionSearchSupplier($q = null, $id = null)
{
    \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
    $out = ['results' => ['id' => '', 'text' => '']];
    if (!is_null($q)) {

        $data = Supplier::find()
            ->select('supplier_code as id,supplier_name AS text')
            ->andFilterWhere(['like', 'supplier_name', $q])
            ->distinct()
            ->limit(50)
            ->asArray()
            ->all();

        $out['results'] = array_values($data);
    }elseif ($id > 0) {
            $out['results'] = ['id' => $id, 'text' => Supplier::find($id)->supplier_name];
    }
    return $out;
}

发出来所诸君共解之,有遇到请晓于大家,谢谢
QQ截图20170806145328.png

补充于 2017-08-06 14:55

QQ截图20170806145515.png

您需要登录后才可以回答。登录 | 立即注册
风
副董事长

深圳

注册时间:2015-03-28
最后登录:2023-10-07
在线时长:273小时23分
  • 粉丝30
  • 金钱17698
  • 威望70
  • 积分21128

热门问题