2016-03-20 228 views
1

我想爲用戶創建一個過濾器進行搜索。我創建這個相關的下拉列表,但它不起作用。我有'城市'和'國家'表。它不工作。 錯誤在conslode POST http://localhost/smarthouse/web/index.php?r=post/lists?id=1 404 (Not Found)Yii2:依賴下拉列表搜索

有人能告訴我我哪裏出錯了嗎? 謝謝。

我的形式

<?php 
$dataCity=ArrayHelper::map(\app\models\Cities::find()-> 
asArray()->all(),'id', 'name');  
       $form = ActiveForm::begin(); 
      echo $form->field($searchModel, 'id')->dropDownList($dataCity, 
           [''=>'-Choose a Name-', 
            'class'=>'adjust', 
         'onchange'=>' 
     $.post("index.php?r=post/lists?id='. 
     '"+$(this).val(),function(data) 
       { 
          $("select#post").html(data); 
         }); 
        ']); 

      $dataState=ArrayHelper::map(\app\models\States::find()-> 
      asArray()->all(), 'id', 'name'); 
      echo $form->field($searchModel, 'id') 
       ->dropDownList(
        $dataState, 
        ['id'=>'name', 
         'class'=>'adjust' 
         ] 
       ); 
      ActiveForm::end(); 
      ?> 

我在後期控制器

public function actionLists($id) 
    { 
    $countPosts = States::find() 
    ->where(['city_id' => $id]) 
    ->count(); 

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

    if($countPosts>0){ 
    foreach($posts as $post){ 

    echo "<option value='".$post->id."'>".$post->name."</option>"; 
    } 
    } 
    else{ 
    echo "<option>-</option>"; 
    } 

回答

1

list動作在你的onchange $。員額路徑第二GET參數應與&,而不是開始?這樣`指數。 php?r = post/lists & id

'onchange'=>' 
    $.post("index.php?r=post/lists&id='. // use & not ? 
    '"+$(this).val(),function(data) 
      { 
         $("select#post").html(data); 
        }); 
       ' 
+0

謝謝: ) 有用 – Starite