promise.html 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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:使用Promise方式加载</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. .void-template{
  73. height:300px;
  74. line-height: 300px;
  75. text-align: center;
  76. font-size: 24px;
  77. color: #ccc;
  78. }
  79. </style>
  80. <script>
  81. // 博文类型
  82. const TYPE_MAP = {
  83. '1': 'HTML/CSS',
  84. '2': 'nodeJS',
  85. '3': 'javaScript',
  86. '4': '前端鸡汤',
  87. '5': 'PM Coffee',
  88. '6': '前端框架',
  89. '7': '前端相关'
  90. };
  91. </script>
  92. </head>
  93. <body>
  94. <section class="search-area">
  95. <div class="sa-ele">
  96. <label class="se-title">标题:</label>
  97. <input class="se-con" name="title"/>
  98. </div>
  99. <div class="sa-ele">
  100. <label class="se-title">博文分类:</label>
  101. <select class="se-con" name="type">
  102. <option value="-1">请选择</option>
  103. <!--通过js增加-->
  104. </select>
  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. </section>
  122. <script type="text/javascript">
  123. // 模拟了一个简单的promise请求
  124. const getBlogList = function(paramse) {
  125. return new Promise((resolve, reject) => {
  126. const xhr = new XMLHttpRequest();
  127. xhr.open('POST', 'https://www.lovejavascript.com/blogManager/getBlogList');
  128. xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  129. xhr.onreadystatechange = function() {
  130. if (xhr.readyState !== 4) {
  131. return;
  132. }
  133. if (xhr.status >= 200 && xhr.status < 300 || xhr.status === 304) {
  134. resolve(xhr.response);
  135. } else {
  136. reject(xhr);
  137. }
  138. };
  139. // 一个简单的处理参数的示例
  140. let formData = '';
  141. for (let key in paramse) {
  142. if(formData !== '') {
  143. formData += '&';
  144. }
  145. formData += key + '=' + paramse[key];
  146. }
  147. xhr.send(formData);
  148. });
  149. };
  150. // GridManager 渲染
  151. var table = document.querySelector('table');
  152. function init() {
  153. table.GM({gridManagerName: 'test'
  154. ,height: '400px'
  155. ,supportAjaxPage:true
  156. ,isCombSorting: false
  157. ,disableCache: false
  158. ,ajaxData: function (setting) {
  159. // 传入分页及排序的配置项
  160. return getBlogList(Object.assign({}, setting.pageData, setting.sortData));
  161. }
  162. ,ajaxType: 'POST'
  163. ,supportMenu: true
  164. ,pageSize:30
  165. ,columnData: [
  166. {
  167. key: 'pic',
  168. remind: 'the pic',
  169. width: '110px',
  170. align: 'center',
  171. text: '缩略图',
  172. // 使用函数返回 dom node
  173. template: function(pic, rowObject) {
  174. var picNode = document.createElement('a');
  175. picNode.setAttribute('href', `https://www.lovejavascript.com/#!zone/blog/content.html?id=${rowObject.id}`);
  176. picNode.setAttribute('title', rowObject.title);
  177. picNode.setAttribute('target', '_blank');
  178. picNode.title = `点击阅读[${rowObject.title}]`;
  179. picNode.style.display = 'block';
  180. picNode.style.height = '58.5px';
  181. var imgNode = document.createElement('img');
  182. imgNode.style.width = '90px';
  183. imgNode.style.margin = '0 auto';
  184. imgNode.alt = rowObject.title;
  185. imgNode.src = `https://www.lovejavascript.com/${pic}`;
  186. picNode.appendChild(imgNode);
  187. return picNode;
  188. }
  189. },{
  190. key: 'title',
  191. remind: 'the title',
  192. width: '300px',
  193. align: 'left',
  194. text: '标题',
  195. sorting: '',
  196. // 使用函数返回 dom node
  197. template: function(title, rowObject) {
  198. var titleNode = document.createElement('a');
  199. titleNode.setAttribute('href', `https://www.lovejavascript.com/#!zone/blog/content.html?id=${rowObject.id}`);
  200. titleNode.setAttribute('title', title);
  201. titleNode.setAttribute('target', '_blank');
  202. titleNode.innerText = title;
  203. titleNode.title = `点击阅读[${rowObject.title}]`;
  204. titleNode.classList.add('plugin-action');
  205. return titleNode;
  206. }
  207. },{
  208. key: 'type',
  209. remind: 'the type',
  210. text: '博文分类',
  211. width: '100',
  212. align: 'center',
  213. template: function(type, rowObject){
  214. return TYPE_MAP[type];
  215. }
  216. },{
  217. key: 'info',
  218. remind: 'the info',
  219. text: '简介',
  220. isShow: false
  221. },{
  222. key: 'username',
  223. remind: 'the username',
  224. width: '100px',
  225. align: 'center',
  226. text: '作者',
  227. template: function(username){
  228. return `<a class="plugin-action" href="https://github.com/baukh789" target="_blank" title="去看看${username}的github">${username}</a>`;
  229. }
  230. },{
  231. key: 'createDate',
  232. remind: 'the createDate',
  233. width: '100px',
  234. text: '创建时间',
  235. sorting: 'DESC',
  236. // 使用函数返回 htmlString
  237. template: function(createDate, rowObject){
  238. return new Date(createDate).toLocaleDateString();
  239. }
  240. },{
  241. key: 'lastDate',
  242. remind: 'the lastDate',
  243. width: '130px',
  244. text: '最后修改时间',
  245. sorting: '',
  246. // 使用函数返回 htmlString
  247. template: function(lastDate, rowObject){
  248. return new Date(lastDate).toLocaleDateString();
  249. }
  250. }
  251. ]
  252. // 排序后事件
  253. ,sortingAfter: function (data) {
  254. console.log('sortAfter', data);
  255. }
  256. });
  257. }
  258. /**
  259. * 渲染博文类型
  260. */
  261. (function () {
  262. // 渲染下拉框
  263. var typeSelect = document.querySelector('.search-area select[name="type"]');
  264. for(var key in TYPE_MAP){
  265. var option = document.createElement('option');
  266. option.value = key;
  267. option.innerText = TYPE_MAP[key];
  268. typeSelect.appendChild(option);
  269. }
  270. })();
  271. /**
  272. * 提供demo中的搜索, 重置
  273. */
  274. (function(){
  275. // 绑定搜索事件
  276. document.querySelector('.search-action').addEventListener('click', function () {
  277. var _query = {
  278. title: document.querySelector('[name="title"]').value,
  279. type: document.querySelector('[name="type"]').value,
  280. content: document.querySelector('[name="content"]').value,
  281. cPage: 1
  282. };
  283. table.GM('setQuery', _query, function(){
  284. console.log('setQuery执行成功');
  285. });
  286. });
  287. // 绑定重置
  288. document.querySelector('.reset-action').addEventListener('click', function () {
  289. document.querySelector('[name="title"]').value = '';
  290. document.querySelector('[name="type"]').value = '-1';
  291. document.querySelector('[name="content"]').value = '';
  292. });
  293. })();
  294. /**
  295. * 绑定 实例化, 消毁事件
  296. */
  297. (function () {
  298. var initDOM = document.getElementById('init-gm');
  299. var destroyDOM = document.getElementById('destroy-gm');
  300. var codeShowDOM = document.querySelector('.code-show');
  301. // 绑定初始化事件
  302. initDOM.onclick = function(){
  303. init();
  304. initDOM.setAttribute('disabled', '');
  305. destroyDOM.removeAttribute('disabled');
  306. };
  307. // 绑定消毁事件
  308. destroyDOM.onclick = function(){
  309. table.GM('destroy');
  310. initDOM.removeAttribute('disabled');
  311. destroyDOM.setAttribute('disabled', '');
  312. };
  313. })();
  314. /**
  315. * 初始进入时, 执行init 方法
  316. */
  317. (function(){
  318. init();
  319. var initDOM = document.getElementById('init-gm');
  320. var destroyDOM = document.getElementById('destroy-gm');
  321. initDOM.setAttribute('disabled', '');
  322. destroyDOM.removeAttribute('disabled');
  323. })();
  324. </script>
  325. </body>
  326. </html>