yii2 checkboxlist使用及数据入库
生活随笔
收集整理的這篇文章主要介紹了
yii2 checkboxlist使用及数据入库
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
視圖表單部分
$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']) \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 的 多選框?qū)崿F(xiàn)方法
第一種:ActiveForm::checkboxList();?優(yōu)點:可以將全部數(shù)據(jù)生成多選框,自帶驗證
$form->field($model, 'username')->checkboxList(ArrayHelper::map($data,'id', 'customer_name'));第二種:ActiveForm::checkbox();?優(yōu)點:只生成一個多選框,自帶驗證
$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'));總結
以上是生活随笔為你收集整理的yii2 checkboxlist使用及数据入库的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: *ST印纪被终止上市,成为A股第一只面临
- 下一篇: 好享贷的钱怎么取出来