diff --git a/common/src/main/resources/static/assets/js/layui-util.js b/common/src/main/resources/static/assets/js/layui-util.js index 59e0dbe4..73dded8d 100644 --- a/common/src/main/resources/static/assets/js/layui-util.js +++ b/common/src/main/resources/static/assets/js/layui-util.js @@ -663,6 +663,105 @@ function LayuiUtil(layui, viewer) { return null; } + /** + * 级联下拉 + * @param opt + */ + this.selectLinkage = function(opt) { + var self = this; + // 级联url + var url = opt.url; + // option key + var valueKey = opt.valueKey; + // option text key + var nameKey = opt.nameKey; + var optionDataKeyArray = opt.optionDataKeyArray && (opt.optionDataKeyArray instanceof Array) ? opt.optionDataKeyArray : []; + // 级联数量 + var linkageSize = opt.linkageSize ? opt.linkageSize : 1; + // 选择ID + var selectId = opt.selectId; + var baseRootId = opt.baseRootId; + var onSelect = opt.onSelect && typeof(opt.onSelect) === 'function' ? opt.onSelect : null; + + /** + * 初始化 + * @param parentId 上级ID + * @param selectIndex 选择下标 + */ + function initSelect(parentId, selectIndex) { + if(parentId === '') { + return; + } + var refreshSelectId = (selectId + parseInt(selectIndex)); + top.restAjax.get(top.restAjax.path(url, [parentId]), {}, null, function(code, data) { + $('#'+ refreshSelectId).append(''); + $.each(data, function (index, item) { + // option携带的值 + var optionDatas = ''; + for(var i = 0, optionDataKey; optionDataKey = optionDataKeyArray[i++];) { + var optionDataValue = item[optionDataKey]; + if(!optionDataValue) { + continue; + } + optionDatas += ' data-data'+ i +'="'+ item[optionDataKey] +'"'; + } + $('#'+ refreshSelectId).append(''); + }); + // 渲染表单下拉框 + form.render('select'); + }, function(code, data) { + top.dialog.msg(data.msg); + }) + } + /** + * 初始化级联事件 + * @param selectId 选择ID + * @param size 级联长度 + */ + function addLinkageEvent() { + for(var i = 0; i < linkageSize; i++) { + form.on('select('+ (selectId + i) +'Select)', function(data) { + var index = parseInt(data.elem.id.replace(selectId, '')); + var nextIndex = index + 1; + // 级联清空 + for(var j = nextIndex; j < linkageSize; j++) { + $('#'+ (selectId + j)).empty(); + } + form.render('select'); + // 最后一个不触发初始化事件 + if(index < (linkageSize - 1)) { + initSelect(data.value, nextIndex); + } + + if(onSelect) { + var option = self.selectedOption('#'+ data.elem.id, data.value); + onSelect(data, option, index); + } + }) + } + } + + initSelect(baseRootId, 0); + addLinkageEvent(); + } + + /** + * 地区级联下拉 + * @param opt + */ + this.selectLinkageArea = function(opt) { + this.selectLinkage({ + url: 'api/area/listbyparentid/{parentId}', + valueKey: 'areaId', + nameKey: 'areaName', + linkageSize: opt.linkageSize ? opt.linkageSize : 5, + baseRootId: opt.baseRootId, + selectId: opt.selectId, + optionDataKeyArray: opt.optionDataKeyArray, + onSelect: opt.onSelect && typeof(opt.onSelect) === 'function' ? opt.onSelect : null + }) + } + /** * 禁用表单全部字段 */