Permalink
Browse files

feat: tree插入数据的时候增加指定index处理

  • Loading branch information...
LiuYueKai committed Apr 19, 2017
1 parent eaf96ed commit d9db726aa188820b721678ae280d223e57ffe16f
Showing with 66 additions and 19 deletions.
  1. +66 −19 src/js/treeComp.js
View
@@ -365,15 +365,29 @@
addInitRoot: function(initRoot) {
_init.roots.push(initRoot);
},
- addNodesData: function(setting, parentNode, nodes) {
+ addNodesData: function(setting, parentNode, nodes, index) {
var childKey = setting.data.key.children;
- if (!parentNode[childKey]) parentNode[childKey] = [];
- if (parentNode[childKey].length > 0) {
+ if (!parentNode[childKey]){
+ parentNode[childKey] = [];
+ index = -1;
+ }else if (index >= parentNode[childKey].length) {
+ index = -1;
+ }
+
+ if (parentNode[childKey].length > 0 && index === 0) {
+ parentNode[childKey][0].isFirstNode = false;
+ view.setNodeLineIcos(setting, parentNode[childKey][0]);
+ }else if (parentNode[childKey].length > 0 && index < 0) {
parentNode[childKey][parentNode[childKey].length - 1].isLastNode = false;
view.setNodeLineIcos(setting, parentNode[childKey][parentNode[childKey].length - 1]);
}
parentNode.isParent = true;
- parentNode[childKey] = parentNode[childKey].concat(nodes);
+ if (index < 0) {
+ parentNode[childKey] = parentNode[childKey].concat(nodes);
+ } else {
+ params = [index, 0].concat(nodes);
+ parentNode[childKey].splice.apply(parentNode[childKey], params);
+ }
},
addSelectedNode: function(setting, node) {
var root = data.getRoot(setting);
@@ -717,7 +731,7 @@
//点击时取消所有超链接效果
$('#'+event.data.treeId+' a').removeClass('focusNode');
//添加focusNode样式
-
+
$('#'+node.tId+'_a').addClass('focusNode');
var setting = data.getSetting(event.data.treeId),
@@ -821,10 +835,12 @@
},
//method of operate ztree dom
view = {
- addNodes: function(setting, parentNode, newNodes, isSilent) {
+ addNodes: function(setting, parentNode, newNodes, isSilent, index) {
if (setting.data.keep.leaf && parentNode && !parentNode.isParent) {
return;
}
+ if(!index && index !=0)
+ index = -1
if (!tools.isArray(newNodes)) {
newNodes = [newNodes];
}
@@ -845,14 +861,14 @@
});
}
- data.addNodesData(setting, parentNode, newNodes);
- view.createNodes(setting, parentNode.level + 1, newNodes, parentNode);
+ data.addNodesData(setting, parentNode, newNodes, index);
+ view.createNodes(setting, parentNode.level + 1, newNodes, parentNode, index);
if (!isSilent) {
view.expandCollapseParentNode(setting, parentNode, true);
}
} else {
- data.addNodesData(setting, data.getRoot(setting), newNodes);
- view.createNodes(setting, 0, newNodes, null);
+ data.addNodesData(setting, data.getRoot(setting), newNodes, index);
+ view.createNodes(setting, 0, newNodes, null, index);
}
},
appendNodes: function(setting, level, nodes, parentNode, initFlag, openFlag) {
@@ -949,7 +965,7 @@
var _tmpV = data.getRoot(setting)._ver;
if (setting.async.selfLoadFunc && typeof setting.async.selfLoadFunc == 'function'){
- setting.async.selfLoadFunc.apply(this, node)
+ setting.async.selfLoadFunc.apply(this, node)
}
else{
$.ajax({
@@ -975,7 +991,7 @@
} catch(err) {
newNodes = msg;
}
-
+
if (node) {
node.isAjaxing = null;
node.zAsync = true;
@@ -1032,8 +1048,8 @@
}
}
},
- createNodes: function(setting, level, nodes, parentNode) {
- if (!nodes || nodes.length == 0) return;
+ createNodes: function(setting, level, nodes, parentNode,index) {
+ /*if (!nodes || nodes.length == 0) return;
var root = data.getRoot(setting),
childKey = setting.data.key.children,
openFlag = !parentNode || parentNode.open || !!$$(parentNode[childKey][0], setting).get(0);
@@ -1047,6 +1063,37 @@
ulObj.append(zTreeHtml.join(''));
}
}
+ view.createNodeCallback(setting);*/
+
+ if (!nodes || nodes.length == 0) return;
+ var root = data.getRoot(setting),
+ childKey = setting.data.key.children,
+ openFlag = !parentNode || parentNode.open || !!$$(parentNode[childKey][0], setting).get(0);
+ root.createdNodes = [];
+ var zTreeHtml = view.appendNodes(setting, level, nodes, parentNode, true, openFlag),
+ parentObj, nextObj;
+
+ if (!parentNode) {
+ parentObj = setting.treeObj;
+ //setting.treeObj.append(zTreeHtml.join(''));
+ } else {
+ var ulObj = $$(parentNode, consts.id.UL, setting);
+ if (ulObj.get(0)) {
+ parentObj = ulObj;
+ //ulObj.append(zTreeHtml.join(''));
+ }
+ }
+ if (parentObj) {
+ if (index >= 0) {
+ nextObj = parentObj.children()[index];
+ }
+ if (index >= 0 && nextObj) {
+ $(nextObj).before(zTreeHtml.join(''));
+ } else {
+ parentObj.append(zTreeHtml.join(''));
+ }
+ }
+
view.createNodeCallback(setting);
},
destroy: function(setting) {
@@ -1195,9 +1242,9 @@
iconLength=18,
pLeft,
fontStyle = [];
-
+
if(setting.check.enable){
-
+
// pLeft=checkboxLength+parPaddingLeft+iconLength*(node.level+1) + 30 +'px';
// 因为a标签前面的图标设置成了absolute,所以忽略前面的宽度即减去21px
pLeft=checkboxLength+parPaddingLeft+iconLength*(node.level +1) +'px';
@@ -1210,7 +1257,7 @@
fontStyle.push('padding-left', ":",pLeft, ";");
fontStyle.push('margin-left', ":", '-'+mLeft, ";");
// 将a标签的宽度根据上级100%再加上层级之间的padding值算出
- parDomWidth='calc(100% + '+18*(node.level+1) +'px )';
+ parDomWidth='calc(100% + '+18*(node.level+1) +'px )';
fontStyle.push('min-width', ":", parDomWidth, ";");
for (var f in fontcss) {
@@ -1557,13 +1604,13 @@
var zTreeTools = {
setting : setting,
- addNodes : function(parentNode, newNodes, isSilent) {
+ addNodes : function(parentNode, newNodes, isSilent, index) {
if (!newNodes) return null;
if (!parentNode) parentNode = null;
if (parentNode && !parentNode.isParent && setting.data.keep.leaf) return null;
var xNewNodes = tools.clone(tools.isArray(newNodes)? newNodes: [newNodes]);
function addCallback() {
- view.addNodes(setting, parentNode, xNewNodes, (isSilent==true));
+ view.addNodes(setting, parentNode, xNewNodes, (isSilent==true), index);
}
if (tools.canAsync(setting, parentNode)) {

0 comments on commit d9db726

Please sign in to comment.