2016-11-27 41 views
0

我覺得我失蹤的depdrop插件的場景1的這個例子的東西,這是代碼:

/* 
* SCENARIO 1: A 3-level nested dependency example 
*/ 
// THE VIEW 
use kartik\widgets\DepDrop; 

// Parent 
echo $form->field($model, 'cat')->dropDownList($catList, ['id'=>'cat-id']); 

// Child # 1 
echo $form->field($model, 'subcat')->widget(DepDrop::classname(), [ 
    'options'=>['id'=>'subcat-id'], 
    'pluginOptions'=>[ 
     'depends'=>['cat-id'], 
     'placeholder'=>'Select...', 
     'url'=>Url::to(['/site/subcat']) 
    ] 
]); 

// Child # 2 
echo $form->field($model, 'prod')->widget(DepDrop::classname(), [ 
    'pluginOptions'=>[ 
     'depends'=>['cat-id', 'subcat-id'], 
     'placeholder'=>'Select...', 
     'url'=>Url::to(['/site/prod']) 
    ] 
]); 

// THE CONTROLLER 
public function actionSubcat() { 
    $out = []; 
    if (isset($_POST['depdrop_parents'])) { 
     $parents = $_POST['depdrop_parents']; 
     if ($parents != null) { 
      $cat_id = $parents[0]; 
      $out = self::getSubCatList($cat_id); 
      // the getSubCatList function will query the database based on the 
      // cat_id and return an array like below: 
      // [ 
      // ['id'=>'<sub-cat-id-1>', 'name'=>'<sub-cat-name1>'], 
      // ['id'=>'<sub-cat_id_2>', 'name'=>'<sub-cat-name2>'] 
      // ] 
      echo Json::encode(['output'=>$out, 'selected'=>'']); 
      return; 
     } 
    } 
    echo Json::encode(['output'=>'', 'selected'=>'']); 
} 

public function actionProd() { 
    $out = []; 
    if (isset($_POST['depdrop_parents'])) { 
     $ids = $_POST['depdrop_parents']; 
     $cat_id = empty($ids[0]) ? null : $ids[0]; 
     $subcat_id = empty($ids[1]) ? null : $ids[1]; 
     if ($cat_id != null) { 
      $data = self::getProdList($cat_id, $subcat_id); 
      /** 
      * the getProdList function will query the database based on the 
      * cat_id and sub_cat_id and return an array like below: 
      * [ 
      *  'out'=>[ 
      *   ['id'=>'<prod-id-1>', 'name'=>'<prod-name1>'], 
      *   ['id'=>'<prod_id_2>', 'name'=>'<prod-name2>'] 
      *  ], 
      *  'selected'=>'<prod-id-1>' 
      * ] 
      */ 

      echo Json::encode(['output'=>$data['out'], 'selected'=>$data['selected']]); 
      return; 
     } 
    } 
    echo Json::encode(['output'=>'', 'selected'=>'']); 
} 

每當我嘗試這一點,說,$ catList是未定義的變量,如該變量來自? 假設我有一個模型/控制器的貓,子貓和prod,這些都是相關的。

如果有人能以正確的方式指導我,那將非常有幫助! 感謝

回答

0

Tipically是要在下拉列表中顯示

$catList=Category::find()->all(); 

$catList = ['a' => 'Item A', 'b' => 'Item B', 'c' => 'Item C']; 
+0

哦感謝很多關於這一點,在例如一個問題這$ catlist是價值列表自動生成或我需要先寫入$ catList = Category :: find() - > all();爲了工作? – Rugleh

+0

在我的答案我的意思是,你必須創建自己的$ catList ..你應該知道從哪裏獲得填充$ catList的值..(你有一個數組?...你有一個ID爲id的表,cat.id ?... – scaisEdge