这里使用kartik depdrop yii2扩展尝试Dependent下拉列表。这个依赖下拉列表的过程是,如果我选择一个productname,它会显示依赖batchno,然后如果选择一个batchno,它将显示依赖itemid。
实际上第一级是完美的,如果我选择了一个产品名称,它会告诉我batchno,这个动作是完美的,但问题在二级。如果我选择一个batchno它需要向我显示一个itemid,这个动作不起作用
控制器
public function actionSubcat() {
$out = [];
if (isset($_POST['depdrop_parents'])) {
$parents = $_POST['depdrop_parents'];
if ($parents != null) {
$cat_id = $parents[0];
$out = Productbatch::getBatchNo($cat_id);
echo Json::encode($out);
// 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 = Productbatch::getItemid($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($out);
//echo Json::encode(['output'=>$out, 'selected'=>$data['selected']]);
return;
}
}
echo Json::encode(['output'=>'', 'selected'=>'']);
}
_form
<?= $form->field($model, 'productname')->widget(Select2::classname(), [
'data' => ArrayHelper::map(Productnames::find()->all(),'productnames_productname','productnames_productname'),
'language' => 'en',
'options' => ['placeholder' => 'Select Product Name', 'id' => 'cat-id'],
'pluginOptions' => [
'allowClear' => true
],
]); ?>
<?= $form->field($model, 'batchno')->widget(DepDrop::classname(), [
'options'=>['id'=>'subcat-id'],
'pluginOptions'=>[
'depends'=>['cat-id'],
'placeholder'=>'Select BatchNo',
'url'=>Url::to(['/production/productbatch/subcat'])
]
]); ?>
<?= $form->field($model, 'itemid')->widget(DepDrop::classname(), [
'pluginOptions'=>[
'depends'=>['cat-id', 'subcat-id'],
'placeholder'=>'Select ItemId',
'url'=>Url::to(['/production/productbatch/prod'])
]
]); ?>
模型
public static function getBatchNo($cat_id)
{
$out = [];
$data = Productbatch::find()
->where(['productname' => $cat_id])
->asArray()
->all();
foreach ($data as $dat) {
$out[] = ['id' => $dat['itemid'], 'name' => $dat['batchno']];
}
return $output = [
'output' => $out,
'selected' => ''
];
}
public static function getItemid($cat_id, $subcat_id)
{
$out = [];
$data = Productbatch::find()
->where(['productname' => $cat_id])
->andWhere(['batchno' => $subcat_id])
->asArray()
->all();
$selected = '';
foreach ($data as $dat => $datas) {
$out[] = ['id' => $datas['itemid'], 'name' => $datas['itemid']];
if($dat == 0){
$aux = $datas['itemid'];
}
($datas['productname'] == $cat_id) ? $selected = $cat_id : $selected = $aux;
}
return $output = [
'output' => $out,
'selected' => $selected
];
}
答案 0 :(得分:1)
您需要在控制器中执行此操作。您必须在控制器中创建新操作。
public function actionState() {
$country_id = $_POST['depdrop_parents'][0];
$state = State::find()->where(['country_id' => $country_id])->all();
$all_state = array();
$i = 0;
foreach ($state as $value) {
$all_state[$i]['id'] = $value['state_id'];
$all_state[$i]['name'] = $value['state_name'];
$i++;
}
echo Json::encode(['output' => $all_state, 'selected' => '']);
return;
}
我也遇到了这个问题,但最后我解决了这个问题..