demo2.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  5. <link rel="stylesheet" type="text/css" href="../css/gm.css">
  6. <script type="text/javascript" src="../js/gm.js"></script>
  7. <title>GridManager:使用静态数据渲染</title>
  8. <style>
  9. html, body{
  10. width: 100%;
  11. height: 100%;
  12. overflow-x:hidden;
  13. margin: 0;
  14. padding: 0;
  15. }
  16. h2{
  17. font-size:22px;
  18. padding:10px 30px;
  19. color:#333;
  20. }
  21. p{
  22. font-size:14px;
  23. padding:10px 30px;
  24. color:#333;
  25. text-indent:2em;
  26. margin: 0;
  27. }
  28. hr{
  29. margin-top: 18px;
  30. margin-bottom: 18px;
  31. border: 0;
  32. border-top: 1px solid #eee;
  33. }
  34. .plugin-action{
  35. display: inline-block;
  36. color: steelblue;
  37. margin-right: 10px;
  38. cursor: pointer;
  39. text-decoration: none;
  40. }
  41. .bottom-bar{
  42. background: #f8f8f8;
  43. padding: 10px;
  44. margin-top: 10px;
  45. }
  46. .bottom-bar button{
  47. padding: 5px 20px;
  48. margin-right: 10px;
  49. }
  50. .bottom-bar a{
  51. font-size: 12px;
  52. padding: 5px 20px;
  53. margin-right: 10px;
  54. }
  55. </style>
  56. </head>
  57. <body>
  58. <table grid-manager="cccc"></table>
  59. <div class="bottom-bar">
  60. <button id="init-gm" disabled>init</button>
  61. <button id="destroy-gm" disabled>destroy</button>
  62. <button id="reset-gm" disabled>重置数据</button>
  63. <button id="update-tree" disabled>切换展开状态</button>
  64. <a href="https://github.com/baukh789/GridManager/blob/master/src/demo/demo2.html" target="_blank">查看源码</a>
  65. </div>
  66. <script>
  67. // 静态数据
  68. var ajaxData1 = {
  69. "data":[
  70. {
  71. "id": 1,
  72. "name": "baukh1",
  73. "age": "30",
  74. "createDate": "2015-03-12",
  75. "info": "野生前端程序",
  76. "children": [
  77. {
  78. "id": 11,
  79. "name": "baukh-11",
  80. "age": "28",
  81. "createDate": "2015-03-12",
  82. "info": "野生前端程序",
  83. "children": [
  84. {
  85. "id": 111,
  86. "name": "baukh-111",
  87. "age": "28",
  88. "createDate": "2015-03-12",
  89. "info": "野生前端程序"
  90. },
  91. {
  92. "id": 112,
  93. "name": "baukh-112",
  94. "age": "28",
  95. "createDate": "2015-03-12",
  96. "info": "野生前端程序"
  97. }
  98. ]
  99. },
  100. {
  101. "id": 12,
  102. "name": "baukh-12",
  103. "age": "28",
  104. "createDate": "2015-03-12",
  105. "info": "野生前端程序"
  106. },
  107. {
  108. "id": 13,
  109. "name": "baukh-13",
  110. "age": "28",
  111. "createDate": "2015-03-12",
  112. "info": "野生前端程序"
  113. }
  114. ]
  115. },
  116. {
  117. "id": 2,
  118. "name": "baukh2",
  119. "age": "28",
  120. "createDate": "2015-03-12",
  121. "info": "野生前端程序"
  122. },
  123. {
  124. "id": 3,
  125. "name": "baukh3",
  126. "age": "28",
  127. "createDate": "2015-03-12",
  128. "info": "野生前端程序"
  129. },
  130. {
  131. "id": 4,
  132. "name": "baukh4",
  133. "age": "28",
  134. "createDate": "2015-03-12",
  135. "info": "野生前端程序",
  136. "children": [
  137. {
  138. "id": 41,
  139. "name": "baukh",
  140. "age": "28",
  141. "createDate": "2015-03-12",
  142. "info": "野生前端程序"
  143. },
  144. {
  145. "id": 42,
  146. "name": "baukh",
  147. "age": "28",
  148. "createDate": "2015-03-12",
  149. "info": "野生前端程序"
  150. }
  151. ]
  152. },
  153. {
  154. "id": 5,
  155. "name": "baukh5",
  156. "age": "28",
  157. "createDate": "2015-03-12",
  158. "info": "野生前端程序"
  159. },{
  160. "id": 6,
  161. "name": "baukh6",
  162. "age": "28",
  163. "createDate": "2015-03-12",
  164. "info": "野生前端程序"
  165. },
  166. {
  167. "id": 7,
  168. "name": "baukh7",
  169. "age": "32",
  170. "createDate": "2015-03-12",
  171. "info": "野生前端程序"
  172. },
  173. {
  174. "id": 8,
  175. "name": "baukh8",
  176. "age": "28",
  177. "createDate": "2015-03-12",
  178. "info": "野生前端程序"
  179. }
  180. ],
  181. "totals": 8
  182. };
  183. const getData = num => {
  184. const data = [];
  185. let child = [];
  186. for (let i = 1; i<= num; i++) {
  187. child = [];
  188. for (let j = 1; j<= 40; j++) {
  189. child.push({
  190. "id": parseInt((i.toString() + j.toString()), 10),
  191. "name": `baukh-${i}-${j}`,
  192. "age": "28",
  193. "createDate": "2015-03-12",
  194. "info": "野生前端程序"
  195. });
  196. }
  197. data.push({
  198. "id": i,
  199. "name": `baukh-${i}`,
  200. "age": "28",
  201. "createDate": "2015-03-12",
  202. "info": "野生前端程序",
  203. "children": child
  204. });
  205. }
  206. return data;
  207. };
  208. var ajaxData2 = {
  209. "data": getData(4),
  210. "totals": 2
  211. };
  212. </script>
  213. <script type="text/javascript">
  214. // 实例化
  215. const now = Date.now();
  216. var table = document.querySelector('table[grid-manager]');
  217. function init(){
  218. table.GM({
  219. gridManagerName:'test',
  220. // fullColumn: {
  221. // template: function(){
  222. // return `<div style="padding: 12px; text-align: center;">
  223. // 快速、灵活的对Table标签进行实例化,让Table标签充满活力。该项目已开源,
  224. // <a target="_blank" href="https://github.com/baukh789/GridManager">点击进入</a>
  225. // github
  226. // </div>`;
  227. // }
  228. // },
  229. emptyTemplate: '<div class="gm-empty-template">什么也没有</div>',
  230. supportAjaxPage: true,
  231. // supportCheckbox: false,
  232. // supportAutoOrder: false,
  233. supportTreeData: true,
  234. supportMoveRow: true,
  235. treeConfig: {
  236. // insertTo: 'age',
  237. openState: false,
  238. treeKey: 'children'
  239. },
  240. ajaxData: ajaxData1,
  241. // useRadio: true,
  242. pageSize: 20,
  243. query: {ex: '用户自定义的查询参数,格式:{key:value}'},
  244. disableAutoLoading: true,
  245. exportConfig: {
  246. handler: (fileName, query, pageData, sortData, selectedList, tableData) => {
  247. GM.showLoading('test');
  248. GM.hideLoading('test', 500);
  249. }
  250. },
  251. summaryHandler: function(data){
  252. let num = 0;
  253. data.forEach(item => {
  254. num += parseInt(item.age, 10);
  255. });
  256. return {
  257. name: '平均年龄',
  258. age: num / data.length
  259. };
  260. },
  261. columnData: [{
  262. key: 'name',
  263. remind: 'the username',
  264. sorting: 'up',
  265. width: '200px',
  266. text: 'username'
  267. },{
  268. key: 'age',
  269. remind: 'the age',
  270. sorting: '',
  271. width: '200px',
  272. text: 'age'
  273. },{
  274. key: 'info',
  275. remind: 'the info',
  276. sorting: '',
  277. text: 'info'
  278. },{
  279. key: 'createDate',
  280. text: '创建时间',
  281. remind: 'the createDate',
  282. sorting: 'down',
  283. width: '200px',
  284. fixed: 'right'
  285. },{
  286. key: 'operation',
  287. remind: '修改创建时间',
  288. sorting: '',
  289. width: '130px',
  290. text: '修改创建时间',
  291. align: 'center',
  292. fixed: 'right',
  293. template: function(operation, rowObject){
  294. return `<span class="plugin-action">修改</span>`;
  295. }
  296. }
  297. ],
  298. pagingBefore:function(query){
  299. console.log('Event: 分页前', query);
  300. },
  301. pagingAfter: function(query){
  302. console.log('Event: 分页后', query);
  303. },
  304. sortingBefore:function(query){
  305. console.log('Event: 排序前', query);
  306. },
  307. sortingAfter: function(query){
  308. console.log('Event: 排序后', query);
  309. },
  310. ajaxBeforeSend: function() {
  311. GM.showLoading('test');
  312. },
  313. ajaxSuccess: function(data){
  314. console.log('Event: ajaxSuccess', data);
  315. GM.hideLoading('test', 300);
  316. },
  317. checkedAfter: function(list){
  318. console.log(list);
  319. }
  320. }, function(query){
  321. // 渲染完成后的回调函数
  322. console.log(query);
  323. console.log(Date.now() - now);
  324. // GM.setCheckedData('test', ajaxData1.data[2]);
  325. });
  326. }
  327. </script>
  328. <script>
  329. var initDOM = document.getElementById('init-gm');
  330. var destroyDOM = document.getElementById('destroy-gm');
  331. var resetDOM = document.getElementById('reset-gm');
  332. var updateTreeDOM = document.getElementById('update-tree');
  333. // 绑定初始化事件
  334. initDOM.onclick = function(){
  335. init();
  336. initDOM.setAttribute('disabled', '');
  337. destroyDOM.removeAttribute('disabled');
  338. resetDOM.removeAttribute('disabled');
  339. };
  340. // 绑定消毁事件
  341. destroyDOM.onclick = function(){
  342. GM.destroy('test');
  343. initDOM.removeAttribute('disabled');
  344. destroyDOM.setAttribute('disabled', '');
  345. resetDOM.setAttribute('disabled', '');
  346. };
  347. // 绑定修改数据方法事件
  348. let newData = ajaxData1;
  349. resetDOM.onclick = function(){
  350. newData = newData === ajaxData2 ? ajaxData1 : ajaxData2;
  351. GM.setAjaxData('test', newData, function(data){
  352. console.log('setAjaxData', data);
  353. });
  354. };
  355. // 绑定更新树打开状态事件
  356. let openState = false;
  357. updateTreeDOM.onclick = function(){
  358. openState = !openState;
  359. GM.updateTreeState('test', openState);
  360. };
  361. // 初始进入时, 执行init 方法
  362. init();
  363. initDOM.setAttribute('disabled', '');
  364. destroyDOM.removeAttribute('disabled');
  365. resetDOM.removeAttribute('disabled');
  366. updateTreeDOM.removeAttribute('disabled');
  367. jTool(table).on('click', '.plugin-action', function(e) {
  368. const row = GM.getRowData('test', this.parentElement.parentElement);
  369. GM.updateRowData('test', 'id', {id: row.id, createDate: new Date().toLocaleDateString()});
  370. });
  371. </script>
  372. </body>
  373. </html>