demo8.html 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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. overflow-x:hidden;
  12. margin: 0;
  13. padding: 0;
  14. }
  15. p{
  16. margin: 0;
  17. }
  18. .plugin-action{
  19. display: inline-block;
  20. color: steelblue;
  21. margin-right: 10px;
  22. cursor: pointer;
  23. text-decoration: none;
  24. }
  25. .plugin-action:hover{
  26. text-decoration: underline;
  27. }
  28. .search-area{
  29. padding: 10px 20px;
  30. border: 1px solid #ccc;
  31. background: #efefef;
  32. margin-bottom: 15px;
  33. }
  34. .search-area .sa-ele{
  35. display: inline-block;
  36. margin-right: 20px;
  37. font-size: 12px;
  38. }
  39. .search-area .sa-ele .se-title{
  40. display: inline-block;
  41. margin-right: 10px;
  42. }
  43. .search-area .sa-ele .se-con{
  44. display: inline-block;
  45. width:180px;
  46. height: 24px;
  47. border: 1px solid #ccc;
  48. padding: 0 4px;
  49. line-height: 24px;
  50. }
  51. .search-area .sa-ele .search-action, .search-area .sa-ele .reset-action{
  52. display: inline-block;
  53. width:80px;
  54. height: 26px;
  55. border: 1px solid #ccc;
  56. background: #e8e8e8;
  57. padding: 0 4px;
  58. line-height: 26px;
  59. text-align: center;
  60. cursor: pointer;
  61. margin-right: 10px;
  62. }
  63. .search-area .sa-ele .search-action:hover, .search-area .sa-ele .reset-action:hover{
  64. opacity: 0.7;
  65. }
  66. .bottom-bar{
  67. background: #f8f8f8;
  68. padding: 10px;
  69. margin-top: 10px;
  70. }
  71. .bottom-bar button{
  72. padding: 5px 20px;
  73. margin-right: 10px;
  74. }
  75. .bottom-bar a{
  76. font-size: 12px;
  77. margin-right: 10px;
  78. }
  79. .void-template{
  80. text-align: center;
  81. font-size: 24px;
  82. color: #ccc;
  83. }
  84. .grid-main {
  85. height: calc(100vh - 64px - 57px);
  86. }
  87. .gm-child{
  88. display:inline-block;
  89. width: 50%;
  90. }
  91. </style>
  92. <script>
  93. // 博文类型
  94. const TYPE_MAP = {
  95. '1': 'HTML/CSS',
  96. '2': 'nodeJS',
  97. '3': 'javaScript',
  98. '4': '前端鸡汤',
  99. '5': 'PM Coffee',
  100. '6': '前端框架',
  101. '7': '前端相关'
  102. };
  103. </script>
  104. </head>
  105. <body>
  106. <section class="search-area">
  107. <div class="sa-ele">
  108. <label class="se-title">标题:</label>
  109. <input class="se-con" name="title"/>
  110. </div>
  111. <div class="sa-ele">
  112. <label class="se-title">内容:</label>
  113. <input class="se-con" name="content"/>
  114. </div>
  115. <div class="sa-ele">
  116. <button class="search-action">搜索</button>
  117. <button class="reset-action">重置</button>
  118. </div>
  119. </section>
  120. <section class="grid-main">
  121. <table></table>
  122. </section>
  123. <section class="bottom-bar">
  124. <button id="init-gm" disabled>init</button>
  125. <button id="destroy-gm" disabled>destroy</button>
  126. <a href="https://github.com/baukh789/GridManager/blob/master/src/demo/demo1.html" target="_blank">查看源码</a>
  127. </section>
  128. <script type="text/javascript">
  129. // GridManager 渲染
  130. var table = document.querySelector('table');
  131. function init() {
  132. table.GM({
  133. gridManagerName: 'test',
  134. width: '100%',
  135. height: '100%',
  136. supportAjaxPage:true,
  137. // supportCheckbox: false,
  138. // supportAutoOrder: false,
  139. isCombSorting: true,
  140. checkboxConfig: {
  141. fixed: 'left'
  142. },
  143. autoOrderConfig: {
  144. fixed: 'left'
  145. },
  146. ajaxData: function () {
  147. return 'https://www.lovejavascript.com/blogManager/getBlogList';
  148. },
  149. ajaxType: 'POST',
  150. summaryHandler: data => {
  151. let praiseNumber = 0;
  152. let readNumber = 0;
  153. let commentSum = 0;
  154. data.forEach(item => {
  155. praiseNumber += parseInt(item.praiseNumber, 10);
  156. readNumber += item.readNumber;
  157. commentSum += item.commentSum;
  158. });
  159. return {
  160. pic: '共计',
  161. praiseNumber,
  162. readNumber,
  163. commentSum
  164. };
  165. },
  166. exportConfig: {
  167. mode: 'static',
  168. handler: (fileName, query, pageData, sortData, selectedList, tableData) => {
  169. const array = [];
  170. array.push(['标题', '博文分类', '简介', '作者', '创建时间', '修改时间']);
  171. tableData.forEach(item => {
  172. array.push([
  173. `"${item.title}"`,
  174. `"${TYPE_MAP[item.type]}"`,
  175. `"${item.info}"`,
  176. `"${item.username}"`,
  177. `"${new Date(item.createDate).toLocaleDateString()}"`,
  178. `"${new Date(item.lastDate).toLocaleDateString()}"`
  179. ]);
  180. });
  181. return array;
  182. }
  183. },
  184. emptyTemplate: '<div class="void-template">通过 emptyTemplate 配置的空数据提示</div>',
  185. disableCache: false,
  186. columnData: [
  187. {
  188. key: 'pic',
  189. remind: 'the pic',
  190. width: '110px',
  191. align: 'center',
  192. text: '缩略图',
  193. // 使用函数返回 dom node
  194. template: function(pic, rowObject) {
  195. var picNode = document.createElement('a');
  196. picNode.setAttribute('href', `https://www.lovejavascript.com/#!zone/blog/content.html?id=${rowObject.id}`);
  197. picNode.setAttribute('title', rowObject.title);
  198. picNode.setAttribute('target', '_blank');
  199. picNode.title = `点击阅读[${rowObject.title}]`;
  200. picNode.style.display = 'block';
  201. picNode.style.height = '58.5px';
  202. var imgNode = document.createElement('img');
  203. imgNode.style.width = '90px';
  204. imgNode.style.margin = '0 auto';
  205. imgNode.alt = rowObject.title;
  206. imgNode.src = `https://www.lovejavascript.com/${pic}`;
  207. picNode.appendChild(imgNode);
  208. return picNode;
  209. }
  210. },{
  211. key: 'title',
  212. align: 'left',
  213. // width: '300px',
  214. text: '标题',
  215. sorting: '',
  216. // 使用函数返回 dom node
  217. template: function(title, rowObject) {
  218. var titleNode = document.createElement('a');
  219. titleNode.setAttribute('href', `https://www.lovejavascript.com/#!zone/blog/content.html?id=${rowObject.id}`);
  220. titleNode.setAttribute('title', title);
  221. titleNode.setAttribute('target', '_blank');
  222. titleNode.innerText = title;
  223. titleNode.title = `点击阅读[${rowObject.title}]`;
  224. titleNode.classList.add('plugin-action');
  225. return titleNode;
  226. }
  227. },{
  228. key: 'info',
  229. remind: 'the info',
  230. text: '简介'
  231. },{
  232. key: 'blogData',
  233. text: '博文数据',
  234. align: 'left',
  235. // width: '200px',
  236. children: [
  237. {
  238. key: 'type',
  239. text: '博文分类',
  240. align: 'left',
  241. template: function(type, rowObject){
  242. return TYPE_MAP[type] + 3;
  243. }
  244. },
  245. {
  246. key: 'blogInteractive',
  247. text: '博文互动',
  248. align: 'left',
  249. children: [
  250. {
  251. key: 'commentSum',
  252. text: '评论评论评论评论评论',
  253. align: 'left'
  254. },
  255. {
  256. key: 'praiseNumber',
  257. text: '点赞',
  258. align: 'left'
  259. },
  260. {
  261. key: 'readNumber',
  262. text: '阅读',
  263. align: 'left'
  264. }
  265. ]
  266. }
  267. ]
  268. },{
  269. key: 'author',
  270. align: 'center',
  271. width: '391px',
  272. text: '作者',
  273. children: [
  274. {
  275. key: 'photo',
  276. align: 'center',
  277. // width: '130px',
  278. text: '头像',
  279. template: photo => {
  280. return `<img width="50" src="https://www.lovejavascript.com/${photo}"/>`;
  281. }
  282. },
  283. {
  284. key: 'username',
  285. align: 'center',
  286. // width: '130px',
  287. text: '昵称'
  288. },
  289. {
  290. key: 'github',
  291. align: 'center',
  292. width: 110,
  293. text: 'github',
  294. template: () => {
  295. return `<a class="plugin-action" href="https://github.com/baukh789" target="_blank">github</a>`
  296. }
  297. }
  298. ]
  299. },{
  300. key: 'actionDate',
  301. text: '操作时间',
  302. sorting: 'DESC',
  303. align: "center",
  304. width: '280px',
  305. // fixed: 'right',
  306. children: [
  307. {
  308. key: 'createDate',
  309. text: '创建时间',
  310. sorting: 'DESC',
  311. align: "center",
  312. width: '140px',
  313. template: function(createDate){
  314. return new Date(createDate).toLocaleDateString();
  315. }
  316. },
  317. {
  318. key: 'lastDate',
  319. text: '修改时间',
  320. sorting: 'DESC',
  321. align: "center",
  322. width: '140px',
  323. template: function(lastDate){
  324. return new Date(lastDate).toLocaleDateString();
  325. }
  326. }
  327. ]
  328. }
  329. ]
  330. });
  331. }
  332. // 删除功能
  333. function delectRowData(rowData){
  334. // 执行删除操作
  335. if(window.confirm('确认要删除['+rowData.title+']?')){
  336. window.alert('当然这只是个示例,并不会真实删除,要不然每天我每天就光填demo数据了.');
  337. }
  338. }
  339. /**
  340. * 提供demo中的搜索, 重置
  341. */
  342. (function(){
  343. // 绑定搜索事件
  344. document.querySelector('.search-action').addEventListener('click', function () {
  345. var _query = {
  346. title: document.querySelector('[name="title"]').value,
  347. content: document.querySelector('[name="content"]').value,
  348. cPage: 1
  349. };
  350. table.GM('setQuery', _query, function(){
  351. console.log('setQuery执行成功');
  352. });
  353. });
  354. // 绑定重置
  355. document.querySelector('.reset-action').addEventListener('click', function () {
  356. document.querySelector('[name="title"]').value = '';
  357. document.querySelector('[name="type"]').value = '-1';
  358. document.querySelector('[name="content"]').value = '';
  359. });
  360. })();
  361. /**
  362. * 绑定 实例化, 消毁事件
  363. */
  364. (function () {
  365. var initDOM = document.getElementById('init-gm');
  366. var destroyDOM = document.getElementById('destroy-gm');
  367. var codeShowDOM = document.querySelector('.code-show');
  368. // 绑定初始化事件
  369. initDOM.onclick = function(){
  370. init();
  371. initDOM.setAttribute('disabled', '');
  372. destroyDOM.removeAttribute('disabled');
  373. };
  374. // 绑定消毁事件
  375. destroyDOM.onclick = function(){
  376. table.GM('destroy');
  377. initDOM.removeAttribute('disabled');
  378. destroyDOM.setAttribute('disabled', '');
  379. };
  380. })();
  381. /**
  382. * 初始进入时, 执行init 方法
  383. */
  384. (function(){
  385. init();
  386. var initDOM = document.getElementById('init-gm');
  387. var destroyDOM = document.getElementById('destroy-gm');
  388. initDOM.setAttribute('disabled', '');
  389. destroyDOM.removeAttribute('disabled');
  390. })();
  391. </script>
  392. </body>
  393. </html>