Skip to content

Commit

Permalink
fix #16
Browse files Browse the repository at this point in the history
  • Loading branch information
FairyEver committed Dec 24, 2019
1 parent 78ec1f1 commit 7a32b63
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 59 deletions.
58 changes: 9 additions & 49 deletions src/components/d2-tree/component.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
</style>

<script>
import utils from '@/utils'
import tree from '@/mixins/component.tree'
import fieldChange from '@/mixins/el.fieldChange'
Expand Down Expand Up @@ -75,57 +76,16 @@ export default {
* @description 删除 half 状态的数据
*/
removeHalf (sourceArray) {
/**
* 找到这个值在 tree 中的对应并且判断是否子集都选中
* 如果没有找到 返回 false
* 如果找到了 并且子集都选中 返回 false
* 如果找到了 子集没有都选中 返回 true
*/
// const isHalf = item => {
// let node = {}
// let half = false
// const find = treeArray => {
// for (let index = 0; index < treeArray.length; index++) {
// const element = treeArray[index]
// if (element[this.keyId] === item) {
// node = element
// break
// } else {
// find(element[this.keyChildren])
// }
// }
// }
// find(this.currentData)
// const children = node[this.keyChildren] || []
// for (let index = 0; index < children.length; index++) {
// const element = children[index]
// if (sourceArray.findIndex(e => e === element[this.keyId]) < 0) {
// half = true
// break
// }
// }
// return half
// }
// return sourceArray.filter(e => !isHalf(e))
/**
* @description 在数据源中找到这一项
*/
function findInSource (item) {
const allCheck = (result, item) => {
if (!result || sourceArray.indexOf(item[this.keyId]) < 0) return false
if (utils.helper.hasChildren(item, this.keyChildren)) return item[this.keyChildren].reduce(allCheck, true)
return true
}
/**
* @description 检查一个项目是否子集全部选中
*/
function isAllChecked (item) {
const filter = item => {
const obj = this.flattenedObject[item]
return obj ? [obj].reduce(allCheck, true) : false
}
console.log(sourceArray)
return sourceArray.reduce((result, item) => {
if (isAllChecked(item)) {
result.push(item)
}
return result
}, [])
return sourceArray.filter(filter)
},
/**
* @description 初始化 根据 source 的数据类型设置数据
Expand Down
14 changes: 4 additions & 10 deletions src/utils/modules/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,21 +149,18 @@ export function isLegalEmailValidator (rule, value, callback) {
* @param {Object} config {Array} data 树形数据
* @param {Object} config {String} keyChildren 子节点字段名
* @param {Object} config {Boolean} includeChildren 输出的数据中是否包含子节点数据
* @param {Object} config {Boolean} freeze 输出结果前冻结数据
*/
export function flatTreeToArray ({
data = [],
keyChildren = 'children_list',
includeChildren = false,
freeze = true
includeChildren = false
} = {}) {
function maker (result, item) {
result.push(includeChildren ? item : omit(item, [ keyChildren ]))
if (hasChildren(item, keyChildren)) result = result.concat(item[keyChildren].reduce(maker, []))
return result
}
const result = data.reduce(maker, [])
return freeze ? Object.freeze(result) : result
return data.reduce(maker, [])
}

/**
Expand All @@ -172,22 +169,19 @@ export function flatTreeToArray ({
* @param {Object} config {String} keyChildren 子节点字段名
* @param {Object} config {String} keyId 唯一 id 字段名
* @param {Object} config {Boolean} includeChildren 输出的数据中是否包含子节点数据
* @param {Object} config {Boolean} freeze 输出结果前冻结数据
*/
export function flatTreeToObject ({
data = [],
keyChildren = 'children_list',
keyId = 'id',
includeChildren = false,
freeze = true
includeChildren = false
} = {}) {
function maker (result, item) {
result[item[keyId]] = includeChildren ? item : omit(item, [ keyChildren ])
if (hasChildren(item, keyChildren)) Object.assign(result, item[keyChildren].reduce(maker, {}))
return result
}
const result = data.reduce(maker, {})
return freeze ? Object.freeze(result) : result
return data.reduce(maker, {})
}

/**
Expand Down

0 comments on commit 7a32b63

Please sign in to comment.