//参数为需要进行过滤的tree和模糊查询关键字 function searchDataSet(value) { //获取tree数据 var treeData = new Array(); var newTreeData = new Array(); $.ajax({ url: '/core/dataset/getTree/', method: 'get', async: false, success: function (data) { treeData = data; } }); //将数据进行过滤 if (treeData) { newTreeData = getNewTreeData(treeData, value); } //给tree加载新数据 $("#businessTree").epaitree({ loadFilter: function (data) { if (value) { return newTreeData; } else { return data; } } }); } function getNewTreeData(treeData, value) { if (!treeData) { return null; } var newTreeData = new Array(); var node = null; var children = null; var text = ""; for (var i = 0; i < treeData.length; i++) {//多个根节点开始遍历 node = treeData[i]; if (node.children) { children = node.children; } text = node.name; if (text.indexOf(value) > -1) { newTreeData.push(node); continue; } else { if (children) { var newNodes = getNewTreeData(node.children, value); if (newNodes.length > 0) { node.children = newNodes; newTreeData.push(node); } } } } return newTreeData; }