You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
var sprintf = function (str) {
var args = arguments,
flag = true,
i = 1;
str = str.replace(/%s/g, function () {
var arg = args[i++];
if (typeof arg === 'undefined') {
flag = false;
return '';
}
return arg;
});
return flag ? str : '';
};
var getFieldIndex = function (columns, field) {
var index = -1;
$.each(columns, function (i, column) {
if (column.field === field) {
index = i;
return false;
}
return true;
});
return index;
};
var escapeHTML = function (text) {
if (typeof text === 'string') {
return text
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''')
.replace(/`/g, '`');
}
return text;
};
var calculateObjectValue = function (self, name, args, defaultValue) {
var func = name;
if (typeof name === 'string') {
var names = name.split('.');
if (names.length > 1) {
func = window;
$.each(names, function (i, f) {
func = func[f];
});
} else {
func = window[name];
}
}
if (typeof func === 'object') {
return func;
}
if (typeof func === 'function') {
return func.apply(self, args);
}
if (!func && typeof name === 'string' && sprintf.apply(this, [name].concat(args))) {
return sprintf.apply(this, [name].concat(args));
}
return defaultValue;
};
var getItemField = function (item, field) {
var value = item;
if (typeof field !== 'string' || item.hasOwnProperty(field)) {
return item[field];
}
var props = field.split('.');
for (var p in props) {
value = value[props[p]];
}
return value;
};
var getParent = function (node, source, field) {
var data = [];
var items = $.grep(source, function (item, index) {
return node[that.options.treePid] == item[field];
});
$.each(items, function (index, item) {
data.splice(0, 0, item);
var child = getParent(item, source, field);
$.each(child, function (i, n) {
data.splice(0, 0, n);
});
});
return data;
};
var getChild = function (node, source, field, pidfield) {
var items = $.grep(source, function (item, index) {
return item[pidfield] == node[field];
});
return items;
};
var getAllChild = function (node, source, field, pidfield) {
var data = [];
var g=function(child){
$.each(child, function (i, n) {
data.push(n);
var subChild = getChild(n, source, field, pidfield);
if(subChild!=null && subChild.length>0){
g(subChild);
}
});
}
var child = getChild(node, source, field, pidfield);
g(child);
return data;
};
遇到了pid不能自定义的问题,我自己改了下源码。同时解决了某些节点的level判断有误,导致缩进有问题的bug。
/**
created by lise
@Date 2018-01-18
*/
(function ($) {
'use strict';
var sprintf = function (str) {
var args = arguments,
flag = true,
i = 1;
};
var getFieldIndex = function (columns, field) {
var index = -1;
};
var escapeHTML = function (text) {
if (typeof text === 'string') {
return text
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''')
.replace(/`/g, '`');
}
return text;
};
var calculateObjectValue = function (self, name, args, defaultValue) {
var func = name;
};
var getItemField = function (item, field) {
var value = item;
};
var getParent = function (node, source, field) {
var data = [];
var items = $.grep(source, function (item, index) {
return node[that.options.treePid] == item[field];
});
$.each(items, function (index, item) {
data.splice(0, 0, item);
var child = getParent(item, source, field);
$.each(child, function (i, n) {
data.splice(0, 0, n);
});
});
return data;
};
var getChild = function (node, source, field, pidfield) {
var items = $.grep(source, function (item, index) {
return item[pidfield] == node[field];
});
return items;
};
var getAllChild = function (node, source, field, pidfield) {
var data = [];
var g=function(child){
$.each(child, function (i, n) {
data.push(n);
var subChild = getChild(n, source, field, pidfield);
if(subChild!=null && subChild.length>0){
g(subChild);
}
});
}
var child = getChild(node, source, field, pidfield);
g(child);
return data;
};
//调用bootstrapTable组件的构造器得到对象
var BootstrapTable = $.fn.bootstrapTable.Constructor,
_initData = BootstrapTable.prototype.initData,
_initPagination = BootstrapTable.prototype.initPagination;
//重写bootstrapTable的initData方法
BootstrapTable.prototype.initData = function () {
_initData.apply(this, Array.prototype.slice.apply(arguments));
var that = this;
//初始化数据,添加level,isLeaf 属性
if (that.options.treeView && this.data.length > 0) {
var rows = [];
var roots = $.grep(this.data, function (row, index) {
return row[that.options.treePid] == null;
});
var g=function(child,lv){
var childLevel=lv;
$.each(child, function (i, n) {
n.level=childLevel;
if (that.options.treeCollapseAll) {
n.hidden = true;
}
var subChild = getChild(n, that.data, that.options.treeId,that.options.treePid);
if(subChild==null || subChild.length==0){
n.isLeaf=true;
}
rows.push(n);
if(subChild!=null && subChild.length>0){
g(subChild,lv+1);
}
});
}
$.each(roots, function (index, item) {
item.level=that.options.treeRootLevel;
var child = getChild(item, that.data, that.options.treeId,that.options.treePid);
if(child==null || child.length==0){
item.isLeaf=true;
}
rows.push(item);
g(child,item.level+1);
});
that.options.data = that.data = rows;
}
};
//重写bootstrapTable的initPagination方法
BootstrapTable.prototype.initPagination = function () {
//理论情况下,treegrid是不支持分页的,所以默认分页参数为false
if(this.options.treeView){
this.options.pagination = false;
}
//调用“父类”的“虚方法”
_initPagination.apply(this, Array.prototype.slice.apply(arguments));
};
//重写bootstrapTable的initRow方法
BootstrapTable.prototype.initRow = function(item, i, data, parentDom) {
var that=this,
key,
html = [],
style = {},
csses = [],
data_ = '',
attributes = {},
htmlAttributes = [];
};
//重写bootstrapTable的initBody方法
BootstrapTable.prototype.initBody = function (fixedScroll) {
var that = this,
html = [],
data = this.getData();
};
/**
*/
BootstrapTable.prototype.expandAllTree = function ()
{
var that=this;
var roots = $.grep(this.data, function (row, index) {
return row[that.options.treePid] == null;
});
$.each(roots, function (index, item) {
var child = getAllChild(item, that.options.data, that.options.treeId, that.options.treePid);
$.each(child, function (i, n) {
n.hidden=false;
});
});
that.initBody(true);
}
/**
*/
BootstrapTable.prototype.collapseAllTree = function ()
{
var that=this;
var roots = $.grep(this.data, function (row, index) {
return row[that.options.treePid] == null;
});
$.each(roots, function (index, item) {
var child = getAllChild(item, that.options.data, that.options.treeId, that.options.treePid);
$.each(child, function (i, n) {
n.hidden=true;
});
});
that.initBody(true);
}
//给组件增加默认参数列表
$.extend($ .fn.bootstrapTable.defaults, {
treeView: false,//treeView视图
treeField: "id",//treeView视图字段
treeId: "id",
treePid:"parentId",
treeRootLevel: 1,//根节点序号
treeCollapseAll: true,//是否全部展开,默认不展开
collapseIcon: "glyphicon glyphicon-chevron-right",//折叠样式
expandIcon: "glyphicon glyphicon-chevron-down",//展开样式
leafIcon:"glyphicon glyphicon-leaf"//叶子节点样式
});
$.fn.bootstrapTable.methods.push('expandAllTree', 'collapseAllTree');
})(jQuery);
The text was updated successfully, but these errors were encountered: