demo4.html 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. overflow-x:hidden;
  11. margin: 0;
  12. padding: 0;
  13. }
  14. body{
  15. padding: 0;
  16. }
  17. .plugin-action{
  18. display: inline-block;
  19. color: steelblue;
  20. margin-right: 10px;
  21. cursor: pointer;
  22. text-decoration: none;
  23. }
  24. .plugin-action:hover{
  25. text-decoration: underline;
  26. }
  27. .search-area{
  28. padding: 10px 20px;
  29. border: 1px solid #ccc;
  30. background: #efefef;
  31. margin-bottom: 15px;
  32. }
  33. .search-area .sa-ele{
  34. display: inline-block;
  35. margin-right: 20px;
  36. font-size: 12px;
  37. }
  38. .search-area .sa-ele .se-title{
  39. display: inline-block;
  40. margin-right: 10px;
  41. }
  42. .search-area .sa-ele .se-con{
  43. display: inline-block;
  44. width:180px;
  45. height: 24px;
  46. border: 1px solid #ccc;
  47. padding: 0 4px;
  48. line-height: 24px;
  49. }
  50. .search-area .sa-ele .search-action, .search-area .sa-ele .reset-action{
  51. display: inline-block;
  52. width:80px;
  53. height: 26px;
  54. border: 1px solid #ccc;
  55. background: #e8e8e8;
  56. padding: 0 4px;
  57. line-height: 26px;
  58. text-align: center;
  59. cursor: pointer;
  60. margin-right: 10px;
  61. }
  62. .search-area .sa-ele .search-action:hover, .search-area .sa-ele .reset-action:hover{
  63. opacity: 0.7;
  64. }
  65. .bottom-bar{
  66. background: #f8f8f8;
  67. padding: 10px;
  68. margin-top: 10px;
  69. }
  70. .bottom-bar button{
  71. padding: 5px 20px;
  72. margin-right: 10px;
  73. }
  74. .bottom-bar a{
  75. font-size: 12px;
  76. margin-right: 10px;
  77. }
  78. .void-template{
  79. height:300px;
  80. line-height: 300px;
  81. text-align: center;
  82. font-size: 24px;
  83. color: #ccc;
  84. }
  85. .grid-main {
  86. height: calc(100vh - 64px - 57px);
  87. }
  88. </style>
  89. <script>
  90. // 博文类型
  91. const TYPE_MAP = {
  92. '1': 'HTML/CSS',
  93. '2': 'nodeJS',
  94. '3': 'javaScript',
  95. '4': '前端鸡汤',
  96. '5': 'PM Coffee',
  97. '6': '前端框架',
  98. '7': '前端相关'
  99. };
  100. </script>
  101. </head>
  102. <body>
  103. <section class="search-area">
  104. <div class="sa-ele">
  105. <label class="se-title">标题:</label>
  106. <input class="se-con" name="title"/>
  107. </div>
  108. <div class="sa-ele">
  109. <label class="se-title">内容:</label>
  110. <input class="se-con" name="content"/>
  111. </div>
  112. <div class="sa-ele">
  113. <button class="search-action">搜索</button>
  114. <button class="reset-action">重置</button>
  115. </div>
  116. </section>
  117. <section class="grid-main">
  118. <table id="test"></table>
  119. </section>
  120. <section class="bottom-bar">
  121. <button id="init-gm" disabled>init</button>
  122. <button id="destroy-gm" disabled>destroy</button>
  123. <a href="https://github.com/baukh789/GridManager/blob/master/src/demo/demo4.html" target="_blank">查看源码</a>
  124. </section>
  125. <script type="text/javascript">
  126. // GridManager 渲染
  127. var table = document.querySelector('#test');
  128. function init() {
  129. table.GM({
  130. gridManagerName: 'test',
  131. width: '100%',
  132. height: '100%',
  133. supportAjaxPage:true,
  134. isCombSorting: true,
  135. disableLine:true,
  136. disableBorder: true,
  137. supportMoveRow: true,
  138. fullColumn: {
  139. interval: 6,
  140. topTemplate: function(row, index){
  141. return `<div style="padding: 12px; text-align: center;">
  142. 快速、灵活的对Table标签进行实例化,让Table标签充满活力。该项目已开源,
  143. <a target="_blank" href="https://github.com/baukh789/GridManager">点击进入</a>
  144. github
  145. </div>`;
  146. },
  147. // bottomTemplate: function(row, index){
  148. // return `<div style="padding: 12px; text-align: center;">
  149. // 如果觉着还不错,就
  150. // <a target="_blank" href="https://github.com/baukh789/GridManager">点个star</a>
  151. // 吧
  152. // </div>`;
  153. // }
  154. },
  155. // supportAutoOrder: false,
  156. // supportCheckbox: false,
  157. // disableCache: true,
  158. ajaxData: function () {
  159. return 'https://www.lovejavascript.com/blogManager/getBlogList';
  160. },
  161. // ,firstLoading: false // 初始渲染时是否加载数据
  162. ajaxType: 'POST',
  163. supportMenu: true,
  164. columnData: [
  165. {
  166. key: 'pic',
  167. remind: 'the pic',
  168. width: '110px',
  169. align: 'center',
  170. text: '缩略图',
  171. // 使用函数返回 dom node
  172. template: function(pic, rowObject) {
  173. var picNode = document.createElement('a');
  174. picNode.setAttribute('href', `https://www.lovejavascript.com/#!zone/blog/content.html?id=${rowObject.id}`);
  175. picNode.setAttribute('title', rowObject.title);
  176. picNode.setAttribute('target', '_blank');
  177. picNode.title = `点击阅读[${rowObject.title}]`;
  178. picNode.style.display = 'block';
  179. picNode.style.height = '58.5px';
  180. var imgNode = document.createElement('img');
  181. imgNode.style.width = '90px';
  182. imgNode.style.margin = '0 auto';
  183. imgNode.alt = rowObject.title;
  184. imgNode.src = `https://www.lovejavascript.com/${pic}`;
  185. picNode.appendChild(imgNode);
  186. return picNode;
  187. }
  188. },{
  189. key: 'title',
  190. remind: 'the title',
  191. align: 'left',
  192. text: '标题',
  193. sorting: '',
  194. // 使用函数返回 dom node
  195. template: function(title, rowObject) {
  196. var titleNode = document.createElement('a');
  197. titleNode.setAttribute('href', `https://www.lovejavascript.com/#!zone/blog/content.html?id=${rowObject.id}`);
  198. titleNode.setAttribute('title', title);
  199. titleNode.setAttribute('target', '_blank');
  200. titleNode.innerText = title;
  201. titleNode.title = `点击阅读[${rowObject.title}]`;
  202. titleNode.classList.add('plugin-action');
  203. return titleNode;
  204. }
  205. },{
  206. key: 'type',
  207. remind: 'the type',
  208. text: '博文分类',
  209. width: '130px',
  210. align: 'center',
  211. sorting: '',
  212. // 表头筛选条件, 该值由用户操作后会将选中的值以{key: value}的形式覆盖至query参数内。非必设项
  213. filter: {
  214. // 筛选条件列表, 数组对象。格式: [{value: '1', text: 'HTML/CSS'}],在使用filter时该参数为必设项。
  215. option: [
  216. {value: '1', text: 'HTML/CSS'},
  217. {value: '2', text: 'nodeJS'},
  218. {value: '3', text: 'javaScript'},
  219. {value: '4', text: '前端鸡汤'},
  220. {value: '5', text: 'PM Coffee'},
  221. {value: '6', text: '前端框架'},
  222. {value: '7', text: '前端相关'}
  223. ],
  224. // 筛选选中项,字符串, 默认为''。 非必设项,选中的过滤条件将会覆盖query
  225. selected: '3',
  226. // 否为多选, 布尔值, 默认为false。非必设项
  227. isMultiple: false
  228. },
  229. template: function(type, rowObject){
  230. return TYPE_MAP[type];
  231. }
  232. },{
  233. key: 'info',
  234. remind: 'the info',
  235. text: '简介'
  236. },{
  237. key: 'username',
  238. remind: 'the username',
  239. align: 'center',
  240. width: '120px',
  241. text: '作者',
  242. template: function(username, row, index){
  243. return `<a class="plugin-action" href="https://github.com/baukh789" target="_blank" title="去看看${username}的github">${username}</a>`;
  244. }
  245. },{
  246. key: 'createDate',
  247. width: '130px',
  248. text: '创建时间',
  249. sorting: 'DESC',
  250. // 使用函数返回 htmlString
  251. template: function(createDate, rowObject){
  252. return new Date(createDate).toLocaleDateString();
  253. }
  254. },{
  255. key: 'lastDate',
  256. width: '130px',
  257. text: '最后修改时间',
  258. sorting: '',
  259. // 使用函数返回 htmlString
  260. template: function(lastDate, rowObject){
  261. return new Date(lastDate).toLocaleDateString();
  262. }
  263. }
  264. ]
  265. // 排序后事件
  266. ,sortingAfter: function (data) {
  267. console.log('sortAfter', data);
  268. }
  269. }, function(query){
  270. // 渲染完成后的回调函数
  271. console.log('渲染完成后的回调函数:', query);
  272. });
  273. }
  274. /**
  275. * 提供demo中的搜索, 重置
  276. */
  277. (function(){
  278. // 绑定搜索事件
  279. document.querySelector('.search-action').addEventListener('click', function () {
  280. var _query = {
  281. title: document.querySelector('[name="title"]').value,
  282. content: document.querySelector('[name="content"]').value,
  283. cPage: 1
  284. };
  285. table.GM('setQuery', _query, function(){
  286. console.log('setQuery执行成功');
  287. });
  288. });
  289. // 绑定重置
  290. document.querySelector('.reset-action').addEventListener('click', function () {
  291. document.querySelector('[name="title"]').value = '';
  292. document.querySelector('[name="content"]').value = '';
  293. });
  294. })();
  295. /**
  296. * 绑定 实例化, 消毁事件
  297. */
  298. (function () {
  299. var initDOM = document.getElementById('init-gm');
  300. var destroyDOM = document.getElementById('destroy-gm');
  301. var codeShowDOM = document.querySelector('.code-show');
  302. // 绑定初始化事件
  303. initDOM.onclick = function(){
  304. init();
  305. initDOM.setAttribute('disabled', '');
  306. destroyDOM.removeAttribute('disabled');
  307. };
  308. // 绑定消毁事件
  309. destroyDOM.onclick = function(){
  310. table.GM('destroy');
  311. initDOM.removeAttribute('disabled');
  312. destroyDOM.setAttribute('disabled', '');
  313. };
  314. })();
  315. /**
  316. * 初始进入时, 执行init 方法
  317. */
  318. (function(){
  319. init();
  320. var initDOM = document.getElementById('init-gm');
  321. var destroyDOM = document.getElementById('destroy-gm');
  322. initDOM.setAttribute('disabled', '');
  323. destroyDOM.removeAttribute('disabled');
  324. })();
  325. </script>
  326. </body>
  327. </html>