diff --git a/src/components/xm-select/index.js b/src/components/xm-select/index.js index 00edfba228d86b530466c4f633dd4993d0f18229..6900dd0f7c46f26aa2e4935b1babdfd600164de9 100644 --- a/src/components/xm-select/index.js +++ b/src/components/xm-select/index.js @@ -3,7 +3,7 @@ import { datas, optionData, childData } from '@/index.js'; import { warn, listenerClose, isArray, deepMerge, exchangeOptionsData, toSimple, delProp } from '@/common/util' import Framework from '@/components/framework' import defaultOptions from '@/config/options' - + class xmOptions { constructor(options) { @@ -33,12 +33,18 @@ class xmOptions { warn(`没有找到渲染对象: ${options.el}, 请检查`) return ; } + + //直接重置data的值,不需要深度复制 + if(updateData){ + this.options.data = options.data; + } //判断data的数据类型 let optionsData = this.options.data || []; if(typeof(optionsData) === 'function'){ optionsData = optionsData(); this.options.data = optionsData; } + if(!isArray(optionsData)){ warn(`data数据必须为数组类型, 不能是${ typeof(data) }类型`) return ; @@ -83,17 +89,17 @@ class xmOptions { /** * 获取多选选中的数据 */ - getValue(type){ - const { tree, prop, data } = this.options; - let sels = childData[this.options.el].state.sels; - let list = sels; - - //树结构开启极简显示 - if(tree.show && tree.strict && tree.simple){ - list = [] - toSimple(data, sels, list, prop); - } - + getValue(type){ + const { tree, prop, data } = this.options; + let sels = childData[this.options.el].state.sels; + let list = sels; + + //树结构开启极简显示 + if(tree.show && tree.strict && tree.simple){ + list = [] + toSimple(data, sels, list, prop); + } + let arr = delProp(list, prop.children, [ '__node' ]);; if(type === 'name'){ @@ -119,7 +125,7 @@ class xmOptions { if(!isArray(sels)){ warn('请传入数组结构...') return ; - } + } childData[this.options.el].value(this.options.radio ? sels.slice(0, 1) : sels, show, listenOn); return this; } @@ -160,96 +166,96 @@ class xmOptions { childData[this.options.el].updateBorderColor(showColor) ) return this; - } - - /** - * 获取选中的节点 - * leafOnly: 是否只是叶子节点,默认值为 false - * includeHalfChecked: 是否包含半选节点,默认值为 false - */ - getTreeValue(leafOnly, includeHalfChecked){ - const { tree, cascader, prop } = this.options; - const { value } = prop; - - //如果不是树状结构, 直接使用getValue - if(!(tree.show || cascader.show)){ - return this.getValue(leafOnly); - } - - //获得当前已经选中的数据 - let sels = childData[this.options.el].state.sels; - - //存储选中的数据 - let list = []; - - let nodeType = tree.nodeType; - - const listPush = (item, type) => { - if(!list.find(i => i[value] === item[value])){ - item = { ...item } - item[nodeType] = type; - list.push(item); - } - } - - for(let i = 0; i < sels.length; i++){ - let node = { ...sels[i] }; - //首页先把子节点放入到数据中 - listPush(node, 'leaf'); - while(node = node.__node.parent){ - let { half, selected } = node.__node - //如果想要父节点, 检测父节点是否为选中状态 - if(!leafOnly && selected){ - listPush(node, 'parent'); - }else - //如果是需要半选状态, 并且处于半选状态 - if(includeHalfChecked && half && !selected){ - listPush(node, 'half'); - } - } - } - - let arr = delProp(list, prop.children, [ '__node' ]);; - - return arr; - } - - /** - * 动态操作树状结构的节点展开状态 - */ - changeExpandedKeys(keys){ - childData[this.options.el].changeExpandedKeys(keys) - return this; - } - - /** - * 动态启用一些选项 - */ - enable(sels){ - if(!isArray(sels)){ - warn('请传入数组结构...') - return ; - } - if(sels.length === 0){ - return ; - } - childData[this.options.el].upDate(sels, true) - return this; - } - - /** - * 动态禁用一些选项 - */ - disable(sels){ - if(!isArray(sels)){ - warn('请传入数组结构...') - return ; - } - if(sels.length === 0){ - return ; - } - childData[this.options.el].upDate(sels, false) - return this; + } + + /** + * 获取选中的节点 + * leafOnly: 是否只是叶子节点,默认值为 false + * includeHalfChecked: 是否包含半选节点,默认值为 false + */ + getTreeValue(leafOnly, includeHalfChecked){ + const { tree, cascader, prop } = this.options; + const { value } = prop; + + //如果不是树状结构, 直接使用getValue + if(!(tree.show || cascader.show)){ + return this.getValue(leafOnly); + } + + //获得当前已经选中的数据 + let sels = childData[this.options.el].state.sels; + + //存储选中的数据 + let list = []; + + let nodeType = tree.nodeType; + + const listPush = (item, type) => { + if(!list.find(i => i[value] === item[value])){ + item = { ...item } + item[nodeType] = type; + list.push(item); + } + } + + for(let i = 0; i < sels.length; i++){ + let node = { ...sels[i] }; + //首页先把子节点放入到数据中 + listPush(node, 'leaf'); + while(node = node.__node.parent){ + let { half, selected } = node.__node + //如果想要父节点, 检测父节点是否为选中状态 + if(!leafOnly && selected){ + listPush(node, 'parent'); + }else + //如果是需要半选状态, 并且处于半选状态 + if(includeHalfChecked && half && !selected){ + listPush(node, 'half'); + } + } + } + + let arr = delProp(list, prop.children, [ '__node' ]);; + + return arr; + } + + /** + * 动态操作树状结构的节点展开状态 + */ + changeExpandedKeys(keys){ + childData[this.options.el].changeExpandedKeys(keys) + return this; + } + + /** + * 动态启用一些选项 + */ + enable(sels){ + if(!isArray(sels)){ + warn('请传入数组结构...') + return ; + } + if(sels.length === 0){ + return ; + } + childData[this.options.el].upDate(sels, true) + return this; + } + + /** + * 动态禁用一些选项 + */ + disable(sels){ + if(!isArray(sels)){ + warn('请传入数组结构...') + return ; + } + if(sels.length === 0){ + return ; + } + childData[this.options.el].upDate(sels, false) + return this; } }