Reputation: 3864
I am using the Kartik Depdrop widget.
Everything is working fine except in the situation where I have not selected a value on creation of new record, on update the dependent field should show Please select
whereas it is showing the first value in the drop-down and it is getting saved on update, whereas I want the value of 'please select' i.e. null to be saved even on update depending on the situation.
Example- I select a room-category - dependant value is packages, which can be null as well. So I didn't select any value in the dependent field packages. But when I come to update the form the first value in the drop-down is showing by default, whereas I want the please select
as the default.
How can correct this?
$form->field($model, 'package')->widget(DepDrop::classname(), [
'data'=>ArrayHelper::map(\app\models\Package::find()->all(), 'id', 'package_name' ),
'pluginOptions'=>[
'depends'=>['room_category'],
'placeholder'=>'Select...',
'url'=> \yii\helpers\Url::to(['patient-detail/subcat']),
]
])
Note:If I am selecting a value in the dependant dropdown on creation, then the value on update is showing correctly.
Upvotes: 7
Views: 9493
Reputation: 1
VIEW
<?= $form->field($model, 'client_id')->widget(Select2::class, [
'data' => $data,
'value' => $model->isNewRecord && empty($model->client) ? 'none' : $model->client,
'options' => ['placeholder' => 'Select a client.', 'id' => 'select-client-id'],
'pluginOptions' => [
'allowClear' => false,
],
]); ?>
<?= Html::hiddenInput('selected_goal_id', $model->isNewRecord ? '' : $model->goal_id, ['id' => 'selected_goal_id']); ?>
<?= Html::hiddenInput('selected_objective_id', $model->isNewRecord ? '' : $model->objective_id, ['id' => 'selected_objective_id']); ?>
<?= $form->field($model, 'goal_id')->widget(DepDrop::class, [
'options' => ['id' => 'select-goal-id'],
'type' => DepDrop::TYPE_SELECT2,
'pluginOptions' => [
'depends' => ['select-client-id'],
'initialize' => !$model->isNewRecord,
'initDepends' => ['select-client-id'],
'params' => ['selected_goal_id'],
'placeholder' => 'Select...',
'url' => Url::to(['/individual-note/goals']),
]
]) ?>
<?= $form->field($model, 'objective_id')->widget(DepDrop::class, [
'type' => DepDrop::TYPE_SELECT2,
'pluginOptions' => [
'depends' => ['select-goal-id'], // 'select-client-id',
'initialize' => !$model->isNewRecord,
'initDepends' => ['select-goal-id'],
'params' => ['selected_objective_id'],
'placeholder' => 'Select...',
'url' => Url::to(['/individual-note/objectives'])
]
]) ?>
CONTROLLER
public function actionGoals(): array {
Yii::$app->response->format = Response::FORMAT_JSON;
$output = [];
$selected = '';
if (isset($_POST['depdrop_parents'][0]) && $_POST['depdrop_parents'][0]) {
$client_id = $_POST['depdrop_parents'][0];
$goals = Goal::find()->where(['client_id' => $client_id])->andWhere(['!=', 'id', ''])->all();
foreach ($goals as $goal) {
/** @var Goal $goal */
if (count($goal->objectives) == 0) {
continue;
}
$output[] = [
'id' => $goal->id,
'name' => $goal->name,
];
}
$selected = !empty($output) ? $output[0]['id'] : '';
if (!empty($_POST['depdrop_all_params']['selected_goal_id'])) {
$selected_goal_id_aux = $_POST['depdrop_all_params']['selected_goal_id'];
foreach ($output as $o) {
if ($o['id'] == $selected_goal_id_aux) {
$selected = $selected_goal_id_aux;
break;
}
}
}
}
return ['output' => $output, 'selected' => $selected];
}
public function actionObjectives(): array {
Yii::$app->response->format = Response::FORMAT_JSON;
$output = [];
$selected = '';
if (isset($_POST['depdrop_parents']) && $_POST['depdrop_parents'] != null) {
$goal_id = $_POST['depdrop_parents'][0];
if (!empty($goal_id)) {
$objectives = Objective::find()->where(['goal_id' => $goal_id])->all();
foreach ($objectives as $objective) {
/** @var Objective $objective */
$output[] = [
'id' => $objective->id,
'name' => $objective->active_until . ' - ' . $objective->description,
];
}
$selected = !empty($output) ? $output[0]['id'] : '';
if (!empty($_POST['depdrop_all_params']['selected_objective_id'])) {
$selected_objective_id_aux = $_POST['depdrop_all_params']['selected_objective_id'];
foreach ($output as $o) {
if ($o['id'] == $selected_objective_id_aux) {
$selected = $selected_objective_id_aux;
break;
}
}
}
}
}
return ['output' => $output, 'selected' => $selected];
}
Upvotes: -1
Reputation: 61
It works well for me.
View file
<?php echo $form->field($model, 'area_parent_id')->dropDownList(AreaCode::getProvinceOption(), ['prompt' => 'select...', 'id' => 'parent_id']); ?>
<?php echo Html::hiddenInput('selected_id', $model->isNewRecord ? '' : $model->area_id, ['id'=>'selected_id']); ?>
<?php echo $form->field($model, 'area_id')->widget(\kartik\depdrop\DepDrop::classname(), [
'options' => ['id' => 'area_id', 'class' => '', 'style' => ''],
'pluginOptions' => [
'depends' => ['parent_id'],
'placeholder' => 'select...',
'initialize' => $model->isNewRecord ? false : true,
'url' => Url::to(['/area-code/subcat']),
'params'=> ['selected_id'],
],
]); ?>
Controller file
public function actionSubcat()
{
$out = [];
if (isset($_POST['depdrop_all_params'])) {
$parent_id = $_POST['depdrop_all_params']['parent_id'];
$selected_id = $_POST['depdrop_all_params']['selected_id'];
$out = AreaCode::find()->select(['id', 'name'])->where(['parent_id' => $parent_id])->asArray()->all();
return \yii\helpers\Json::encode(['output' => $out, 'selected' => $selected_id]);
}
return \yii\helpers\Json::encode(['output' => '', 'selected' => '']);
}
Upvotes: 6
Reputation: 11
Okay this is the solution you want to do:
// THE VIEW
use kartik\widgets\DepDrop;
// Top most parent
echo $form->field($account, 'lev0')->widget(Select2::classname(), [
'data' => ArrayHelper::map(Account::find()->where('parent IS NULL')->asArray()->all(), 'id', 'name')
]);
//ADDITIONAL PARAM ID YOU MAY USE TO SELECT A DEFAULT VALUE OF YOUR MODEL IN YOUR DEPDROP WHEN YOU WANT TO UPDATE:
echo Html::hiddenInput('model_id1', $model->id, ['id'=>'model_id1' ]);
// Child level 1
echo $form->field($account, 'lev1')->widget(DepDrop::classname(), [
'data'=> [6 =>'Bank'],
'options' => ['placeholder' => 'Select ...'],
'type' => DepDrop::TYPE_SELECT2,
'select2Options'=>['pluginOptions'=>['allowClear'=>true]],
'pluginOptions'=>[
'depends'=>['account-lev0'],
'url' => Url::to(['/account/child-account']),
'loadingText' => 'Loading child level 1 ...',
'params'=>['model_id1'] ///SPECIFYING THE PARAM
]
]);
// CONTROLLER
public function actionChildAccount() {
$out = [];
if (isset($_POST['depdrop_parents'])) {
$id = end($_POST['depdrop_parents']);
$list = Account::find()->andWhere(['parent'=>$id])->asArray()->all();
$selected = null;
if ($id != null && count($list) > 0) {
//EXACTLY THIS IS THE PART YOU NEED TO IMPLEMENT:
$selected = '';
if (!empty($_POST['depdrop_params'])) {
$params = $_POST['depdrop_params'];
$id1 = $params[0]; // get the value of model_id1
foreach ($list as $i => $account) {
$out[] = ['id' => $account['id'], 'name' => $account['name']];
if ($i == 0){
$aux = $account['id'];
}
($account['id'] == $id1) ? $selected = $id1 : $selected = $aux;
}
}
// Shows how you can preselect a value
echo Json::encode(['output' => $out, 'selected'=>$selected]);
return;
}
}
echo Json::encode(['output' => '', 'selected'=>'']);
}
Ensure this process to be executed when the page is completely loaded.
Upvotes: 1