Html :: dropDownList取决于Yii2

时间:2018-04-20 09:54:39

标签: javascript php html yii2

连接两个选择时遇到问题。我想选择一个选择过滤器来搜索第二个select语句。从selectDB中选择获取数据但不会相互影响。有人能帮助我吗?

我的第一批精选客户从数据库表客户那里获取数据:

<?=
    Html::dropDownList('userlist', [],  
    ArrayHelper::map(Customers::find()->where('id',['company_id' => 
    'name'])->orderBy('name')->all(), 'id', 'name'), 
    ['prompt' => 'Select a User ...', 
    'class' => 'form-control', 
    'style' => 'width: 100%;']);
    ?>

我的第二个选择提供从数据库表格中获取数据:

<?=
   Html::dropDownList('offerslist', [],  
   ArrayHelper::map(Offers::find()->where('customer_id',['company_id' 
   => 'offers_n'])->orderBy('customer_id')->all(), 
   'id','offers_n','customer_id'), 
   ['prompt' => 'Select a Offert ...',
   'onchange' => 'change_user_list(this.value)', 
   'class' => 'form-control', 
   'style' => 'width: 100%;']);
   ?>

这是我的函数js:

 function change_user_list(company_id)
   {
   $.ajax({
   url: "<?= \yii\helpers\Url::to(['offers/userlist'])?>",
   type: 'get',
   data: {
   company_id: company_id
   },
   success: function (data) {
   document.getElementsByName('userlist')[0].innerHTML = data;
   }
   });
   }

这是我在控制器中的actionFucntion:

public function actionUserlist($company_id)
   {
   if (Yii::$app->request->isAjax)  {      
   $userlist = ArrayHelper::map(Customers::find()- 
   >where(['company_id' => $company_id])->orderBy('name')->all(), 
   'id', 'name');
   $string = "<option value>Select a Users</option>";
   foreach($userlist as $id => $name) {
   $string .=   "<option value=".$id.">".$name."</option>";
   }
    return $string;
   }
   }

0 个答案:

没有答案