demo5.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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. .plugin-action{
  16. display: inline-block;
  17. color: steelblue;
  18. margin-right: 10px;
  19. cursor: pointer;
  20. text-decoration: none;
  21. }
  22. .plugin-action:hover{
  23. text-decoration: underline;
  24. }
  25. .search-area{
  26. padding: 10px 20px;
  27. border: 1px solid #ccc;
  28. background: #efefef;
  29. margin-bottom: 15px;
  30. }
  31. .search-area .sa-ele{
  32. display: inline-block;
  33. margin-right: 20px;
  34. font-size: 12px;
  35. }
  36. .search-area .sa-ele .se-title{
  37. display: inline-block;
  38. margin-right: 10px;
  39. }
  40. .search-area .sa-ele .se-con{
  41. display: inline-block;
  42. width:180px;
  43. height: 24px;
  44. border: 1px solid #ccc;
  45. padding: 0 4px;
  46. line-height: 24px;
  47. }
  48. .search-area .sa-ele .search-action, .search-area .sa-ele .reset-action{
  49. display: inline-block;
  50. width:80px;
  51. height: 26px;
  52. border: 1px solid #ccc;
  53. background: #e8e8e8;
  54. padding: 0 4px;
  55. line-height: 26px;
  56. text-align: center;
  57. cursor: pointer;
  58. margin-right: 10px;
  59. }
  60. .search-area .sa-ele .search-action:hover, .search-area .sa-ele .reset-action:hover{
  61. opacity: 0.7;
  62. }
  63. .bottom-bar{
  64. background: #f8f8f8;
  65. padding: 10px;
  66. margin-top: 10px;
  67. }
  68. .bottom-bar button{
  69. padding: 5px 20px;
  70. margin-right: 10px;
  71. }
  72. .bottom-bar a{
  73. font-size: 12px;
  74. margin-right: 10px;
  75. }
  76. .void-template{
  77. height:300px;
  78. line-height: 300px;
  79. text-align: center;
  80. font-size: 24px;
  81. color: #ccc;
  82. }
  83. .grid-main {
  84. height: calc(100vh - 64px - 57px);
  85. }
  86. </style>
  87. <script>
  88. // 博文类型
  89. const TYPE_MAP = {
  90. '1': 'HTML/CSS',
  91. '2': 'nodeJS',
  92. '3': 'javaScript',
  93. '4': '前端鸡汤',
  94. '5': 'PM Coffee',
  95. '6': '前端框架',
  96. '7': '前端相关'
  97. };
  98. </script>
  99. </head>
  100. <body>
  101. <section class="search-area">
  102. <div class="sa-ele">
  103. <label class="se-title">标题:</label>
  104. <input class="se-con" name="title"/>
  105. </div>
  106. <div class="sa-ele">
  107. <label class="se-title">内容:</label>
  108. <input class="se-con" name="content"/>
  109. </div>
  110. <div class="sa-ele">
  111. <button class="search-action">搜索</button>
  112. <button class="reset-action">重置</button>
  113. </div>
  114. </section>
  115. <section class="grid-main">
  116. <table></table>
  117. </section>
  118. <section class="bottom-bar">
  119. <button id="init-gm" disabled>init</button>
  120. <button id="destroy-gm" disabled>destroy</button>
  121. <a href="https://github.com/baukh789/GridManager/blob/master/src/demo/demo5.html" target="_blank">查看源码</a>
  122. </section>
  123. <script type="text/javascript">
  124. // GridManager 渲染
  125. var table = document.querySelector('table');
  126. function init() {
  127. table.GM({
  128. gridManagerName: 'test',
  129. width: '100%',
  130. height: '100%',
  131. supportAjaxPage:true,
  132. isCombSorting: true,
  133. checkboxConfig: {
  134. useRowCheck: true,
  135. useRadio: true,
  136. },
  137. // disableBorder: true,
  138. disableLine: true,
  139. disableHover: true,
  140. ajaxData: function () {
  141. return 'https://www.lovejavascript.com/blogManager/getBlogList';
  142. },
  143. ajaxType: 'POST',
  144. // checkedBefore: function(checkedList, checked, row) {
  145. // console.log(checkedList, checked, row);
  146. // return row.id === 92;
  147. // },
  148. checkedAfter: function(checkedList) {
  149. console.log('当前选中的行数据=>', checkedList);
  150. },
  151. columnData: [
  152. {
  153. key: 'pic',
  154. remind: 'the pic',
  155. width: '110px',
  156. align: 'center',
  157. text: '缩略图',
  158. // 使用函数返回 dom node
  159. template: function(pic, rowObject) {
  160. var picNode = document.createElement('a');
  161. picNode.setAttribute('href', `https://www.lovejavascript.com/#!zone/blog/content.html?id=${rowObject.id}`);
  162. picNode.setAttribute('title', rowObject.title);
  163. picNode.setAttribute('target', '_blank');
  164. picNode.title = `点击阅读[${rowObject.title}]`;
  165. picNode.style.display = 'block';
  166. picNode.style.height = '58.5px';
  167. var imgNode = document.createElement('img');
  168. imgNode.style.width = '90px';
  169. imgNode.style.margin = '0 auto';
  170. imgNode.alt = rowObject.title;
  171. imgNode.src = `https://www.lovejavascript.com/${pic}`;
  172. picNode.appendChild(imgNode);
  173. return picNode;
  174. }
  175. },{
  176. key: 'title',
  177. remind: 'the title',
  178. align: 'left',
  179. text: '标题',
  180. sorting: '',
  181. // 使用函数返回 dom node
  182. template: function(title, rowObject) {
  183. var titleNode = document.createElement('a');
  184. titleNode.setAttribute('href', `https://www.lovejavascript.com/#!zone/blog/content.html?id=${rowObject.id}`);
  185. titleNode.setAttribute('title', title);
  186. titleNode.setAttribute('target', '_blank');
  187. titleNode.innerText = title;
  188. titleNode.title = `点击阅读[${rowObject.title}]`;
  189. titleNode.classList.add('plugin-action');
  190. return titleNode;
  191. }
  192. },{
  193. key: 'type',
  194. remind: 'the type',
  195. text: '博文分类',
  196. width: '120px',
  197. align: 'center',
  198. sorting: '',
  199. template: function(type, rowObject){
  200. return TYPE_MAP[type];
  201. }
  202. },{
  203. key: 'info',
  204. remind: 'the info',
  205. width: '100px',
  206. text: '简介'
  207. },{
  208. key: 'username',
  209. remind: 'the username',
  210. align: 'center',
  211. width: '100px',
  212. text: '作者',
  213. template: function(username){
  214. return `<a class="plugin-action" href="https://github.com/baukh789" target="_blank" title="去看看${username}的github">${username}</a>`;
  215. }
  216. },{
  217. key: 'createDate',
  218. width: '130px',
  219. text: '创建时间',
  220. sorting: 'DESC',
  221. // 使用函数返回 htmlString
  222. template: function(createDate, rowObject){
  223. return new Date(createDate).toLocaleDateString();
  224. }
  225. },{
  226. key: 'lastDate',
  227. width: '130px',
  228. text: '最后修改时间',
  229. sorting: '',
  230. // 使用函数返回 htmlString
  231. template: function(lastDate, rowObject){
  232. return new Date(lastDate).toLocaleDateString();
  233. }
  234. },{
  235. key: 'action',
  236. remind: 'the action',
  237. width: '100px',
  238. align: 'center',
  239. text: '<span style="color: red">操作</span>',
  240. template: function(action, rowObject){
  241. return `<span class="plugin-action" title="${rowObject.title}"onclick="delectRowData(this.title)">删除</span>`;
  242. }
  243. }
  244. ]
  245. });
  246. }
  247. // 删除功能
  248. function delectRowData(title){
  249. window.event.stopPropagation();
  250. // 执行删除操作
  251. if(window.confirm('确认要删除['+title+']?')){
  252. window.alert('当然这只是个示例,并不会真实删除,要不然每天我每天就光填demo数据了.');
  253. }
  254. }
  255. /**
  256. * 提供demo中的搜索, 重置
  257. */
  258. (function(){
  259. // 绑定搜索事件
  260. document.querySelector('.search-action').addEventListener('click', function () {
  261. var _query = {
  262. title: document.querySelector('[name="title"]').value,
  263. content: document.querySelector('[name="content"]').value,
  264. cPage: 1
  265. };
  266. table.GM('setQuery', _query, function(){
  267. console.log('setQuery执行成功');
  268. });
  269. });
  270. // 绑定重置
  271. document.querySelector('.reset-action').addEventListener('click', function () {
  272. document.querySelector('[name="title"]').value = '';
  273. document.querySelector('[name="content"]').value = '';
  274. });
  275. })();
  276. /**
  277. * 绑定 实例化, 消毁事件
  278. */
  279. (function () {
  280. var initDOM = document.getElementById('init-gm');
  281. var destroyDOM = document.getElementById('destroy-gm');
  282. var codeShowDOM = document.querySelector('.code-show');
  283. // 绑定初始化事件
  284. initDOM.onclick = function(){
  285. init();
  286. initDOM.setAttribute('disabled', '');
  287. destroyDOM.removeAttribute('disabled');
  288. };
  289. // 绑定消毁事件
  290. destroyDOM.onclick = function(){
  291. table.GM('destroy');
  292. initDOM.removeAttribute('disabled');
  293. destroyDOM.setAttribute('disabled', '');
  294. };
  295. })();
  296. /**
  297. * 初始进入时, 执行init 方法
  298. */
  299. (function(){
  300. init();
  301. var initDOM = document.getElementById('init-gm');
  302. var destroyDOM = document.getElementById('destroy-gm');
  303. initDOM.setAttribute('disabled', '');
  304. destroyDOM.removeAttribute('disabled');
  305. })();
  306. </script>
  307. </body>
  308. </html>