2016-11-27 5 views
0

、これはコードです:

/* 
* 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は未定義の変数であることを述べています変数は? 私はモデル/コントローラcat、sub-catとprodを持っていると仮定し、これらは関連しています。

誰かが正しい方法で私を導くことができれば、本当に役に立ちます! おかげ

答えて

0

Tipicallyは、あなたがこの$のcatlistがあるドロップダウンリストで、このため

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

または

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

Oohのおかげ多くのことを、例の1つの以上の質問を表示する値のリストです自動的に生成されるか、最初に$ catList = Category :: find() - > all();を書く必要があります。働くために? – Rugleh

+0

私の答えでは、あなたはあなた自身の$ catListを作成しなければならないということを意味します。あなたはfill $ catListの値をどこから得るべきかを知っていなければなりません。(配列を持っていますか?... id、cat.id ?... – scaisEdge

関連する問題