tree_table.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  1. /*
  2. * Created by MaiXinRong on 2017/2/9.
  3. * 项目管理 专用树结构
  4. */
  5. (function($) {
  6. var _setting = {
  7. tree: {
  8. id: 'id',
  9. pid: 'parentId',
  10. nid: 'nextId',
  11. btnColumn: 1,
  12. iconCol: 'projType',
  13. nullId: -1
  14. },
  15. columns: [
  16. {
  17. head: '工程列表',
  18. data: 'name',
  19. static: false,
  20. width: '100%',
  21. event: {
  22. getText: null,
  23. getIcon: null
  24. }
  25. }
  26. ],
  27. dataTemp: {
  28. id: -1,
  29. parentId: -1,
  30. nextId: -1
  31. },
  32. viewEvent: {
  33. beforeSelect: null,
  34. onSelectNode: null
  35. }
  36. };
  37. var Node = (function () {
  38. function Node(tree, data) {
  39. this.parent = null;
  40. this.nextSibling = null;
  41. this.children = [];
  42. this.tree = tree;
  43. this.data = data;
  44. this.setting = tree.setting;
  45. this.expanded = true;
  46. this.row = null;
  47. this.expandBtn = null;
  48. }
  49. Node.prototype.firstChild = function() {
  50. return (this.children.length === 0) ? null : this.children[0];
  51. };
  52. Node.prototype.lastChild = function () {
  53. return (this.children.length === 0) ? null : this.children[this.children.length - 1];
  54. }
  55. Node.prototype.deepestRow = function () {
  56. return (this.children.length === 0) ? this.row : this.lastChild().deepestRow();
  57. }
  58. Node.prototype.addChild = function (child, childNext) {
  59. if (childNext){
  60. this.children.push(child);
  61. } else {
  62. if (this.childIndex(childNext) > -1){
  63. this.children.splice(this.childIndex(childNext) - 1, 0, child);
  64. } else {
  65. this.children.push(child);
  66. }
  67. }
  68. return child;
  69. };
  70. Node.prototype.childIndex = function (child) {
  71. return this.children.indexOf(child);
  72. };
  73. Node.prototype.depth = function () {
  74. return this.parent ? this.parent.depth() + 1 : 0;
  75. };
  76. Node.prototype.domId = function () {
  77. return this.data ? this.tree.domId + '_' + this.data[this.setting.tree.id] : '';
  78. };
  79. Node.prototype.expand = function (bool) {
  80. this.expanded = bool;
  81. _view._refreshTreeBtn(this);
  82. if (this.expanded) {
  83. _view._showNodes(this.children);
  84. } else {
  85. _view._hideNodes(this.children);
  86. }
  87. };
  88. Node.prototype.preSibling = function () {
  89. var iIndex = this.parent.childIndex(this);
  90. if (iIndex === -1){
  91. return null;
  92. } else {
  93. return iIndex > 0 ? this.parent.children[iIndex-1] : null;
  94. }
  95. };
  96. Node.prototype.setParent = function (parent) {
  97. if (parent && this.parent !== parent) {
  98. this.parent = parent;
  99. this.data[this.setting.tree.pid] = this.pid();
  100. }
  101. };
  102. Node.prototype.setNextSibling = function (nextSibling) {
  103. if (this.nextSibling !== nextSibling) {
  104. this.nextSibling = nextSibling;
  105. this.data[this.setting.tree.nid] = this.nid();
  106. }
  107. }
  108. Node.prototype.id = function () {
  109. return this.data ? this.data[this.setting.tree.id] : -1;
  110. };
  111. Node.prototype.pid = function () {
  112. return this.parent ? this.parent.id() : -1;
  113. };
  114. Node.prototype.nid = function () {
  115. return this.nextSibling ? this.nextSibling.id() : -1;
  116. };
  117. Node.prototype.propertyJoin = function (dataName) {
  118. return this.parent ? this.parent.propertyJoin(dataName) + ';' + this.data[dataName] : this.data[dataName];
  119. }
  120. return Node;
  121. })();
  122. var Tree = (function () {
  123. function Tree(obj, setting) {
  124. this._root = new Node(this);
  125. this.treeObj = obj;
  126. this.domId = obj.attr('id');
  127. this.treeHeadObj = _view._makeTableHead(this.treeObj, setting);
  128. this.treeBodyObj = _view._makeTableBody(this.treeObj);
  129. this.setting = setting;
  130. var _maxNodeId = 0;
  131. this.newNodeId = function (id) {
  132. if (arguments.length > 0){
  133. _maxNodeId = (id > _maxNodeId) ? id : _maxNodeId;
  134. } else {
  135. _maxNodeId += 1;
  136. return _maxNodeId;
  137. }
  138. };
  139. this.maxNodeId = function (){
  140. return _maxNodeId;
  141. }
  142. };
  143. Tree.prototype.firstNode = function (){
  144. return this._root.firstChild();
  145. };
  146. Tree.prototype.traverseDF = function(callback){
  147. var recurse = function (node) {
  148. var i;
  149. if (node !== this._root) {
  150. callback(node);
  151. }
  152. for (i = 0; i < node.children.length; i++){
  153. recurse(node.children[i]);
  154. }
  155. }
  156. recurse(this._root);
  157. };
  158. Tree.prototype.findNode = function (id){
  159. var treenode = null,
  160. callback = function (node) {
  161. if (node.data && node.data[node.setting.tree.id] === id){
  162. treenode = node;
  163. }
  164. };
  165. this.traverseDF.call(this, callback);
  166. return treenode;
  167. };
  168. Tree.prototype.findNodeByDomId = function (domId) {
  169. var treenode = null,
  170. callback = function (node) {
  171. if (node.domId === domId) {
  172. treenode = node;
  173. }
  174. };
  175. this.traverseDF.call(this, callback);
  176. return treenode;
  177. };
  178. Tree.prototype.removeNode = function (node) {
  179. var iIndex;
  180. if (node) {
  181. iIndex = node.parent.childIndex(node);
  182. if (iIndex > 0) {
  183. node.parent.children[iIndex - 1].setNextSibling(node.nextSibling);
  184. }
  185. node.parent.children.splice(iIndex, 1);
  186. }
  187. _view._removeNodesRowDom([node]);
  188. };
  189. Tree.prototype.loadData = function (arrData) {
  190. var i, that = this;
  191. var createTempNode = function (id, setting) {
  192. var tempData = {};
  193. tempData[setting.tree.id] = id;
  194. return new Node(that, tempData);
  195. };
  196. var loadNode = function (data, setting) {
  197. var node = that.findNode(data[setting.tree.id]) || null,
  198. parent = that.findNode(data[setting.tree.pid]) || null,
  199. next = that.findNode(data[setting.tree.nid]) || null,
  200. tempData;
  201. if (!node) {
  202. node = new Node(that, data);
  203. }
  204. node.data = data;
  205. that.newNodeId(node.id());
  206. if (!parent){
  207. if (data[setting.tree.pid] === setting.tree.nullId) {
  208. parent = that._root;
  209. } else {
  210. parent = createTempNode(data[setting.tree.pid], setting);
  211. parent.parent = that._root;
  212. that._root.children.push(parent);
  213. }
  214. }
  215. if (node.parent && node.parent !== parent) {
  216. node.parent.children.splice(node.parent.childIndex(node), 1);
  217. }
  218. node.parent = parent;
  219. if (!next && data[setting.tree.nid] !== setting.tree.nullId) {
  220. next = createTempNode(data[setting.tree.nid], setting);
  221. next.parent = parent;
  222. }
  223. node.nextSibling = next;
  224. if (parent.childIndex(node) === -1){
  225. if (!next){
  226. parent.children.push(node);
  227. } else if (parent.childIndex(next) === -1){
  228. parent.children.push(node);
  229. parent.children.push(next);
  230. } else {
  231. parent.children.splice(parent.childIndex(next), 0, node);
  232. }
  233. }
  234. };
  235. for (i = 0; i < arrData.length; i++){
  236. loadNode(arrData[i], this.setting);
  237. }
  238. };
  239. Tree.prototype.refreshNodesDom = function (nodes, recurse){
  240. var that = this;
  241. nodes.forEach(function (node) {
  242. if (node.row) {
  243. $('td', node.row).remove();
  244. _view._makeTableRowCells(node.row, node);
  245. } else if (node !== that._root) {
  246. _view._makeRowDom(that.treeBodyObj, node);
  247. }
  248. if (recurse) {
  249. that.refreshNodesDom(node.children, recurse);
  250. }
  251. })
  252. };
  253. Tree.prototype.refreshTreeDom = function () {
  254. this.refreshNodesDom(this._root.children, true);
  255. };
  256. Tree.prototype.addNodeData = function (data, parent, nextSibling) {
  257. var node = null;
  258. var pNode = parent ? parent : this._root;
  259. if (!nextSibling || (nextSibling.parent === pNode && pNode.childIndex(nextSibling) > -1)) {
  260. node = new Node(this, data);
  261. this.newNodeId(data[this.setting.tree.id]);
  262. node.row = _view._makeRowDom(this.treeBodyObj, node);
  263. this.move(node, pNode, nextSibling);
  264. }
  265. return node;
  266. }
  267. Tree.prototype.move = function(node, parent, nextSibling) {
  268. var iIndex = -1, pre;
  269. if (parent && (!nextSibling || (nextSibling.parent === parent && parent.childIndex(nextSibling) > -1))) {
  270. if (node) {
  271. if (node.parent) {
  272. iIndex = node.parent.childIndex(node);
  273. if (iIndex > 0) {
  274. node.parent.children[iIndex - 1].setNextSibling(node.nextSibling);
  275. }
  276. node.parent.children.splice(iIndex, 1);
  277. this.refreshNodesDom([node.parent], false);
  278. }
  279. if (nextSibling) {
  280. iIndex = parent.childIndex(nextSibling);
  281. if (iIndex > 0){
  282. pre = parent.children[iIndex - 1];
  283. pre.setNextSibling(node);
  284. parent.children.splice(iIndex - 1, 0, node);
  285. } else {
  286. parent.children.splice(0, 0, node);
  287. }
  288. } else {
  289. if (parent.children.length > 0){
  290. pre = parent.lastChild();
  291. pre.setNextSibling(node);
  292. }
  293. parent.children.push(node);
  294. }
  295. node.setParent(parent);
  296. node.setNextSibling(nextSibling);
  297. if (node.row) {
  298. if (pre) {
  299. _view._moveRowDom(node, pre.deepestRow());
  300. } else if (parent.id() !== setting.tree.nullId) {
  301. _view._moveRowDom(node, parent.row);
  302. } else if (nextSibling) {
  303. _view._moveRowDomBefore(node, nextSibling.row);
  304. }
  305. }
  306. if (node.parent.row) {
  307. this.refreshNodesDom([node.parent], false);
  308. }
  309. }
  310. } else {
  311. this.e.throw('Error: information of moving node has mistake.');
  312. }
  313. };
  314. Tree.prototype.selected = (function () {
  315. var selectNode = null;
  316. var select = function (node) {
  317. if (arguments.length === 0) {
  318. return selectNode;
  319. } else {
  320. if (node) {
  321. if (selectNode && selectNode.row) {
  322. selectNode.row.removeClass('table-active');
  323. }
  324. selectNode = node;
  325. node.row.addClass('table-active');
  326. }
  327. }
  328. }
  329. return select;
  330. })();
  331. return Tree;
  332. })();
  333. _data = {
  334. clone: function (obj) {
  335. if (obj === null) return null;
  336. var o = _data.isArray(obj) ? [] : {};
  337. for (var i in obj) {
  338. o[i] = (obj[i] instanceof Date) ? new Date(obj[i].getTime()) : (typeof obj[i] === "object" ? _data.clone(obj[i]) : obj[i]);
  339. }
  340. return o;
  341. },
  342. isArray: function (arr) {
  343. return Object.prototype.toString.apply(arr) === "[object Array]";
  344. }
  345. },
  346. _event = {
  347. _selectProj: function (node) {
  348. if (node.setting.viewEvent.beforeSelect) {
  349. node.setting.viewEvent.beforeSelect(node.tree.selected());
  350. }
  351. node.tree.selected(node);
  352. if (node.setting.viewEvent.onSelectNode) {
  353. node.setting.viewEvent.onSelectNode(node);
  354. }
  355. }
  356. }
  357. _view = {
  358. _makeTableHeadCell: function (obj, col){
  359. var th;
  360. th = $('<th>');
  361. th.attr('width', col.width);
  362. th.text(col.head);
  363. obj.append(th);
  364. },
  365. _makeTableHead: function (obj, setting){
  366. var thead, tr, i;
  367. thead = $('<thead>');
  368. tr = $('<tr>');
  369. for (i = 0; i < setting.columns.length; i++){
  370. _view._makeTableHeadCell(tr, setting.columns[i]);
  371. }
  372. thead.append(tr);
  373. obj.append(thead);
  374. return thead;
  375. },
  376. _makeTableBody: function (obj){
  377. var tbody;
  378. tbody = $('<tbody>');
  379. obj.append(tbody);
  380. return tbody;
  381. },
  382. _makeTableRowCell: function (obj, node, columns, index) {
  383. var html = [], td, i, url;
  384. html.push('<td ');
  385. if (index === node.setting.tree.btnColumn){
  386. html.push('class=', 'in-', node.depth().toString(), '>');
  387. if (node.children.length !== 0) {
  388. html.push('<a href="#" class="tree-open" title="收起"><i class="fa fa-minus-square-o mr-1"></i></a>');
  389. } else {
  390. html.push('<a href="#" class="tree-open" title="收起"><i></i></a>');
  391. }
  392. } else {
  393. html.push('>');
  394. }
  395. if (columns.event.getIcon) {
  396. columns.event.getIcon(html, node);
  397. }
  398. if (columns.name !== '') {
  399. if (columns.event.getText) {
  400. columns.event.getText(html, node, node.data[columns.data]);
  401. } else {
  402. html.push(node.data[columns.name]);
  403. }
  404. }
  405. html.push('</td>');
  406. td = $(html.join(''));
  407. if (index === node.setting.tree.btnColumn) {
  408. node.expandBtn = $('.tree-open>.fa', td);
  409. node.expandBtn.bind('click', {node: node}, function(e){
  410. e.data.node.expand(!e.data.node.expanded);
  411. });
  412. node.icon = $('.tree-icon', td);
  413. }
  414. if (columns.event.tdBindEvent) {
  415. columns.event.tdBindEvent(td, node);
  416. }
  417. obj.append(td);
  418. return td;
  419. },
  420. _makeTableRowCells: function (obj, node) {
  421. for (i = 0; i < node.setting.columns.length; i++){
  422. _view._makeTableRowCell(obj, node, node.setting.columns[i], i);
  423. }
  424. },
  425. _makeRowDom: function (obj, node) {
  426. var tr, i;
  427. tr = $('<tr>');
  428. obj.append(tr);
  429. tr.attr('id', node.domId());
  430. node.row = tr;
  431. _view._makeTableRowCells(tr, node);
  432. tr.click(function(){
  433. _event._selectProj(node);
  434. });
  435. /*$(tr).draggable({
  436. helper: "clone",
  437. opacity: .75,
  438. refreshPositions: true, // Performance?
  439. revert: "invalid",
  440. revertDuration: 300,
  441. scroll: true
  442. });
  443. tr.droppable({
  444. drop: function (e, ui) {
  445. var target = node.tree.findNodeByDomId($(this).attr('id')),
  446. drag = node.tree.findNodeByDomId($(ui).attr('id'));
  447. tree.move(drag, target);
  448. }
  449. }); */
  450. return tr;
  451. },
  452. _moveRowDomBefore: function (node, destination) {
  453. var moveNodesRowDomBefore = function (nodes) {
  454. nodes.forEach(function (node) {
  455. node.row.insertBefore(destination);
  456. moveNodesRowDomBefore(node.children);
  457. });
  458. }
  459. moveNodesRowDomBefore([node]);
  460. _view._refreshNodesLevelCss([node]);
  461. },
  462. _moveRowDom: function (node, destination) {
  463. var pre = destination,
  464. moveNodeRowDom = function (node) {
  465. node.row.insertAfter(pre);
  466. pre = node.row;
  467. moveChildrenRowDom(node);
  468. },
  469. moveChildrenRowDom = function(node){
  470. var i;
  471. for (i = 0; i < node.children.length; i++){
  472. moveNodeRowDom(node.children[i]);
  473. }
  474. };
  475. moveNodeRowDom(node);
  476. _view._refreshNodesLevelCss([node]);
  477. },
  478. _refreshTreeBtn: function (node) {
  479. var i;
  480. if (node.children.length === 0) {
  481. node.expandBtn.hide();
  482. } else {
  483. node.expandBtn.show();
  484. if (node.expanded) {
  485. node.expandBtn.removeClass('fa-plus-square-o');
  486. node.expandBtn.addClass('fa-minus-square-o');
  487. } else {
  488. node.expandBtn.removeClass('fa-minus-square-o');
  489. node.expandBtn.addClass('fa-plus-square-o');
  490. }
  491. }
  492. },
  493. _refreshNodesLevelCss: function (nodes) {
  494. nodes.forEach(function(node){
  495. var td = $('td:eq(' + node.setting.tree.btnColumn.toString() + ')', node.row);
  496. td.attr('class', 'in-'+ node.depth().toString());
  497. _view._refreshNodesLevelCss(node.children);
  498. })
  499. },
  500. _hideNodes: function (nodes) {
  501. var i, node;
  502. for (i = 0; i < nodes.length; i++){
  503. node = nodes[i];
  504. node.row.hide();
  505. _view._hideNodes(node.children);
  506. }
  507. },
  508. _showNodes: function (nodes) {
  509. var i, node;
  510. for (i = 0; i < nodes.length; i++){
  511. node = nodes[i];
  512. node.row.show();
  513. if (node.expanded) {
  514. _view._showNodes(node.children);
  515. }
  516. }
  517. },
  518. _removeNodesRowDom: function (nodes) {
  519. var i, node;
  520. for (i = 0; i < nodes.length; i++){
  521. node = nodes[i];
  522. _view._removeNodesRowDom(node.children);
  523. if (node.row) {
  524. node.row.remove();
  525. }
  526. }
  527. }
  528. };
  529. $.fn.treeTable = {
  530. init: function(obj, setting, arrData){
  531. var _treeSetting, _tree;
  532. _treeSetting = _data.clone(_setting);
  533. $.extend(true, _treeSetting, setting)
  534. var _tree = new Tree(obj, _treeSetting);
  535. _tree.loadData(arrData);
  536. _tree.refreshTreeDom();
  537. return _tree;
  538. }
  539. }
  540. })(jQuery);