视图表单部分

<?php $form = \yii\widgets\ActiveForm::begin() ?>
 
<?=$form->field($model,'username')->textInput() ?>
 
<?=$form->field($model,'hobby')->checkboxList(['1'=>'篮球','2'=>'足球','3'=>'游戏','4'=>'读书'])?>
 
 
<?=\yii\helpers\Html::submitButton('保存',['class'=>'btn btn-primary'])?>
 
<?php \yii\widgets\ActiveForm::end()?>

模型部分

public function beforeSave($insert) {
    if($this->hobby) {
        $this->hobby = implode(',',$this->hobby);
 
    }
    return parent::beforeSave($insert); // TODO: Change the autogenerated stub
}
 
public function afterFind() {
    $this->hobby = explode(',',$this->hobby);
    parent::afterFind();
}
yii2.0 的 多选框实现方法

第一种:
ActiveForm::checkboxList();
 优点:可以将全部数据生成多选框,自带验证

$form->field($model, 'username')->checkboxList(ArrayHelper::map($data,'id', 'customer_name'));
第二种:
ActiveForm::checkbox();
 优点:只生成一个多选框,自带验证

$form->field($model, 'username')->checkbox(ArrayHelper::map($data,'id', 'customer_name'));
第三种:
Html::activeCheckbox();

Html::activeCheckbox($model, 'username', ArrayHelper::map($data,'id', 'customer_name'));
第四种:
Html::activeCheckboxList();

Html::activeCheckboxList($model, 'username', ArrayHelper::map($data,'id', 'customer_name'));

Leave A Comment

Recommended Posts

在原文件名前加上前缀并实现文件的批量重命名

要在原文件名前加上前缀并实现文件的批量重命名,你可以根据你所使用的操作系统选择合适的脚本语言来编写脚本。以下是在不同操作系统上实现这一功能的示例: 在 Windows 上使用 PowerShell 在 macOS/Linux 上使用 Shell 脚本 在 Python 中使用 os 模块 注意事项 将上述脚本中的 folder_path 或 $folderPath 替换为你的文件夹路径,将 prefix 替换为你想要添加的前缀,然后运行脚本即可。

blueidea