ThinkSSL🔒 一键申购 5分钟快速签发 30天无理由退款 购买更放心 广告
## **表单生成** * 当需要对WEB项目数据输入时,就需要使用到表单元素,通过网页将填写好的信息发送到后端处理后保存至数据库。官方手册:https://getbootstrap.com/docs/4.5/components/forms/ * 在继承基类模板后,模板里统一使用助手函数 ```DcBuildForm``` 来生成表单,如果您没有继承基类模板,则还需要在网页里调用表单事件监听方法daicuo.form.submit()。 ``` {:DcBuildForm($args)} ``` ## **表单属性** | 参数 | 类型 | 必须 | 默认 | 说明 | |------|--------|----|----|------| | action | string | y | | 表单提交地址 | | class | string | n | | 表单class属性<br>form-inline<br>was-validated | | method | string | n | post | 表单提交方式 | | ajax | bool | y | true | AJAX提交表单 | | submit | string | n | 空 | 提交按钮 | | close | string | n | 空 | 关闭按钮 | | reset | string | n | 空 | 重置按钮 | | disabled | bool | n | false | 是否禁止提交 | | callback | string | n | function | AJAX提交成功后的回调方法 | | items | array | y | 数组 | 表单控件列表,参考下文 | ## **示例代码1** ``` {:DcBuildForm([ 'name' => 'daohang_config', 'class' => 'bg-white px-2 py-2', 'action' => DcUrlAddon(['module'=>'daohang','controll'=>'admin','action'=>'update'],''), 'method' => 'post', 'ajax' => true, 'submit' => lang('submit'), 'reset' => lang('reset'), 'close' => false, 'disabled' => false, 'callback' => '', 'items' => DcFormItems([ 'site_theme' => ['type'=>'select', 'value'=>config('site_theme'), 'option'=>$optionThemes], 'theme_wap' => ['type'=>'select', 'value'=>config('theme_wap'), 'option'=>$optionThemes], 'url_type' => ['type'=>'select', 'value'=>config('daohang.url_type'), 'option'=>$optionUrlType], 'url_index' => ['type'=>'text', 'value'=>config('daohang.url_index')], 'url_category' => ['type'=>'text', 'value'=>config('daohang.url_category')], 'url_tag' => ['type'=>'text', 'value'=>config('daohang.url_tag')], 'url_search' => ['type'=>'text', 'value'=>config('daohang.url_search')], 'url_detail' => ['type'=>'text', 'value'=>config('daohang.url_detail')], 'page_size' => ['type'=>'number', 'value'=>config('daohang.page_size')], 'title_index' => ['type'=>'text', 'value'=>config('daohang.title_index')], 'keywords_index' => ['type'=>'text', 'value'=>config('daohang.keywords_index')], 'description_index' => ['type'=>'text', 'value'=>config('daohang.description_index')], ]) ])} ``` ## **示例代码2** 与示例代码1不同的是items参数手动添加,不是通过DcFormItems函数生成。 ``` {:DcBuildForm([ 'class'=>'bg-white px-2 py-2', 'action'=>DcUrl('admin/video/update', 'module=common', ''), 'method'=>'post', 'ajax'=>true, 'submit'=>lang('submit'), 'close'=>lang('close'), 'reset'=>lang('reset'), 'disabled'=>false, 'callback'=>'daicuo.form.test', 'items'=>[ [ 'type'=>'switch', 'name'=>'video_in', 'id'=>'video_in', 'title'=>lang('video_in'), 'tips'=>'', 'value'=>config('common.video_in'), 'readonly'=>false, 'disabled'=>false, 'required'=>false, 'class'=>'row form-group', 'class_left'=>'col-md-2', 'class_right'=>'col-auto', 'class_right_control'=>'', 'class_right_tips'=>'', ], [ 'type'=>'select.custom', 'name'=>'video_size', 'id'=>'video_size', 'title'=>lang('video_size'), 'placeholder'=>'', 'tips'=>'', 'value'=>config('common.video_size'), 'option'=>['16by9'=>lang('video_size_16by9'),'21by9'=>lang('video_size_21by9'),'4by3'=>lang('video_size_4by3'),'1by1'=>lang('video_size_1by1')], 'readonly'=>false, 'disabled'=>false, 'required'=>false, 'class'=>'row form-group', 'class_left'=>'col-md-2', 'class_right'=>'col-auto', 'class_right_control'=>'', 'class_right_tips'=>'', ], [ 'type'=>'text', 'name'=>'video_ai', 'id'=>'video_ai', 'title'=>lang('video_ai'), 'placeholder'=>lang('video_ai_placeholder'), 'tips'=>'', 'value'=>config('common.video_ai'), 'readonly'=>false, 'disabled'=>false, 'required'=>false, 'class'=>'row form-group', 'class_left'=>'col-md-2', 'class_right'=>'col-md-6', 'class_right_control'=>'', 'class_right_tips'=>'', ] ])} ``` **callback JS方法的参数** | 参数 | 类型 | 必须 | 默认 | 说明 | |------|--------|----|----|------| | data | obj | y | | ajax返回的数据 | | status | string | y | | ajax返回的状态 | | xhr | string | y | | jquery的xhr装态 | **callback 示例代码** ``` function callback(data,status,xhr){ ...... } ```