在更新帖子时检查Yii2中的checkboxList?

时间:2022-10-05 08:55:58

I want to checked Yii2 CheckboxList at time of post update my list of options is mention below 在更新帖子时检查Yii2中的checkboxList?

我想在发布更新时检查Yii2 CheckboxList我的选项列表如下所述

<?= $form->field($category,'title')->checkboxList([1=>'Latest news','2'=>'Unit Performance','3'=>'Latest Technology'])->label(FALSE); ?>

I want to check some item at time of update which is selected at the time of post creation like latest news .

我想在更新时检查一些项目,这是在创建帖子时选择的最新消息。

在更新帖子时检查Yii2中的checkboxList?

Kindly help me

请帮助我

2 个解决方案

#1


15  

Use line of code of example.

使用示例代码行。

 $list = [1=>'Latest news','2'=>'Unit Performance','3'=>'Latest Technology'];

<?= $form->field($category,'title')->checkboxList($list)->label(FALSE); ?>

If option "Latest news" and "Unit Performance" is selected so, on update selected option value array will be $checkedList = [1, 2];

如果选择“最新消息”和“单位性能”选项,则更新时选择的选项值数组将为$ checkedList = [1,2];

So, Simply assign $checkedList array to $category->title. Like as,

因此,只需将$ checkedList数组分配给$ category-> title。像,

$category->title = $checkedList;

Full example is:

完整的例子是:

 $list = [1=>'Latest news','2'=>'Unit Performance','3'=>'Latest Technology'];

 if(!$category->isNewRecord) {
     $checkedList = [1, 2]; //get selected value from db if value exist
     $category->title = $checkedList;
 }

<?= $form->field($category,'title')->checkboxList($list)->label(FALSE); ?>

#2


2  

You can use the following code to get array of selected checkboxes.

您可以使用以下代码获取所选复选框的数组。

$selected_checkbox_array = Yii::$app->request->post("title");

Here if you would like to concat them into string, you can use php's implode function

在这里,如果你想将它们连成字符串,你可以使用php的implode函数

$selected_checkboxes = implode(',', $selected_checkbox_array);

Other way is

其他方式是

if ($model->load(Yii::$app->request->post()))
{

       $model->title= implode(",", $model->title);

       if($model->save())
       {
              return $this->redirect(['gridpage', 'id' => $model->id]);
       }    
}

#1


15  

Use line of code of example.

使用示例代码行。

 $list = [1=>'Latest news','2'=>'Unit Performance','3'=>'Latest Technology'];

<?= $form->field($category,'title')->checkboxList($list)->label(FALSE); ?>

If option "Latest news" and "Unit Performance" is selected so, on update selected option value array will be $checkedList = [1, 2];

如果选择“最新消息”和“单位性能”选项,则更新时选择的选项值数组将为$ checkedList = [1,2];

So, Simply assign $checkedList array to $category->title. Like as,

因此,只需将$ checkedList数组分配给$ category-> title。像,

$category->title = $checkedList;

Full example is:

完整的例子是:

 $list = [1=>'Latest news','2'=>'Unit Performance','3'=>'Latest Technology'];

 if(!$category->isNewRecord) {
     $checkedList = [1, 2]; //get selected value from db if value exist
     $category->title = $checkedList;
 }

<?= $form->field($category,'title')->checkboxList($list)->label(FALSE); ?>

#2


2  

You can use the following code to get array of selected checkboxes.

您可以使用以下代码获取所选复选框的数组。

$selected_checkbox_array = Yii::$app->request->post("title");

Here if you would like to concat them into string, you can use php's implode function

在这里,如果你想将它们连成字符串,你可以使用php的implode函数

$selected_checkboxes = implode(',', $selected_checkbox_array);

Other way is

其他方式是

if ($model->load(Yii::$app->request->post()))
{

       $model->title= implode(",", $model->title);

       if($model->save())
       {
              return $this->redirect(['gridpage', 'id' => $model->id]);
       }    
}