项目中使用BootstrapTable-treeview 实现了一个组织结构的modal,但是客户有要求希望可以在页面去控制整个树形表格的展开层级,我在treeview的基础上增加了一个openTo方法去实现整个功能。
bootstraptable-treeview.js
//展开指定层级 updateByNio
BootstrapTable.prototype.openTo = function (nid) {
this.collapseAllTree();
this.nId = nid;
data = this.data;
var that = this;
var roots = $.grep(this.data, function (row, index) {
return row.parentId == null;
});
for (var i = 0; i < this.nId - 1; i++){
var child = null;
if (i === 0) {
var root = roots;
child = getChild(root[i], data, that.options.treeId);
$.each(child, function (i, n) {
n.hidden = false;
});
}
if (root.length != 1) {
console.log(root.length);
$.each(root, function (index, item) {
child = getChild(item, data, that.options.treeId);
$.each(child, function (i, n) {
n.hidden = false;
});
})
}
root = child;
}
that.initBody(true);
}
增加了方法后是不能直接调用的,需要将该方法名加到allowedMethods数组中。
bootstraptable.js
var allowedMethods = [
'getOptions','openTo',
页面调用:
$('#zou').click(function () {
let value = $('input[name="level"]').val();
$('.treeTable').bootstrapTable('openTo', value);
});
源码:https://pan.baidu.com/s/1eG87aJbwi_yOZGtfXnrWOA
j8qt