<rlx>
1. 优化arr2tree
This commit is contained in:
parent
f8fa44c94c
commit
12b7f0ce0e
101
src/index.js
101
src/index.js
@ -1,16 +1,16 @@
|
|||||||
import { name, version, website } from '../package.json'
|
import { name, version, website } from '../package.json'
|
||||||
import { selector, warn } from '@/common/util'
|
import { selector, warn } from '@/common/util'
|
||||||
import Select from '@/components/xm-select'
|
import Select from '@/components/xm-select'
|
||||||
|
|
||||||
export const datas = {};
|
export const datas = {};
|
||||||
export const optionData = {};
|
export const optionData = {};
|
||||||
export const childData = {};
|
export const childData = {};
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
name,
|
name,
|
||||||
version,
|
version,
|
||||||
doc: website,
|
doc: website,
|
||||||
render(options) {
|
render(options) {
|
||||||
let { el } = options;
|
let { el } = options;
|
||||||
options.dom = selector(el);
|
options.dom = selector(el);
|
||||||
|
|
||||||
@ -23,50 +23,57 @@ export default {
|
|||||||
optionData[el] = options;
|
optionData[el] = options;
|
||||||
|
|
||||||
let instance = new Select(options);
|
let instance = new Select(options);
|
||||||
//已经渲染
|
//已经渲染
|
||||||
if (instance && instance.options.__render_success) {
|
if (instance && instance.options.__render_success) {
|
||||||
datas[el] = instance;
|
datas[el] = instance;
|
||||||
}
|
}
|
||||||
return instance;
|
return instance;
|
||||||
},
|
|
||||||
get(filter, single) {
|
|
||||||
let type = Object.prototype.toString.call(filter);
|
|
||||||
let method;
|
|
||||||
switch (type) {
|
|
||||||
case '[object String]':
|
|
||||||
filter && (method = item => item === filter);
|
|
||||||
break;
|
|
||||||
case '[object RegExp]':
|
|
||||||
method = item => filter.test(item);
|
|
||||||
break;
|
|
||||||
case '[object Function]':
|
|
||||||
method = filter;
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
let keys = Object.keys(datas)
|
|
||||||
let list = (method ? keys.filter(method) : keys).map(key => datas[key]).filter(instance => selector(instance.options.el));
|
|
||||||
|
|
||||||
return single ? list[0] : list;
|
|
||||||
},
|
|
||||||
batch(filter, method) {
|
|
||||||
let args = [...arguments];
|
|
||||||
args.splice(0, 2);
|
|
||||||
return this.get(filter).map(instance => instance[method](...args));
|
|
||||||
},
|
},
|
||||||
arr2tree(arr, pid, id, children, topParentId){
|
get(filter, single) {
|
||||||
arr.forEach(item => {
|
let type = Object.prototype.toString.call(filter);
|
||||||
if(item[pid] != topParentId){
|
let method;
|
||||||
let parent = arr.find(i => i[id] === item[pid])
|
switch (type) {
|
||||||
if(parent){
|
case '[object String]':
|
||||||
if(!parent[children]){
|
filter && (method = item => item === filter);
|
||||||
parent[children] = [];
|
break;
|
||||||
}
|
case '[object RegExp]':
|
||||||
parent[children].push(item)
|
method = item => filter.test(item);
|
||||||
}
|
break;
|
||||||
|
case '[object Function]':
|
||||||
|
method = filter;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
let keys = Object.keys(datas)
|
||||||
|
let list = (method ? keys.filter(method) : keys).map(key => datas[key]).filter(instance => selector(instance.options.el));
|
||||||
|
|
||||||
|
return single ? list[0] : list;
|
||||||
|
},
|
||||||
|
batch(filter, method) {
|
||||||
|
let args = [...arguments];
|
||||||
|
args.splice(0, 2);
|
||||||
|
return this.get(filter).map(instance => instance[method](...args));
|
||||||
|
},
|
||||||
|
arr2tree(arr=[], pid='pid', id='id', children='children', topParentId=0){
|
||||||
|
const datas = {} // 数据缓存
|
||||||
|
return safety(arr).filter(item =>{
|
||||||
|
const _id = item[id] // 提取自身id 必须唯一
|
||||||
|
const _pid = item[pid] // 提取父级id
|
||||||
|
const self = datas[_id] // 读取数据缓存中的数据
|
||||||
|
let parent = datas[_pid] // 读取父级数据
|
||||||
|
if(self){ // 如果缓存中有数据
|
||||||
|
item[children] = self[children] // 把缓存的数据保存到自身
|
||||||
}
|
}
|
||||||
|
datas[_id] = item // 替换缓存中的数据
|
||||||
|
if(!parent){ // 如果没有找到父级数据
|
||||||
|
parent = { // 创建一个全新的父级数据
|
||||||
|
[children]:[]
|
||||||
|
}
|
||||||
|
datas[_pid] = parent // 记录到缓存
|
||||||
|
}
|
||||||
|
parent.push(item) // 推送自身到父级
|
||||||
|
return id == topParentId // 这里用 == 防止出现 字符串不等于数字类型的情况
|
||||||
})
|
})
|
||||||
return arr.filter(i => i[pid] == topParentId)
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user