{"id":572,"date":"2024-08-20T23:10:28","date_gmt":"2024-08-20T15:10:28","guid":{"rendered":"https:\/\/blog.yuekegu.com\/?p=572"},"modified":"2024-08-20T23:10:28","modified_gmt":"2024-08-20T15:10:28","slug":"yii2-checkboxlist%e4%bd%bf%e7%94%a8%e5%8f%8a%e6%95%b0%e6%8d%ae%e5%85%a5%e5%ba%93","status":"publish","type":"post","link":"https:\/\/book.yuekegu.com\/index.php\/2024\/08\/20\/yii2-checkboxlist%e4%bd%bf%e7%94%a8%e5%8f%8a%e6%95%b0%e6%8d%ae%e5%85%a5%e5%ba%93\/","title":{"rendered":"yii2 checkboxlist\u4f7f\u7528\u53ca\u6570\u636e\u5165\u5e93"},"content":{"rendered":"\n<p>\u89c6\u56fe\u8868\u5355\u90e8\u5206<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php $form = \\yii\\widgets\\ActiveForm::begin() ?>\n \n&lt;?=$form->field($model,'username')->textInput() ?>\n \n&lt;?=$form->field($model,'hobby')->checkboxList(&#91;'1'=>'\u7bee\u7403','2'=>'\u8db3\u7403','3'=>'\u6e38\u620f','4'=>'\u8bfb\u4e66'])?>\n \n \n&lt;?=\\yii\\helpers\\Html::submitButton('\u4fdd\u5b58',&#91;'class'=>'btn btn-primary'])?>\n \n&lt;?php \\yii\\widgets\\ActiveForm::end()?>\n<\/code><\/pre>\n\n\n\n<p>\u6a21\u578b\u90e8\u5206<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public function beforeSave($insert) {\n    if($this->hobby) {\n        $this->hobby = implode(',',$this->hobby);\n \n    }\n    return parent::beforeSave($insert); \/\/ TODO: Change the autogenerated stub\n}\n \npublic function afterFind() {\n    $this->hobby = explode(',',$this->hobby);\n    parent::afterFind();\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>yii2.0 \u7684 \u591a\u9009\u6846\u5b9e\u73b0\u65b9\u6cd5\n\n\u7b2c\u4e00\u79cd\uff1a\nActiveForm::checkboxList();\n \u4f18\u70b9\uff1a\u53ef\u4ee5\u5c06\u5168\u90e8\u6570\u636e\u751f\u6210\u591a\u9009\u6846\uff0c\u81ea\u5e26\u9a8c\u8bc1\n\n$form->field($model, 'username')->checkboxList(ArrayHelper::map($data,'id', 'customer_name'));\n\u7b2c\u4e8c\u79cd\uff1a\nActiveForm::checkbox();\n \u4f18\u70b9\uff1a\u53ea\u751f\u6210\u4e00\u4e2a\u591a\u9009\u6846\uff0c\u81ea\u5e26\u9a8c\u8bc1\n\n$form->field($model, 'username')->checkbox(ArrayHelper::map($data,'id', 'customer_name'));\n\u7b2c\u4e09\u79cd\uff1a\nHtml::activeCheckbox();\n\nHtml::activeCheckbox($model, 'username', ArrayHelper::map($data,'id', 'customer_name'));\n\u7b2c\u56db\u79cd\uff1a\nHtml::activeCheckboxList();\n\nHtml::activeCheckboxList($model, 'username', ArrayHelper::map($data,'id', 'customer_name'));<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u89c6\u56fe\u8868\u5355\u90e8\u5206 \u6a21\u578b\u90e8\u5206<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18,27],"tags":[],"class_list":["post-572","post","type-post","status-publish","format-standard","hentry","category-yii2","category-27"],"_links":{"self":[{"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/posts\/572","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/comments?post=572"}],"version-history":[{"count":0,"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/posts\/572\/revisions"}],"wp:attachment":[{"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/media?parent=572"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/categories?post=572"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/tags?post=572"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}