【问题标题】:Yii2 : kartik Select2Yii2:kartik Select2
【发布时间】:2016-12-30 12:20:49
【问题描述】:

我有 2 个 select2 下拉列表,第二个取决于第一个的数据

第一个代码:

<?= $form->field($model, 'patient_id')->widget(select2::className(),[
   'data'=>  arrayhelper::map(patient::find()->all(),'patient_id','patient_name'),
   'options'=>['placeholder'=>'select patient Name ... '],
   'pluginOptions'=>[
            'allowClear'=>true
   ],
])?>

第二个代码:

<?= $form->field($model, 'doctor_id')->widget(select2::className(),[
   'data'=>  arrayhelper::map(doctors::find()->all(),'doctor_id','doctor_name'),
   'options'=>['placeholder'=>'أختر اسم الطبيب '],
   'pluginOptions'=>[
            'allowClear'=>true
   ],
])?>

我知道第二个中的sql代码是:

从医生中选择医生姓名

所以我需要它:

从医生 ID 所在的医生中选择 DISTINCT 医生名称(从病人服务中选择医生 ID WHERE patient_id="THE VALUE FROM THE 1st DROPDOWNLIST")

在常规下拉列表中,它的工作方式与 Yii2 Lesson - 20 Dependent Drop Down Lists By DoingITeasyChannel 类似,但在 select2 中我没有找到方法。

-------------------更新后----
作为 cmets 有 DepDrop 但我很困惑如何使用它。

我变了

<?= $form->field($model, 'patient_id')->widget(Select2::classname(), [ 'data' => ArrayHelper::map(patient::find()->asArray()->all(), 'patient_id', 'patient_name')]); ?>

另一个是:

<?= $form->field($model, 'doctor_id')->widget(DepDrop::classname(), [ 'options' => ['placeholder' => 'Select ...'], 'type' => DepDrop::TYPE_SELECT2, 'select2Options'=>['pluginOptions'=>['allowClear'=>true]], 'pluginOptions'=>[ 'depends'=>['receipts-doctor_id'], // here i got confused 'url' => Url::to(['/receipts/child']), 'loadingText' => 'Loading child level 1 ...', ] ]); ?>

在控制器中

public function actionChild() { $out = []; if (isset($_POST['depdrop_parents'])) { // what they meaning by depdrop_parents or what i should change it ?
$id = end($_POST['depdrop_parents']); $list = Account::find()->andWhere(['parent'=>$id])->asArray()->all(); $selected = null; if ($id != null && count($list) > 0) { $selected = ''; foreach ($list as $i => $account) { $out[] = ['id' => $account['id'], 'name' => $account['name']]; if ($i == 0) { $selected = $account['id']; } } // Shows how you can preselect a value echo Json::encode(['output' => $out, 'selected'=>$selected]); return; } } echo Json::encode(['output' => '', 'selected'=>'']); }

【问题讨论】:

  • 你可以使用Kartik DepDrop
  • 要添加到 @InsaneSkull 建议的内容,您可以使用 DepDrop 插件并仍然使用 Select2,因为 DepDrop 插件支持 Select2 下拉菜单。您必须使用type 选项。
  • @InsaneSkull 谢谢 .. 我以前没见过这个 ,,, 我看到了,我有一些问题 'depends'=&gt;['account-lev0'], 我没明白我的父 ID 是 patient_id还有一些我在控制器代码$list =Account::find()-&gt;andWhere(['parent'=&gt;$id])-&gt;asArray()-&gt;all();中感到困惑
  • @EdvinTenovimas 我对如何使用它感到困惑。我会更新我的问题
  • 好的,我看到你已经写了 Select2 和 Depdrop。它看起来不错(至少一开始是这样)。你被困在哪里了?

标签: php yii2 yii2-advanced-app select2


【解决方案1】:

第一个字段(Select2):

<?= $form->field($model, 'patient_id')->widget(Select2::classname(), [
    'data' => ArrayHelper::map(patient::find()->asArray()->all(), 'patient_id', 'patient_name')]);
?>

第二个字段(DepDrop):

<?= $form->field($model, 'doctor_id')->widget(DepDrop::classname(), [
    'options' => ['placeholder' => 'Select ...'],
    'type' => DepDrop::TYPE_SELECT2,
    'select2Options'=> ['pluginOptions' => ['allowClear' => true]],
    'pluginOptions'=> [
        'depends' => ['receipts-doctor_id'],
        'url' => Url::to(['/receipts/child']),
        'loadingText' => 'Loading child level 1 ...',
    ]
]);
?>

插件选项'depends' =&gt; ['receipts-doctor_id'], 显示必须更改(单击、选择等)哪个元素(获取元素的 ID),以便发送 Ajax 请求并从控制器检索结果。在这种情况下,应该存在 ID 为 receipts-doctor_id 的元素。如果你不知道或者你想为父元素设置 ID,你可以使用'options' =&gt; ['id' =&gt; 'receipts-doctor_id'], 作为你的父元素。

对于控制器,您可以像这样检索值:

public function actionChild()
{
    $depdropParents = Yii::$app->request->post('depdrop_parents');

    if ($depdropParents !== null) {
        $value = $depdropParents[0];
        // Now your $value contains what was being selected in parent element
    }
}

【讨论】:

  • 有些东西不清楚。在我选择第一个下拉列表之后没有任何事情发生......并且第二个 DepDrop 仍然禁用!
  • 如果是这样,则表示控制器(后端代码)存在错误。使用网络(如果您知道如何)查看错误消息。
猜你喜欢
  • 2017-10-16
  • 1970-01-01
  • 2023-04-01
  • 1970-01-01
  • 2016-07-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-30
相关资源
最近更新 更多