demo9.html 12 KB

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