首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Yii2 Dependent下拉列表不工作

Yii2 Dependent下拉列表不工作
EN

Stack Overflow用户
提问于 2018-05-28 22:39:30
回答 1查看 709关注 0票数 1

我有一个客户字段,它依赖于一个项目字段。

在我的表单中,我有一个项目的下拉列表,我需要我的第二个客户下拉列表根据项目动态变化。

我在web上的一些地方找到了解决方案,但阵列并没有改变。

有人能帮上忙吗?

我的表单:

代码语言:javascript
复制
$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']
       );

客户控制器中的代码:

代码语言:javascript
复制
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>";
 }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-05-29 03:49:17

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

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

将您的onchange函数更改为以下内容

代码语言:javascript
复制
'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)); 
         });
});

然后,您将对Customer表查询两次,一次是对所有记录进行计数,一次是对所有客户列表进行查询

代码语言:javascript
复制
$countPosts = Customer::find()
      ->where(['project_id' => $id])
      ->count();
$posts = Customer::find()
      ->where(['project_id' => $id])
      ->orderBy('id DESC')
      ->all();

您可以简单地查询客户,并在结果集$posts上使用php:count()函数来计算记录总数。

代码语言:javascript
复制
  $posts = Customer::find()
          ->where(['project_id' => $id])
          ->orderBy('id DESC')
          ->all();
  $countPosts = count($post);

但我们无论如何都不需要count这只是为了提供信息,将您的操作actionLists()更改为下面并删除参数$id,因为我们正在发送带有post的id。

代码语言:javascript
复制
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和从服务器端提供数据的痛苦更少。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50568761

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档