PHP建站技术分享-从入门到精通PHP建站技术分享-从入门到精通PHP建站技术分享-从入门到精通

QQ:420220301 微信/手机:150-3210-7690
当前位置:首页 > CMS教程 > Fastadmin

一张图解析FastAdmin中的FormBuilder表单生成器

管理员 2024-12-14
Fastadmin
27

一张图解FastAdmin中的FormBuilder表单生成器
点击查看大图

如果此文章有帮忙到你,欢迎打赏支持FastAdmin

功能描述

在使用FastAdmin一键生成CRUD后,默认的生成的都是原生HTML的组件代码,会有许多不熟悉前端的小伙伴改动起来会比较费劲。其实在FastAdmin中有一个简单的FormBuilder,但是它只能生成一些简单的文本框或下拉框,像FastAdmin中常用的动态下拉框、城市选择框、联动框,它就没法实现了。从1.0.1.20180630_beta版本开始,我们可以使用全新的FormBuilder用于生成我们的组件了。

千万别忘记在对应的JS方法中添加代码Form.api.bindevent("form[role=form]");进行组件初始化,否则部分组件会不生效。

通用属性

$name 通常为我们组件的名称,我们在后台接收时可以通过这个名称来获取到它所对应的值
$value 通常为我们数据库中的值,在新增的时候通常为空,在修改的时候通常需要是数据库中对应字段的值
$options 组件的扩展属性,通常为一一键值匹配并最终渲染在组件的属性中,通常我们使用的有data-rule/disabled/readonly/multiple等等,也常用于自定义组件属性。

支持组件

//生成TokenForm::token() //Label标签Form::label(string $name, string $value = null, array $options = []) //按类型生成文本框Form::input($type, $name, string $value = null, array $options = [])//普通文本框 Form::text(string $name, string $value = null, array $options = []) //密码文本框Form::password(string $name, array $options = []) //隐藏文本框Form::hidden(string $name, string $value = null, array $options = [])//Email文本框 Form::email(string $name, string $value = null, array $options = []) //URL文本框Form::url(string $name, string $value = null, array $options = []) //文件组件Form::file(string $name, array $options = []) //多行文本框Form::textarea(string $name, string $value = null, array $options = []) //富文本编辑器Form::editor(string $name, string $value = null, array $options = []) //下拉列表组件Form::select(string $name, array $list = [], string $selected = null, array $options = []) //下拉列表组件(多选)Form::selects(string $name, array $list = [], string $selected = null, array $options = []) //下拉列表组件(友好)Form::selectpicker(string $name, array $list = [], string $selected = null, array $options = []) //下拉列表组件(友好)(多选)Form::selectpickers(string $name, array $list = [], string $selected = null, array $options = []) //动态下拉列表组件Form::selectpage(string $name, string $value, string $url, string $field = null, string $primaryKey = null, array $options = []) //动态下拉列表组件(多选)Form::selectpages(string $name, string $value, string $url, string $field = null, string $primaryKey = null, array $options = []) //城市选择组件Form::citypicker(string $name, string $value, array $options = []) //开关组件Form::switcher(string $name, string $value, array $options = []) //日期选择组件Form::datepicker(string $name, string $value, array $options = []) //时间选择组件Form::timepicker(string $name, string $value, array $options = []) //日期时间选择组件Form::datetimepicker(string $name, string $value, array $options = []) //日期区间组件Form::daterange(string $name, string $value, array $options = []) //时间区间组件Form::timerange(string $name, string $value, array $options = []) //日期时间区间组件Form::datetimerange(string $name, string $value, array $options = []) //字段列表组件Form::fieldlist(string $name, string $value, string $title = null, string $template = null, array $options = []) //联动组件Form::cxselect(string $url, array $names = [], array $values = [], array $options = []) //选择数字区间Form::selectRange(string $name, string $begin, string $end, string $selected = null, array $options = []) //选择年Form::selectYear(string $name, string $begin, string $end, string $selected = null, array $options = []) //选择月Form::selectMonth(string $name, string $selected = null, array $options = [], string $format = '%m') //单个复选框Form::checkbox(string $name, string $value = '1', string $checked = null, array $options = []) //一组复选框Form::checkboxs(string $name, array $list = [], string $checked = null, array $options = []) //单个单选框Form::radio(string $name, string $value = null, string $checked = null, array $options = []) //一组单选框Form::radios(string $name, array $list = [], string $checked = null, array $options = []) //上传图片组件Form::image(string $name = null, string $value, array $inputAttr = [], array $uploadAttr = [], array $chooseAttr = [], array $previewAttr = []) //上传图片组件(多图)Form::images(string $name = null, string $value, array $inputAttr = [], array $uploadAttr = [], array $chooseAttr = [], array $previewAttr = []) //上传文件组件Form::upload(string $name = null, string $value, array $inputAttr = [], array $uploadAttr = [], array $chooseAttr = [], array $previewAttr = []) //上传文件组件(多文件)Form::uploads(string $name = null, string $value, array $inputAttr = [], array $uploadAttr = [], array $chooseAttr = [], array $previewAttr = []) //表单buttonForm::button(string $value = null, array $options = []) 

完整示例

以下为FormBuilder所支持的所有组件调用示例,你可以复制你所需要的组件到你的表单中,然后按需要修改名称或值即可

<form id="add-form" class="form-horizontal" role="form" data-toggle="validator" method="POST" action="">    <div class="form-group">        <label class="control-label col-xs-12 col-sm-2">{:__('文本框')}:</label>        <div class="col-xs-12 col-sm-8">            {:Form::text('row[text]', '', ['data-rule'=>'required'])}        </div>    </div>    <div class="form-group">        <label class="control-label col-xs-12 col-sm-2">{:__('多行文本框')}:</label>        <div class="col-xs-12 col-sm-8">            {:Form::textarea('row[textarea]', '', ['data-rule'=>'required'])}        </div>    </div>    <div class="form-group">        <label class="control-label col-xs-12 col-sm-2">{:__('富文本编辑器')}:</label>        <div class="col-xs-12 col-sm-8">            {:Form::editor('row[editor]', '', ['data-rule'=>'required'])}        </div>    </div>    <div class="form-group">        <label class="control-label col-xs-12 col-sm-2">{:__('单选')}:</label>        <div class="col-xs-12 col-sm-8">            {:Form::radios('row[radio]', ['aa'=>'AA', 'bb'=>'BB'], 'aa', ['data-rule'=>'required'])}        </div>    </div>    <div class="form-group">        <label class="control-label col-xs-12 col-sm-2">{:__('复选')}:</label>        <div class="col-xs-12 col-sm-8">            {:Form::checkboxs('row[checkbox]', ['aa'=>'AA', 'bb'=>'BB'], 'aa', ['data-rule'=>'required'])}        </div>    </div>    <div class="form-group">        <label class="control-label col-xs-12 col-sm-2">{:__('下拉列表')}:</label>        <div class="col-xs-12 col-sm-8">            {:Form::select('row[select]', ['aa'=>'AA', 'bb'=>'BB'], 'aa', ['data-rule'=>'required'])}        </div>    </div>    <div class="form-group">        <label class="control-label col-xs-12 col-sm-2">{:__('下拉列表(多选)')}:</label>        <div class="col-xs-12 col-sm-8">            {:Form::selects('row[select]', ['aa'=>'AA', 'bb'=>'BB'], 'aa', ['data-rule'=>'required'])}        </div>    </div>    <div class="form-group">        <label class="control-label col-xs-12 col-sm-2">{:__('下拉列表(友好)')}:</label>        <div class="col-xs-12 col-sm-8">            {:Form::selectpicker('row[selectpicker]', ['aa'=>'AA', 'bb'=>'BB'], 'aa', ['data-rule'=>'required'])}        </div>    </div>    <div class="form-group">        <label class="control-label col-xs-12 col-sm-2">{:__('下拉列表(友好)(多选)')}:</label>        <div class="col-xs-12 col-sm-8">            {:Form::selectpickers('row[selectpickers]', ['aa'=>'AA', 'bb'=>'BB'], 'aa', ['data-rule'=>'required'])}        </div>    </div>    <div class="form-group">        <label class="control-label col-xs-12 col-sm-2">{:__('动态下拉列表')}:</label>        <div class="col-xs-12 col-sm-8">            {:Form::selectpage('row[select]', 2, 'category/selectpage', null, null, ['data-rule'=>'required'])}        </div>    </div>    <div class="form-group">        <label class="control-label col-xs-12 col-sm-2">{:__('动态下拉列表(多选)')}:</label>        <div class="col-xs-12 col-sm-8">            {:Form::selectpages('row[select]', 2, 'category/selectpage', null, null, ['data-rule'=>'required'])}        </div>    </div>    <div class="form-group">        <label class="control-label col-xs-12 col-sm-2">{:__('城市选择框')}:</label>        <div class="col-xs-12 col-sm-8">            {:Form::citypicker('row[citypicker]', 2, ['data-rule'=>'required'])}        </div>    </div>    <div class="form-group">        <label class="control-label col-xs-12 col-sm-2">{:__('日期')}:</label>        <div class="col-xs-12 col-sm-8">            {:Form::datepicker('row[datepicker]', '', ['data-rule'=>'required'])}        </div>    </div>    <div class="form-group">        <label class="control-label col-xs-12 col-sm-2">{:__('时间')}:</label>        <div class="col-xs-12 col-sm-8">            {:Form::timepicker('row[timepicker]', '', ['data-rule'=>'required'])}        </div>    </div>    <div class="form-group">        <label class="control-label col-xs-12 col-sm-2">{:__('日期时间')}:</label>        <div class="col-xs-12 col-sm-8">            {:Form::datetimepicker('row[timepicker]', '', ['data-rule'=>'required'])}        </div>    </div>    <div class="form-group">        <label class="control-label col-xs-12 col-sm-2">{:__('日期区间')}:</label>        <div class="col-xs-12 col-sm-8">            {:Form::daterange('row[daterange]', '', ['data-rule'=>'required'])}        </div>    </div>    <div class="form-group">        <label class="control-label col-xs-12 col-sm-2">{:__('时间区间')}:</label>        <div class="col-xs-12 col-sm-8">            {:Form::timerange('row[timerange]', '', ['data-rule'=>'required'])}        </div>    </div>    <div class="form-group">        <label class="control-label col-xs-12 col-sm-2">{:__('日期时间区间')}:</label>        <div class="col-xs-12 col-sm-8">            {:Form::datetimerange('row[datetimerange]', '', ['data-rule'=>'required'])}        </div>    </div>    <div class="form-group">        <label class="control-label col-xs-12 col-sm-2">{:__('动态字段列表')}:</label>        <div class="col-xs-12 col-sm-8">            {:Form::fieldlist('row[fieldlist]', ['aa'=>'AA', 'bb'=>'BB'], null, '', ['data-rule'=>'required'])}        </div>    </div>    <div class="form-group">        <label class="control-label col-xs-12 col-sm-2">{:__('单图')}:</label>        <div class="col-xs-12 col-sm-8">            {:Form::image('row[image]', '/uploads/2018/20180629/b83227ea668e7b2d61def9812bbce3da.png', ['data-rule'=>'required'])}        </div>    </div>    <div class="form-group">        <label class="control-label col-xs-12 col-sm-2">{:__('多图')}:</label>        <div class="col-xs-12 col-sm-8">            {:Form::images('row[images]', '', ['data-rule'=>'required'])}        </div>    </div>    <div class="form-group">        <label class="control-label col-xs-12 col-sm-2">{:__('单文件')}:</label>        <div class="col-xs-12 col-sm-8">            {:Form::upload('row[upload]', '', ['data-rule'=>'required'])}        </div>    </div>    <div class="form-group">        <label class="control-label col-xs-12 col-sm-2">{:__('多文件')}:</label>        <div class="col-xs-12 col-sm-8">            {:Form::uploads('row[uploads]', '', ['data-rule'=>'required'])}        </div>    </div>    <div class="form-group">        <label class="control-label col-xs-12 col-sm-2">{:__('开关')}:</label>        <div class="col-xs-12 col-sm-8">            {:Form::switcher('row[switcher1]', '0', ['color'=>'success'])}            {:Form::switcher('row[switcher2]', '1', ['color'=>'yellow', 'disabled'=>true])}            {:Form::switcher('row[switcher3]', 'Y', ['color'=>'navy', 'yes'=>'Y', 'no'=>'N'])}            {:Form::switcher('row[switcher4]', '1', ['color'=>'info'])}            {:Form::switcher('row[switcher4]', '1', ['color'=>'danger', 'disabled'])}        </div>    </div>    <div class="form-group">        <label class="control-label col-xs-12 col-sm-2">{:__('联动选择')}:</label>        <div class="col-xs-12 col-sm-8">            {:Form::cxselect('ajax/area', ['province','city'], ['province'=>37, 'city'=>38])}        </div>    </div>    <div class="form-group layer-footer">        <label class="control-label col-xs-12 col-sm-2"></label>        <div class="col-xs-12 col-sm-8">            <button type="submit" class="btn btn-success btn-embossed disabled">{:__('Submit')}</button>            <button type="reset" class="btn btn-default btn-embossed">{:__('Reset')}</button>        </div>    </div></form>

希望以上内容对你有所帮助!如果还有其他问题,请随时提问。 各类知识收集 拥有多年CMS企业建站经验,对 iCMS, Fastadmin, ClassCMS, LeCMS, PbootCMS, PHPCMS, 易优CMS, YzmCMS, 讯睿CMS, 极致CMS, Wordpress, HkCMS, YznCMS, WellCMS, ThinkCMF, 等各类cms的相互转化,程序开发,网站制作,bug修复,程序杀毒,插件定制都可以提供最佳解决方案。

相关推荐

扫码关注

qrcode

QQ交谈

回顶部