/** * 1.多选数据赋值 * * formSelects.data(ID, type, config); * * @param ID xm-select的值 * @param type 'local' | 'server', 本地数据或者远程数据 * @param config 配置项 * arr 本地数据数组 * url 远程数据链接 * keyword 远程数据搜索内容, 其他附加数据可以使用 formSelects.config设置 * linkage 是否为联动多选 * linkageWidth 联动多选没级宽度 */ //以下代码可以在console中运行测试, 结果查看基础示例第一个 var formSelects = layui.formSelects; //local模式 formSelects.data( 'select1' , 'local' , { arr: [ { "name" : "分组-1" , "type" : "optgroup" }, { "name" : "北京" , "value" : 1}, { "name" : "上海" , "value" : 2}, { "name" : "分组-2" , "type" : "optgroup" }, { "name" : "广州" , "value" : 3}, { "name" : "深圳" , "value" : 4}, { "name" : "天津" , "value" : 5} ] }); //server模式 formSelects.data( 'select1' , 'server' , { url: 'https://www.fastmock.site/mock/29487363107ab7d242c46305cadf4c52/formSelects/layui/data' , keyword: '水果' }); //server返回数据与远程搜索数据结构一致 { "code" :0, "msg" : "success" , "data" :[ { "name" : "北京" , "value" :1, "selected" : "" , "disabled" : "" }, { "name" : "上海" , "value" :2, "selected" : "" , "disabled" : "" }, { "name" : "广州" , "value" :3, "selected" : "selected" , "disabled" : "" }, { "name" : "深圳" , "value" :4, "selected" : "" , "disabled" : "disabled" }, { "name" : "天津" , "value" :5, "selected" : "" , "disabled" : "" } ] } //当然你也可以偷懒, 返回如下结构 [ { "name" : "北京" , "value" :1, "selected" : "" , "disabled" : "" }, { "name" : "上海" , "value" :2, "selected" : "" , "disabled" : "" }, { "name" : "广州" , "value" :3, "selected" : "selected" , "disabled" : "" }, { "name" : "深圳" , "value" :4, "selected" : "" , "disabled" : "disabled" }, { "name" : "天津" , "value" :5, "selected" : "" , "disabled" : "" } ] /*************** 华丽的分割线 ***************/ //来一个树状结构的数据 formSelects.data( 'select15' , 'local' , { arr: [ {name: '北京' , value: 1, xslkdf: '123' , children: [ {name: '朝阳' , value: 11}, {name: '海淀' , value: 12} ]}, {name: '深圳' , value: 2, children: [{name: '龙岗' , value: 21}]}, ] }); /*************** 华丽的分割线 ***************/ //联动多选数据格式 //local, 注意 value值请保证唯一 formSelects.data( 'select15' , 'local' , { arr: [ { "name" : "北京" , "value" : 1, "children" : [ { "name" : "北京市1" , "value" : 12, "children" : [ { "name" : "朝阳区1" , "value" : 13, "children" : []}, { "name" : "朝阳区2" , "value" : 14, "children" : []}, { "name" : "朝阳区3" , "value" : 15, "children" : []}, { "name" : "朝阳区4" , "value" : 16, "children" : []}, ]}, { "name" : "北京市2" , "value" : 17, "children" : []}, { "name" : "北京市3" , "value" : 18, "children" : []}, { "name" : "北京市4" , "value" : 19, "children" : []}, ] }, { "name" : "天津" , "value" : 2, "children" : [ { "name" : "天津市1" , "value" : 51, "children" : []}, ] }, ], linkage: true //开启联动模式 }); //server formSelects.data( 'select15' , 'server' , { url: 'http://yapi.demo.qunar.com/mock/9813/layui/citys' , linkage: true , linkageWidth: 130 //代表每一级别的宽度, 默认是100 }); //server返回数据, 同上 value不能重复 { "code" :0, "msg" : "success" , "data" :[ { "name" : "北京" , "value" : 1, "children" : [ { "name" : "北京市1" , "value" : 12, "children" : [ { "name" : "朝阳区1" , "value" : 13, "children" : []}, { "name" : "朝阳区2" , "value" : 14, "children" : []}, { "name" : "朝阳区3" , "value" : 15, "children" : []}, { "name" : "朝阳区4" , "value" : 16, "children" : []}, ]}, { "name" : "北京市2" , "value" : 17, "children" : []}, { "name" : "北京市3" , "value" : 18, "children" : []}, { "name" : "北京市4" , "value" : 19, "children" : []}, ] }, { "name" : "天津" , "value" : 2, "children" : [ { "name" : "天津市1" , "value" : 51, "children" : []}, ] }, ] } |
所有评论(0)