mirror of https://github.com/ElemeFE/element
Tree: fix setCheckedKeys bug
parent
02459f1833
commit
cdd4aa7a8b
|
@ -5,8 +5,8 @@ export const getChildState = node => {
|
||||||
let all = true;
|
let all = true;
|
||||||
let none = true;
|
let none = true;
|
||||||
let allWithoutDisable = true;
|
let allWithoutDisable = true;
|
||||||
|
for (let i = 0, j = node.length; i < j; i++) {
|
||||||
for (let n of node) {
|
const n = node[i];
|
||||||
if (n.checked !== true || n.indeterminate) {
|
if (n.checked !== true || n.indeterminate) {
|
||||||
all = false;
|
all = false;
|
||||||
if (!n.disabled) {
|
if (!n.disabled) {
|
||||||
|
@ -23,7 +23,6 @@ export const getChildState = node => {
|
||||||
|
|
||||||
const reInitChecked = function(node) {
|
const reInitChecked = function(node) {
|
||||||
const {all, none, half} = getChildState(node.childNodes);
|
const {all, none, half} = getChildState(node.childNodes);
|
||||||
|
|
||||||
if (all) {
|
if (all) {
|
||||||
node.checked = true;
|
node.checked = true;
|
||||||
node.indeterminate = false;
|
node.indeterminate = false;
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import Node, { getChildState } from './node';
|
import Node from './node';
|
||||||
import { getNodeKey } from './util';
|
import { getNodeKey } from './util';
|
||||||
|
|
||||||
export default class TreeStore {
|
export default class TreeStore {
|
||||||
|
@ -189,35 +189,36 @@ export default class TreeStore {
|
||||||
|
|
||||||
_setCheckedKeys(key, leafOnly = false, checkedKeys) {
|
_setCheckedKeys(key, leafOnly = false, checkedKeys) {
|
||||||
const allNodes = this._getAllNodes().sort((a, b) => b.level - a.level);
|
const allNodes = this._getAllNodes().sort((a, b) => b.level - a.level);
|
||||||
|
const cache = Object.create(null);
|
||||||
const keys = Object.keys(checkedKeys);
|
const keys = Object.keys(checkedKeys);
|
||||||
for (let node of allNodes) {
|
allNodes.forEach(node => node.setChecked(false, false));
|
||||||
let checked = keys.indexOf(node.data[key] + '') > -1;
|
for (let i = 0, j = allNodes.length; i < j; i++) {
|
||||||
|
const node = allNodes[i];
|
||||||
|
const nodeKey = node.data[key].toString();
|
||||||
|
let checked = keys.indexOf(nodeKey) > -1;
|
||||||
if (!checked) {
|
if (!checked) {
|
||||||
node.setChecked(false, false);
|
if (node.checked && !cache[nodeKey]) {
|
||||||
|
node.setChecked(false, false);
|
||||||
|
}
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let parent = node.parent;
|
||||||
|
while (parent && parent.level > 0) {
|
||||||
|
cache[parent.data[key]] = true;
|
||||||
|
parent = parent.parent;
|
||||||
|
}
|
||||||
|
|
||||||
if (node.isLeaf || this.checkStrictly) {
|
if (node.isLeaf || this.checkStrictly) {
|
||||||
node.setChecked(checked, false);
|
node.setChecked(true, false);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
node.setChecked(true, true);
|
||||||
const { all, none, half } = getChildState(node.childNodes);
|
|
||||||
|
|
||||||
if (all) {
|
|
||||||
node.setChecked(true, !this.checkStrictly);
|
|
||||||
} else if (half) {
|
|
||||||
checked = checked ? true : 'half';
|
|
||||||
node.setChecked(checked, !this.checkStrictly && checked === true);
|
|
||||||
} else if (none) {
|
|
||||||
node.setChecked(checked, !this.checkStrictly);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (leafOnly) {
|
if (leafOnly) {
|
||||||
node.setChecked(false, false);
|
node.setChecked(false, false);
|
||||||
const traverse = function(node) {
|
const traverse = function(node) {
|
||||||
const childNodes = node.childNodes;
|
const childNodes = node.childNodes;
|
||||||
|
|
||||||
childNodes.forEach((child) => {
|
childNodes.forEach((child) => {
|
||||||
if (!child.isLeaf) {
|
if (!child.isLeaf) {
|
||||||
child.setChecked(false, false);
|
child.setChecked(false, false);
|
||||||
|
|
Loading…
Reference in New Issue