kendo-table-object.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /**
  2. * 初始化 kendo ui Table 的封装
  3. * 约定:toolbar的id为 (kdtableId + "Toolbar")
  4. * @author LiaoYun 2019-09-17 945505778@qq.com
  5. */
  6. (function () {
  7. var KDTable = function (kdtableId, url, columns) {
  8. this.kdtableId = kdtableId;
  9. this.url = Feng.ctxPath + url;
  10. this.method = "post";
  11. this.columns = columns;
  12. this.height = ""; //默认表格高度665
  13. this.scrollable = true;
  14. this.selectable = true; //是否允许选择
  15. this.groupable = false; //是否允许分组
  16. this.serverPaging = true; //服务端分页
  17. this.sortable = false; //是否允许排序
  18. this.serverSorting = false; //服务端排序
  19. this.filterable = false; //是否允许过滤
  20. this.serverFiltering = false; //服务端过滤
  21. this.pageSize = 10; //每次分页加载18条数据
  22. this.resizable = true; //表格宽度允许拖动
  23. this.reorderable = true; //是否允许拖动列来改变列的显示顺序
  24. this.columnMenu = true;
  25. this.alwaysVisible = false; //是否在列表数据没有达到分页时显示分页组件
  26. this.readData = {}; //后台传递的自定义参数
  27. this.queryParams = {}; //固定的查询条件
  28. this.fields = {}; //data type of the field {number|string|boolean|date} default is string
  29. if (columns!=null && columns.length>0) {
  30. // 添加序号列
  31. columns.unshift({field: "rowNumber", title: "序号", width:"80px", template: "<span class='row-number'></span>"});
  32. for(var i=0; i<columns.length; i++){
  33. var temp = columns[i];
  34. if (!temp) {
  35. break;
  36. }
  37. if (temp.type!=null && temp.type!='' && typeof(temp)!="undefined") {
  38. this.fields[temp.field] = {'type': temp.type};
  39. } else {
  40. this.fields[temp.field] = {'type': 'string'};
  41. }
  42. }
  43. }
  44. };
  45. KDTable.prototype = {
  46. /**
  47. * 初始化kendo table
  48. */
  49. init: function() {
  50. var tableId = this.kdtableId;
  51. var me = this;
  52. this.btInstance = $("#" + tableId).kendoGrid({
  53. dataSource:{
  54. transport:{
  55. read: {
  56. url: this.url, //The remote service url.
  57. type: this.method, //The request type.
  58. dataType: "json", //The data type of the returned result.
  59. contentType: "application/x-www-form-urlencoded",
  60. data: this.readData //The additional custom parameters sent to the remote service.
  61. },
  62. parameterMap: function(options, operation){
  63. console.log("====> 查询数据 ======");
  64. console.log(options);
  65. if (operation == "read") {
  66. var parameter = {};
  67. $.each(options, function(name, value){
  68. if (name === 'take') {
  69. parameter.limit = value;
  70. } else if (name === 'skip') {
  71. parameter.offset = value
  72. } else if (name === 'sort') {
  73. parameter.sort = value[0].field;
  74. parameter.order = value[0].dir
  75. } else if (name === 'page') {
  76. } else if (name === 'pageSize'){
  77. }else {
  78. parameter[name] = value;
  79. }
  80. });
  81. //设置固定的查询条件
  82. if (me.queryParams) {
  83. $.each(me.queryParams, function(name, value){
  84. parameter[name] = value;
  85. });
  86. }
  87. // 如果有动态查询条件
  88. if (me.readData) {
  89. $.each(me.readData, function(name, value){
  90. parameter[name] = value;
  91. });
  92. }
  93. return parameter;
  94. }
  95. }
  96. },
  97. schema: {
  98. data: "rows",
  99. total: "total"
  100. },
  101. pageSize: this.pageSize,
  102. serverPaging: this.serverPaging,
  103. sortable: this.sortable,
  104. serverSorting: this.serverSorting,
  105. alwaysVisible: this.alwaysVisible
  106. },
  107. // 设置分页的序号
  108. dataBound: function() {
  109. var rows = this.items();
  110. var page = this.pager.page() - 1;
  111. var pagesize = this.pager.pageSize();
  112. $(rows).each(function () {
  113. var index = $(this).index() + 1 + page * pagesize;
  114. var rowLabel = $(this).find(".row-number");
  115. if (rowLabel && rowLabel.length>0) {
  116. $(rowLabel).html(index);
  117. }
  118. });
  119. },
  120. columns: this.columns,
  121. fields: this.fields,
  122. height: this.height,
  123. scrollable: this.scrollable,
  124. selectable: this.selectable,
  125. groupable: this.groupable,
  126. filterable: this.filterable,
  127. serverFiltering: this.serverFiltering,
  128. resizable: this.resizable,
  129. columnMenu: this.columnMenu,
  130. reorderable: this.reorderable,
  131. sortable: this.sortable,
  132. serverSorting: this.serverSorting,
  133. pageable: {
  134. refresh: true,
  135. pageSizes: [10, 50, 100, 500, 1000],
  136. buttonCount: 5
  137. }
  138. });
  139. return this;
  140. },
  141. /**
  142. * 向后台传递的自定义参数
  143. * @param param
  144. */
  145. setQueryParams: function (param) {
  146. this.readData = param;
  147. },
  148. clearQueryParams: function() {
  149. this.readData = {};
  150. },
  151. /**
  152. * 设置分页 server | client
  153. */
  154. setPaginationType: function(val) {
  155. if (val === "server") {
  156. this.serverPaging = true;
  157. } else if (val === "client") {
  158. this.serverPaging = false;
  159. }
  160. },
  161. /**
  162. * 设置数据过滤方式 server | client
  163. */
  164. setFilterType: function(val) {
  165. if (val === "server") {
  166. this.filterable = true;
  167. this.serverFiltering = true;
  168. } else if (val === "client") {
  169. this.filterable = true;
  170. }
  171. },
  172. /**
  173. * 设置排序 server | client
  174. */
  175. setSortType: function(val) {
  176. if (val === "server") {
  177. this.sortable = true;
  178. this.serverSorting = true;
  179. } else if (val === "client") {
  180. this.sortable = true;
  181. }
  182. },
  183. /**
  184. * 选择方式 true or false or 'row' or 'multiple row' or 'cell' or 'multiple cell'
  185. */
  186. setSelectable: function(bl) {
  187. this.selectable = bl;
  188. },
  189. getSelections: function() {
  190. var grid = $("#"+this.kdtableId+",.k-grid-content").data("kendoGrid");
  191. // var grid = $("#"+this.kdtableId).data("kendoGrid");
  192. var selected = [];
  193. var rows = grid.select();
  194. // console.log(rows);
  195. for(var i=0; i<rows.length; i++){
  196. var temp = rows.eq(i);
  197. var className = temp.parent().parent().parent().attr("class");
  198. // console.log("class=" + temp.parent().parent().parent().attr("class"));
  199. if (className === 'k-grid-content-locked') {
  200. continue;
  201. }
  202. selected.push(grid.dataItem(rows[i]))
  203. }
  204. return selected;
  205. },
  206. refresh: function(val) {
  207. // 清空历史动态查询条件
  208. this.readData = {};
  209. var grid = $("#"+this.kdtableId).data("kendoGrid");
  210. if (val && val.query) {
  211. var filter = [];
  212. var flag = false;
  213. for (var key of Object.keys(val.query)) {
  214. // console.log(key + ': ' + val.query[key]);
  215. var value = val.query[key];
  216. if (value!=null && value!='' && typeof(value)!="undefined") {
  217. this.readData[key] = value;
  218. flag = true;
  219. }else {
  220. delete val.query[key];
  221. }
  222. }
  223. if (flag && this.serverSorting) {
  224. val.query.page = 1;
  225. val.query.pageSize = this.pageSize;
  226. val.query.take = this.pageSize;
  227. val.query.skip = 0;
  228. grid.dataSource.query(val.query);
  229. } else {
  230. grid.dataSource.read();
  231. }
  232. } else {
  233. grid.dataSource.read();
  234. }
  235. },
  236. /**
  237. * 设置固定的查询条件 {name1:value1, name2:value2}
  238. */
  239. setConstantQueryParams: function(val) {
  240. if (val != null) {
  241. var temp = {};
  242. $.each(val, function(name, value){
  243. if (value!=null && value!='' && typeof(value)!="undefined") {
  244. temp[name] = value;
  245. }
  246. });
  247. this.queryParams = temp;
  248. }
  249. },
  250. /**
  251. * 设置每页查询条数
  252. */
  253. pageSize: function(val) {
  254. this.pageSize = val;
  255. }
  256. };
  257. window.KDTable = KDTable;
  258. }());