下拉插件问题 [ 2.0 版本 ]
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;
}
发出来所诸君共解之,有遇到请晓于大家,谢谢
风 补充于 2017-08-06 14:55
风 深圳
注册时间:2015-03-28
最后登录:2025-04-14
在线时长:276小时40分
最后登录:2025-04-14
在线时长:276小时40分
- 粉丝30
- 金钱18268
- 威望70
- 积分21728