tree.table.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /**
  2. * 初始化 Tree Table 的封装
  3. *
  4. * @author cyf
  5. */
  6. (function () {
  7. var TreeTable = function (tableId, url, columns) {
  8. this.btInstance = null; //jquery和bootstrapTreeTable绑定的对象
  9. this.bstableId = tableId;
  10. this.url = url;
  11. this.method = "GET";
  12. this.columns = columns;
  13. this.data = {};// ajax的参数
  14. this.expandColumn = null;// 展开显示的列
  15. this.id = 'menuId';// 选取记录返回的值
  16. this.code = 'menuId';// 用于设置父子关系
  17. this.parentCode = 'parentId';// 用于设置父子关系
  18. this.expandAll = false;// 是否默认全部展开
  19. this.toolbarId = tableId + "Toolbar";
  20. this.height = 430;
  21. };
  22. TreeTable.prototype = {
  23. /**
  24. * 初始化bootstrap table
  25. */
  26. init: function () {
  27. var tableId = this.bstableId;
  28. this.btInstance =
  29. $('#' + tableId).bootstrapTreeTable({
  30. id: this.id,// 选取记录返回的值
  31. code: this.code,// 用于设置父子关系
  32. parentCode: this.parentCode,// 用于设置父子关系
  33. rootCodeValue: this.rootCodeValue,//设置根节点code值----可指定根节点,默认为null,"",0,"0"
  34. type: this.method, //请求数据的ajax类型
  35. url: this.url, //请求数据的ajax的url
  36. ajaxParams: this.data, //请求数据的ajax的data属性
  37. expandColumn: this.expandColumn,//在哪一列上面显示展开按钮,从0开始
  38. striped: true, //是否各行渐变色
  39. expandAll: this.expandAll, //是否全部展开
  40. columns: this.columns, //列数组
  41. toolbar: "#" + this.toolbarId,//顶部工具条
  42. height: this.height,
  43. });
  44. return this;
  45. },
  46. /**
  47. * 设置在哪一列上面显示展开按钮,从0开始
  48. */
  49. setExpandColumn: function (expandColumn) {
  50. this.expandColumn = expandColumn;
  51. },
  52. /**
  53. * 设置记录返回的id值
  54. */
  55. setIdField: function (id) {
  56. this.id = id;
  57. },
  58. /**
  59. * 设置记录分级的字段
  60. */
  61. setCodeField: function (code) {
  62. this.code = code;
  63. },
  64. /**
  65. * 设置记录分级的父级字段
  66. */
  67. setParentCodeField: function (parentCode) {
  68. this.parentCode = parentCode;
  69. },
  70. /**
  71. * 设置根节点code值----可指定根节点,默认为null,"",0,"0"
  72. */
  73. setRootCodeValue: function (rootCodeValue) {
  74. this.rootCodeValue = rootCodeValue;
  75. },
  76. /**
  77. * 设置是否默认全部展开
  78. */
  79. setExpandAll: function (expandAll) {
  80. this.expandAll = expandAll;
  81. },
  82. /**
  83. * 设置表格高度
  84. */
  85. setHeight: function (height) {
  86. this.height = height;
  87. },
  88. /**
  89. * 设置ajax post请求时候附带的参数
  90. */
  91. set: function (key, value) {
  92. if (typeof key == "object") {
  93. for (var i in key) {
  94. if (typeof i == "function")
  95. continue;
  96. this.data[i] = key[i];
  97. }
  98. } else {
  99. this.data[key] = (typeof value == "undefined") ? $("#" + key).val() : value;
  100. }
  101. return this;
  102. },
  103. /**
  104. * 设置ajax get请求时候附带的参数
  105. */
  106. setData: function (data) {
  107. this.data = data;
  108. return this;
  109. },
  110. /**
  111. * 清空ajax post请求参数
  112. */
  113. clear: function () {
  114. this.data = {};
  115. return this;
  116. },
  117. /**
  118. * 刷新表格
  119. */
  120. refresh: function (parms) {
  121. if (typeof parms != "undefined") {
  122. this.btInstance.bootstrapTreeTable('refresh', parms.query);// 为了兼容bootstrap-table的写法
  123. } else {
  124. this.btInstance.bootstrapTreeTable('refresh');
  125. }
  126. },
  127. /**
  128. * 设置高度
  129. */
  130. resetHeight: function (parms) {
  131. if (typeof parms != "undefined") {
  132. this.btInstance.bootstrapTreeTable('resetHeight', parms.height);// 为了兼容bootstrap-table的写法
  133. } else {
  134. this.btInstance.bootstrapTreeTable('resetHeight');
  135. }
  136. },
  137. /**
  138. * 获取选中行
  139. */
  140. getSelectedRow: function () {
  141. return this.btInstance.bootstrapTreeTable('getSelections');
  142. }
  143. };
  144. window.TreeTable = TreeTable;
  145. }());