Answer the question
In order to leave comments, you need to log in
Problem with checkboxlist. Yii2?
Good afternoon, I have such a problem.
I have a regular CRUD. And I need to add an entry to the database with the sizes of the goods. To do this, I created a memory file for myself (SizeOneHelper):
class SizeOneHelper
{
public static function typeList(): array
{
return [
'S (55/56)',
'M (57/58)',
'L (59/60)',
'XL (61/62)',
];
}
public static function typeName($drive): string
{
return ArrayHelper::getValue(self::typeList(), $drive);
}
public static function typeLabel($drive): string
{
return Html::tag('span', ArrayHelper::getValue(self::typeList(), $drive));
}
}
<?= $form->field($model, 'size')->checkboxList(SizeOneHelper::typeList()) ?>
public function actionUpdate($id)
{
$model = $this->findModel($id);
if ($model->load(Yii::$app->request->post()) && $model->save()) {
$model->size = implode(', ',$model->size);
$model->save();
return $this->redirect(['view', 'id' => $model->id]);
}
return $this->render('update', [
'model' => $model,
]);
}
Answer the question
In order to leave comments, you need to log in
Your size data is not stored very conveniently.
For now, this is how you can do it.
// Ваш статический метод в модели SizeOneHelper
public static function getSizeList(): array
{
return [
'S (55/56)',
'M (57/58)',
'L (59/60)',
'XL (61/62)',
];
}
// если в $model->size данные хранятся в виде строки, то надо привести к массиву и передать в checkboxList()
$select = explode(',', $model->size);
echo $form->field($model, 'size')->checkboxList(SizeOneHelper::getSizeList(),[
'item' => function($index, $label, $name, $checked, $value) use ($select){
if(in_array($index, $select)){
$checked = 'checked';
}
return Html::checkbox($name, $checked);
}
])
// метод в модели SizeOneHelper
public function getSizeName()
{
return ArrayHelper::filter(self::getSizeList(), explode(',',$this->size));
}
// в DetailView получаем строку по названиям размеров
echo DetailView::widget([
'model' => $model,
'attributes' => [
[
'attribute' => 'size',
'value' => function($model){
return implode(',',$model->getSizeName());
}
]
]
]);
public function getSelectSizeList()
{
return explode(',',$this->size);
}
// метод в модели SizeOneHelper
public function getSizeName()
{
return ArrayHelper::filter(self::getSizeList(), $this->getSelectSizeList());
}
// в checkboxList
echo $form->field($model, 'size')->checkboxList(SizeOneHelper::getSizeList(),[
'item' => function($index, $label, $name, $checked, $value) use ($model){
if(in_array($index, $model->getSelectSizeList())){
$checked = 'checked';
}
return Html::checkbox($name, $checked, ['label' => $label, 'value' => $value]);
}
])
Well, something like this:
//controller
public function actionView() {
$data = ;
return $this->render('view', [ 'data' => $data, 'model' => $this->findModel($id), ]);
}
//View
foreach ($data as $key => $value) {
$checked = '';
if ($value['checked'] === true) {
$checked = 'checked';
}
echo "<input type=checkbox id=" . $value['id'] . " $checked>";
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question