如何在Yii2中获取dropDownList的值?

时间:2017-09-04 18:44:46

标签: yii2

我没有抓住Yii2中dropDownList上的选定值($ model->属性),这可能是错的?感谢

这是位于View:

上的代码
    $command = $connection->createCommand('SELECT att1 
                                             FROM table
                                                ORDER By table_id ASC');
    $rows = $command->queryAll();
    $command->execute();

    $listData = ArrayHelper::index($rows, null, 'table_id');

然后在同一个View上调用$ listData

    <div class="row">
        <div class="col-lg-9"> 

            <?php Pjax::begin(['enablePushState' => false]); ?>

            <?php $form = ActiveForm::begin(['id' => 'test-form', 'options' => ['data-pjax' => true]
            ]); ?>

                <?= $form->field($model, 'attribute')->dropDownList($listData, ['prompt'=>'List of attributes.']); ?>


                <div class="form-group">
                    <?= Html::submitButton('Submit', ['class' => 'btn btn-primary', 'name' => 'test-button']) ?>
                </div>

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

        </div>
 </div>

这是控制器:

public function actionTest()
    {
        $model = new TestForm();

            if ($model->load(Yii::$app->request->post()) && $model->validate()) {
                    $model->insertTest();
                 return $this->renderAjax('test', ['model' => $model]);       

            } else {
                return $this->renderAjax('test', ['model' => $model]);
            }

    } 

模型具有$ attribute的定义,而insertTest()是一个使用$ attribute的值来查询数据库的函数。

4 个答案:

答案 0 :(得分:2)

想到你应该使用

ArrayHelper::map($listData, 'table_id', 'table_id');

因为你需要一维数组。

最好使用ActiveRecord查询数据库。

ArrayHelper Docs

答案 1 :(得分:1)

您只从表中选择att1,因此您应该映射此列

$listData = ArrayHelper::index($rows, 'att1');

for debug尝试修改你的actionTest评论$ model-&gt; insertTest();并使用$ model-&gt; save(); 如果值保存在db中,那么你应该检查你的$ model-&gt; insertTest()函数

  public function actionTest()
  {
      $model = new TestForm();

          if ($model->load(Yii::$app->request->post()) && $model->validate()) {
                // $model->insertTest();
               $model->save();
               return $this->renderAjax('test', ['model' => $model]);       

          } else {
              return $this->renderAjax('test', ['model' => $model]);
          }

  } 

答案 2 :(得分:1)

你应该在ArrayHelper中使用map

program HashTags;

{$APPTYPE CONSOLE}

uses
  Classes, SysUtils;

procedure TestHashTags;
var
  TL : TStringList;
  S : String;
  i : Integer;
begin
  TL := TStringList.Create;
  try
    S := '2017-08-31 This is a useless sentence being used as an example. #Example #Date:2017-09-01 #NothingWow (and then some more text for good measure)';
    TL.DelimitedText := S;
    for i := 0 to TL.Count - 1 do begin
    if Pos('#', TL[i]) = 1 then
      writeln(i, ' ', TL[i]);
    end;
  finally
    TL.Free;
  end;
  readln;
end;

begin
  TestHashTags;
end.

注意:&#39; table_text&#39;是表格属性,将显示在下拉标签中。

答案 3 :(得分:0)

我找到了解决这个问题的方法。

通过这种方式,表单无法保存提交的信息。

   $command = $connection->createCommand('SELECT att1 
                                                 FROM table
                                                    ORDER By table_id ASC');
   $rows = $command->queryAll();
   $command->execute();

  $listData = ArrayHelper::index($rows, null, 'table_id');

但是,通过这种方式,表单可以获取所选的值并将其放入变量中。

$rows = table::find()->all();
$listData = ArrayHelper::map($rows,'table_id','att1');