Yii2相关下拉列表不工作

Yii2相关下拉列表不工作,yii2,Yii2,我有一个依赖于项目域的客户域。 在我的表单中,我有一个项目下拉列表,我需要第二个客户下拉列表根据项目动态更改。 我在网上的一些地方找到了解决方案,但阵列没有改变。 有人能帮忙吗 我的表格: $dataProject=ArrayHelper::map(Project::find()->asArray()->all(), 'id', 'name'); echo $form->field($model, 'project_id')->dropDownList($dataPr

我有一个依赖于项目域的客户域。
在我的表单中,我有一个项目下拉列表,我需要第二个客户下拉列表根据项目动态更改。
我在网上的一些地方找到了解决方案,但阵列没有改变。
有人能帮忙吗

我的表格:

$dataProject=ArrayHelper::map(Project::find()->asArray()->all(), 'id', 'name');
  echo $form->field($model, 'project_id')->dropDownList($dataProject,
       ['prompt'=>'-Choose a Project-',
       'onchange'=>'
            $.post( "'.Yii::$app->urlManager->createUrl('customer/lists?id=').'"+$(this).val(), function( data ) {
            $( "select#title" ).html( data );
       });
  ']);
  $dataPost=ArrayHelper::map(Customer::find()->asArray()->all(), 'id', 'first_name');
  echo $form->field($model, 'customer_id')
       ->dropDownList(
            $dataPost,
            ['id'=>'title']
       );
客户控制器中的代码:

public function actionLists($id) {
 $countPosts = Customer::find()
      ->where(['project_id' => $id])
      ->count();
 $posts = Customer::find()
      ->where(['project_id' => $id])
      ->orderBy('id DESC')
      ->all();
 if($countPosts>0) {
      foreach($posts as $post){
           echo "<option value='".$post->id."'>".$post->first_name."</option>";
      }
 }
 else{
      echo "<option>-</option>";
 }
}
公共功能操作列表($id){
$countPosts=Customer::find()
->其中(['project_id'=>$id])
->计数();
$posts=客户::查找()
->其中(['project_id'=>$id])
->orderBy('id DESC')
->全部();
如果($countPosts>0){
foreach($posts作为$post){
回显“$post->first_name.”;
}
}
否则{
回声“-”;
}
}

到目前为止,当您有权访问jquery时,将下拉选项添加到select的最佳方法是使用
。each()
,但您需要从
控制器/action
json
的形式提供选项,而不是创建html然后将html添加到下拉列表中

然后使用
$.post
并为
id
添加带有
url
的查询字符串,而您可以使用
data
选项发送
id

onchange
函数更改为以下内容

'onchange'=>'
     $.post( "'.Yii::$app->urlManager->createUrl('/customer/lists').'", {id:$(this).val()},function( data ) {

         //this will clear the dropdown of the previous options

         $("#title").children("option").remove();

         //optionally you can use the following if you have a placeholder in the dropdown so that the first option is not removed
         //$("#title").children("option:not(:first)").remove();

         $.each(data, function(key, value) {

         $("#title")
             .append($("<option></option>")
             .attr("value",key)
             .text(value)); 
         });
});
您只需查询客户,然后在结果集
$posts
上使用
php:count()
函数来计算记录总数

  $posts = Customer::find()
          ->where(['project_id' => $id])
          ->orderBy('id DESC')
          ->all();
  $countPosts = count($post);
但是我们不需要
计数
无论如何,这只是为了提供信息,请将您的操作
actionlist()
更改为下方,并删除参数
$id
,因为我们现在正在使用
post
发送id

public function actionLists() {
    //set the response format to JSON
    Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;

    //get the id 
    $id = Yii::$app->request->post ( 'id' );

    $posts = Customer::find ()
            ->where ( [ 'project_id' => $id ] )
            ->orderBy ( 'id DESC' )
            ->all ();

    return ArrayHelper::map ( $posts , 'id' , 'first_name' );
}

除了完成上述所有工作之外,您还应该习惯于以广泛可用的扩展或插件的形式使用可用资源,其中之一就是这样做,编写javascript和仅从服务器端提供数据的痛苦大大减少。

Wow,感谢您的详细回复。但是,它仍然不起作用。我输入了你提供的代码。客户下拉列表不会根据所选项目而更改。您可以通过添加您更新的代码来更新您的问题吗?哦,我发现了问题,我在控制器中缺少“use yii\helpers\ArrayHelper”。现在很好,非常感谢穆罕默德!!
public function actionLists() {
    //set the response format to JSON
    Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;

    //get the id 
    $id = Yii::$app->request->post ( 'id' );

    $posts = Customer::find ()
            ->where ( [ 'project_id' => $id ] )
            ->orderBy ( 'id DESC' )
            ->all ();

    return ArrayHelper::map ( $posts , 'id' , 'first_name' );
}