将新值添加到下拉列表

时间:2017-03-01 20:57:02

标签: yii2 select2 active-form

在项目/创建活动表单中,我将字段“相关公司帐户”作为下拉列表(select2 by kartik)。在此字段后面,我想添加一个加号或其他内容,以便在下拉列表中添加新帐户,但行为如下:

  • 收集到目前为止完成的所有输入(如$input = compact(array_keys(get_defined_vars()));,但客户端可能需要)
  • 跳转到帐户/创建并传递$ input
  • 提交新帐户后
  • 跳回到项目/创建(例如return $this->redirect(Yii::$app->request->referrer);)并填写先前输入的数据(extract($input, EXTR_PREFIX_SAME, "arr");

我现在正在努力解决几个问题:

  • 这个过程是根据最佳实践还是我应该从根本上改变一些事情?
  • 按钮怎么样?提交按钮,链接或某种形式的JavaScript?
  • 提交按钮的问题是并非所有必填字段都可以填写。因此,可能无法保存和恢复/更新项目模型。
  • 链接问题是它是在输入数据之前构建的
  • javascript的问题在于我没有胶水

欢迎任何提示。提前谢谢。

1 个答案:

答案 0 :(得分:1)

我建议使用Session

对于“添加帐户”按钮,我会使用“提交”按钮,并为实际的“提交”按钮指定不同的名称(表单中有两个提交按钮,如here中所述)。因此,projects / create视图将如下所示:

<?php $form = ActiveForm::begin(); ?>

...
...
...

<?= $form->field($model, 'account_id')->widget(Select2::classname(), [
    'data' => ArrayHelper::map(Account::find()->all(), "id", "name"),
    'options' => ['placeholder' => 'Select a related company account ...'],
    'pluginOptions' => [
        'allowClear' => true
    ],
]) ?>

<?= Html::submitButton('Add Account ;)', ['class' => 'btn btn-success', 'name' => 'add_account_submit']) ?>

...
...
...

<div class="form-group">
    <?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>

<?php ActiveForm::end(); ?>

然后检查ProjectsController,提交用户按下的按钮。如果按下了添加帐户,则保存输入的字段(我将此功能放入模型中以进行清除),否则,保存模型或任何其他内容。并且,在此之前,检查是否设置了关于项目的会话,如果是,则将其预加载到模型(再次,在模型中)。好吧,就像他们说的,一个代码值得千言万语,所以,这就是ProjectsController看起来像:

class ProjectsController extends Controller
{

...
...
...

    public function actionCreate($category)
    {
        $model = new Projects();

        if (Projects::isSavedInSession()) {
            $model->loadFromSession();
        }
        if (Yii::$app->request->post('add_account_submit')) { // if add_account_submit is clicked
            $model->saveTosession(Yii::$app->request->post('Projects')); // I assume your model named Projects, if not, change this value to your model name
            return $this->redirect(['accounts/create']);
        }
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            $model->clearSession(); // we dont need the session anymore
            return $this->redirect(['index');
        }
        return $this->render('create', [
            'model' => $model,
        ]);
    }

...
...
...
}

项目模型将如下所示:

class Projects extends \yii\db\ActiveRecord
{
...
...
...

    public static function isSavedInSession() { // why this is static is beyond this question context
        if (Yii::$app->session->get('projects')) return true;
        return false;
    }

    public function loadFromSession() {
        if (Yii::$app->session->get('projects_name')) $this->name = if (Yii::$app->session->get('projects_name'));
        if (Yii::$app->session->get('projects_account_id')) $this->account_id = if (Yii::$app->session->get('projects_account_id'));
        ...
        ... // insert all model's field here
        ...
    }

    public function saveToSession($fields) {
        Yii::$app->session->set('projects', 1);
        foreach ($fields as $field=>$value) {
            Yii::$app->session->set('projects_' . $field, $value);
        }
    }

    public function clearSession() {
        Yii::$app->session->remove('projects'));
        Yii::$app->session->remove('projects_name'));
        Yii::$app->session->remove('projects_account_id'));
        ...
        ... // insert all model's field here
        ...
    }

...
...
...
}

在AccountsController中,只需告诉程序跳回项目/创建项目会话是否已设置,如下所示:

class AccountsController extends Controller
{

...
...
...

    public function actionCreate($category)
    {
        $model = new Accounts();

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            if (Projects::isSavedInSession()) {
                return $this->redirect(['projects/create');
            }
            return $this->redirect(['index');
        }
        return $this->render('create', [
            'model' => $model,
        ]);
    }

...
...
...
}

嗯,它看起来有点长,但是,是值得尝试的。无论如何,您可以将此方法用于其他目的,例如保存当前表单状态。

哦,还有一件事,我还没有在实际代码中对此进行过测试,所以如果我的代码中出现任何错误,请发表评论。

快乐的编码。 :)

相关问题