原理:
通過下拉列表onchange事件,向服務(wù)器獲取另一個下拉列表的內(nèi)容(在控制器中填充actionMyproduct內(nèi)容)充坑。代碼中黑色加粗的地方是空易錯的地方减江。
視圖文件
<?php
use yii\widgets\ActiveForm;
use common\helpers\Url;
use yii\helpers\Html;
use common\enums\WorkOrderStatusEnum;
use common\helpers\ArrayHelper;
$form = ActiveForm::begin([
? ? 'id' => $model->formName(),
? ? 'enableAjaxValidation' => true,
? ? 'validationUrl' => Url::to(['ajax-edit', 'id' => $model['id']),
]);
?>
? ? <div class="modal-header">
? ? ? ? <button type="button" class="close" data-dismiss="modal" aria-label="Close">
? ? ? ? ? ? <span aria-hidden="true">×</span></button>
? ? ? ? <h4 class="modal-title">新工單</h4>
? ? </div>
? ? <div class="modal-body">
? ? ? ? <div class="row">
<div class="col-sm-4">
? ? ? ? <?= $form->field($model, 'member_id')->dropDownList(ArrayHelper::map($customer,'id', 'realname'),
? ? ? ? ['prompt'=>'-選擇客戶-',
? 'onchange'=>'
$.get( "'.Url::toRoute('/member/order/myproduct').'", { id: $(this).val() } )
? ? ? ? ? ? ? ? .done(function( data ) {
? ? ? ? ? ? ? ? ? ? $( "#'.Html::getInputId($model, 'product_id').'" ).html( data );
? ? ? ? ? ? ? ? });
']);
? ? ? ? ? ? ?>
? ? ? ? <?= $form->field($model, 'product_id')->dropDownList(ArrayHelper::map($myproduct,'id', 'categary_name'),
? ? ? ? ['prompt'=>'-選擇已購產(chǎn)品-']) ?>
</div>
? ? </div>
? ? <div class="modal-footer">
? ? ? ? <button type="button" class="btn btn-white" data-dismiss="modal">關(guān)閉</button>
? ? ? ? <button class="btn btn-primary" type="submit">保存</button>
? ? </div>
<?php ActiveForm::end(); ?>
視圖說明:$customer,$myproduct捻爷,是控制器給的變量辈灼。
控制器
? ? // 已購產(chǎn)品列表
? ? public function actionMyproduct($id)
? ? {
? ? ? ? $myproducts = Myproduct::find()
? ? ? ? ? ? ->where(['member_id' => $id])
? ? ? ? ? ? ->asArray()
? ? ? ? ? ? ->all();
? ? ? ? if(!$myproducts){
? ? ? ? ? ? echo "<option value='" . 0 . "'>"? . "</option>";
? ? ? ? }
? ? ? ? foreach ($myproducts as $myproduct) {
? ? ? ? ? ? echo "<option value='" . $myproduct['id'] . "'>" . $myproduct['categary_name'] . "</option>";
? ? ? ? }
? ? }
完成效果
[網(wǎng)上大多是某個人的內(nèi)容,缺少代碼也榄,轉(zhuǎn)載且不完整巡莹。如果可用,請點個zan]