UNPKG

nimble-ui

Version:
73 lines (72 loc) 2.92 kB
/** * 数据映射过滤器(需要修改则返回对应的字段即可) * @param {Object} back 映射的对象 * @param {Object} back.lists 当前显示的lists * @param {Object} back.initMap 初始化项的map * @param {Array<Object>} back.selectList 选中项列表 * @param {Object} back.activeIndexMap 选中index的map * @param {Object} back.data 当前选中项的数据(只读) * @param {Number} back.index 当前所属的列表所在lists的索引(只读) * @param {String} back.prefix 前缀(只读) * @param {String} back.suffix 后缀(只读) * @param {Boolean} back.isInit 是否为初始化 * @param {Object} options 初始化的时候的选项 * @return {Object} */ export default function filterLinkage (back, options) { // 联动设置 back = back || {}; let _props = options; let res = {}; let _checkData = back.data; let _initList = (_props && _props.defaultValue) || []; let index = back.index || 0; let defIndex = 0; let _lists = back.lists || []; if (back.isInit && index === 0 && _initList.length > 0 && _initList[0] !== _checkData.value) { defIndex = getActiveIndex(index, _lists); } if (defIndex > 0) { // 设置第一列默认选中项 res.activeIndexMap = Object.assign({}, back.activeIndexMap); res.activeIndexMap[index] = defIndex; } else { let nextIndex = index + 1; res.lists = _lists.slice(0); if (nextIndex < res.lists.length) { res.lists[nextIndex] = (_checkData && _checkData.children) || _props.lists[nextIndex] || []; if (!back.initMap[nextIndex]) { res.initMap = Object.assign({}, back.initMap); res.initMap[nextIndex] = true; let _activeIndex = getActiveIndex(nextIndex, res.lists); if (_activeIndex >= 0) { res.activeIndexMap = Object.assign({}, back.activeIndexMap); res.activeIndexMap[nextIndex] = _activeIndex; } } else { // 设置新项为0 if (back.isResetIndex) { res.activeIndexMap = Object.assign({}, back.activeIndexMap); res.activeIndexMap[nextIndex] = 0; } } } } return res; /** * 获取索引号 * * @param {*} _index 当前列表的索引 * @param {Array} list 列表 * @returns {Number} */ function getActiveIndex(_index, list) { let defName = _initList && _initList[_index]; if (defName && list && list.length > 0) { let activeIndex = list[_index].findIndex((item) => { if (item instanceof Object) { return item.value === defName || item.text === defName; } return item === defName; }); return activeIndex; } } }