layui.define(['form', 'common', 'ajax'], function (exports) { let $ = layui.$; let form = layui.form; let layer = layui.layer; let common = layui.common; let ajax = layui.ajax; /** * 通用 * @param opt */ function dataDynamic(opt) { let url = opt.url; let name = opt.name; let valueKey = opt.valueKey ? opt.valueKey : ''; let nameKey = opt.nameKey ? opt.nameKey : ''; let where = opt.where ? opt.where : {}; // 请求得到的列表Key,格式'a.b.c' let dataListKey = opt.dataListKey ? opt.dataListKey : ''; let formFilter = opt.formFilter; let checkedValue = opt.checkedValue; let required = typeof (opt.required) !== 'undefined' && opt.required !== null ? opt.required : false; let radioDatasetKeyArray = opt.radioDatasetKeyArray && $.isArray(opt.radioDatasetKeyArray) ? opt.radioDatasetKeyArray : []; let onInit = opt.onInit && $.isFunction(opt.onInit) ? opt.onInit : null; let onCheck = opt.onCheck && $.isFunction(opt.onCheck) ? opt.onCheck : null; let elemContainer = $(`#${name}Container`); /** * 初始化容器 */ function initContainer() { elemContainer.empty(); ajax.get(url, where, null, function (code, data) { let radioArray = []; let dataList = data; if (dataListKey) { let dataListKeyArray = dataListKey.split('.'); $.each(dataListKeyArray, function (i, key) { dataList = dataList[key]; }) } $.each(dataList, function (i, item) { let value = valueKey ? item[valueKey] : item; let title = nameKey ? item[nameKey] : item; let radioDatasetArray = []; $.each(radioDatasetKeyArray, function (j, radioDatasetKey) { let radioDatasetValue = item[radioDatasetKey]; if (!radioDatasetValue) { return; } radioDatasetArray.push(`data-${common.camelCase2lineCase(radioDatasetKey)}="${radioDatasetValue}"`); }); radioArray.push(``); }); elemContainer.append(radioArray.join('')); // 赋值 if (checkedValue) { let formData = {}; formData[`${name}`] = checkedValue; form.val(formFilter, formData); onInit ? onInit(checkedValue) : null; } form.render('radio', formFilter); }, function (code, data) { layer.msg(data.msg); }); } /** * 添加事件 */ function addEvent() { form.on(`radio(${name}Radio)`, function(data) { onCheck ? onCheck(data) : null; }); } initContainer(); addEvent(); } /** * 静态数据 * @param opt */ function dataStatic(opt) { let name = opt.name; let radios = opt.radios && $.isArray(opt.radios) ? opt.radios : []; let formFilter = opt.formFilter; let checkedValue = opt.checkedValue; let required = typeof (opt.required) !== 'undefined' && opt.required !== null ? opt.required : false; let onInit = opt.onInit && $.isFunction(opt.onInit) ? opt.onInit : null; let onCheck = opt.onCheck && $.isFunction(opt.onCheck) ? opt.onCheck : null; let elemContainer = $(`#${name}Container`); /** * 初始化容器 */ function initContainer() { elemContainer.empty(); let radioArray = []; $.each(radios, function (i, radio) { let value = radio['value']; let title = radio['title']; let radioDatasetArray = []; for(let key in radio) { if(key === 'value' || key === 'title') { continue; } radioDatasetArray.push(`data-${common.camelCase2lineCase(key)}="${radio[key]}"`); } radioArray.push(``); }); elemContainer.append(radioArray.join('')); // 赋值 if (checkedValue) { let formData = {}; formData[`${name}`] = checkedValue; form.val(formFilter, formData); onInit ? onInit(checkedValue) : null; } form.render('radio', formFilter); } /** * 添加事件 */ function addEvent() { form.on(`radio(${name}Radio)`, function(data) { onCheck ? onCheck(data) : null; }); } initContainer(); addEvent(); } /** * 数据字典 * @param opt */ function dataDictionary(opt) { dataDynamic({ url: ajax.path('api/data/list/parent-id/{parentId}', [opt.parentId]), name: opt.name, required: opt.required, formFilter: opt.formFilter, valueKey: 'dataId', nameKey: 'dataName', checkedValue: opt.checkedValue, radioDatasetKeyArray: opt.radioDatasetKeyArray, where: opt.where, onInit: opt.onInit, onCheck: opt.onCheck }) } exports('radio', { dataDynamic: dataDynamic, dataStatic: dataStatic, dataDictionary: dataDictionary }); });