控制台应用,yzncms本身基于tp5.1框架,里面的队列用不了,bug,坑
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092
  1. /**
  2. @ Name:简单封下table
  3. */
  4. layui.define(['form', 'table', 'yzn', 'laydate', 'laytpl', 'element','notice'], function(exports) {
  5. var MOD_NAME = 'yznTable',
  6. $ = layui.$,
  7. table = layui.table,
  8. yzn = layui.yzn,
  9. laydate = layui.laydate,
  10. element = layui.element,
  11. laytpl = layui.laytpl,
  12. form = layui.form,
  13. notice = layui.notice;
  14. var init = {
  15. table_elem: '#currentTable',
  16. table_render_id: 'currentTable',
  17. };
  18. var ColumnsForSearch = [];
  19. yznTable = {
  20. bindevent: function () {
  21. // 监听请求
  22. $(document).on('click', '[data-request]', function() {
  23. var that = $(this);
  24. var title = $(this).data('title'),
  25. url = $(this).data('request') || $(this).attr("href"),
  26. tableId = $(this).data('table'),
  27. checkbox = $(this).data('checkbox');
  28. var postData = {};
  29. if (checkbox === 'true') {
  30. tableId = tableId || init.table_render_id;
  31. var checkStatus = table.checkStatus(tableId),
  32. data = checkStatus.data;
  33. if (data.length <= 0) {
  34. yzn.msg.error('请勾选需要操作的数据');
  35. return false;
  36. }
  37. var ids = [];
  38. $.each(data, function(i, v) {
  39. ids.push(v.id);
  40. });
  41. postData.id = ids;
  42. }
  43. title = title || '确定进行该操作?';
  44. tableId = tableId || init.table_render_id;
  45. func = function() {
  46. yzn.request.post({
  47. url: url,
  48. data: postData,
  49. }, function(data,res) {
  50. yzn.msg.success(res.msg, function() {
  51. tableId && table.reload(tableId);
  52. });
  53. }, function(data,res) {
  54. yzn.msg.error(res.msg, function () {
  55. tableId && table.reload(tableId);
  56. });
  57. })
  58. }
  59. if (typeof(that.attr('confirm')) == 'undefined') {
  60. func();
  61. } else {
  62. yzn.msg.confirm(title, func);
  63. }
  64. return false;
  65. });
  66. //单行表格删除(不刷新)
  67. $(document).on('click', '.layui-tr-del', function() {
  68. var that = $(this),
  69. tableId = $(this).data('table'),
  70. href = !that.attr('data-href') ? that.attr('href') : that.attr('data-href');
  71. tableId = tableId || init.table_render_id;
  72. layer.confirm('删除之后无法恢复,您确定要删除吗?', { icon: 3, title: '提示信息' }, function(index) {
  73. if (!href) {
  74. notice.info({ message: '请设置data-href参数' });
  75. return false;
  76. }
  77. $.post(href, function(res) {
  78. if (res.code == 1) {
  79. notice.success({ message: res.msg });
  80. tableId && table.reload(tableId);
  81. } else {
  82. notice.error({ message: res.msg });
  83. }
  84. });
  85. layer.close(index);
  86. });
  87. return false;
  88. });
  89. // 列表页批量操作按钮组
  90. $(document).on('click', '[data-batch-all]', function() {
  91. var that = $(this),
  92. tableId = that.attr('data-batch-all'),
  93. url = that.attr('data-href');
  94. tableId = tableId || init.table_render_id;
  95. url = url !== undefined ? url : window.location.href;
  96. var checkStatus = table.checkStatus(tableId),
  97. data = checkStatus.data;
  98. if (data.length <= 0) {
  99. yzn.msg.error('请选择要操作的数据');
  100. return false;
  101. }
  102. var ids = [];
  103. $.each(data, function(i, v) {
  104. ids.push(v.id);
  105. });
  106. var index = yzn.msg.confirm('您确定要执行此操作吗?', function() {
  107. yzn.request.post({
  108. url: url,
  109. data: {
  110. id: ids
  111. },
  112. }, function(data,res) {
  113. notice.success({ message: res.msg });
  114. tableId && table.reload(tableId);
  115. }, function(data,res) {
  116. notice.error({ message: res.msg });
  117. });
  118. layer.close(index);
  119. });
  120. return false;
  121. });
  122. // 监听动态表格刷新
  123. $(document).on('click', '[data-table-refresh]', function() {
  124. var tableId = $(this).attr('data-table-refresh');
  125. if (tableId === undefined || tableId === '' || tableId == null) {
  126. tableId = init.table_render_id;
  127. }
  128. table.reload(tableId);
  129. });
  130. //通用状态设置开关
  131. form.on('switch(switchStatus)', function(data) {
  132. var that = $(this),
  133. status = 0;
  134. if (!that.attr('data-href')) {
  135. notice.info({ message: '请设置data-href参数' });
  136. return false;
  137. }
  138. if (this.checked) {
  139. status = 1;
  140. }
  141. $.get(that.attr('data-href'), { value: status }, function(res) {
  142. if (res.code === 1) {
  143. notice.success({ message: res.msg });
  144. } else {
  145. notice.error({ message: res.msg });
  146. that.trigger('click');
  147. form.render('checkbox');
  148. }
  149. });
  150. });
  151. },
  152. render: function(options) {
  153. options.init = options.init || init;
  154. options.modifyReload = yzn.parame(options.modifyReload, false);
  155. options.id = options.id || options.init.table_render_id;
  156. options.elem = options.elem || options.init.table_elem;
  157. options.cols = options.cols || [];
  158. options.layFilter = options.id + '_LayFilter';
  159. options.url = options.url || options.init.index_url;
  160. options.search = yzn.parame(options.search, true);
  161. options.showSearch = yzn.parame(options.showSearch, true);
  162. options.searchFormVisible = yzn.parame(options.searchFormVisible, false);
  163. options.searchFormTpl = yzn.parame(options.searchFormTpl || options.init.searchFormTpl, false);
  164. options.defaultToolbar = options.defaultToolbar || ['filter', 'print', 'exports'];
  165. if (options.search && options.showSearch) {
  166. options.defaultToolbar.push({
  167. title: '搜索',
  168. layEvent: 'TABLE_SEARCH',
  169. icon: 'layui-icon-search',
  170. extend: 'data-table-id="' + options.id + '"'
  171. })
  172. }
  173. options.even = yzn.parame(options.even, true);
  174. // 判断是否为移动端
  175. if (yzn.checkMobile()) {
  176. options.defaultToolbar = !options.search ? ['filter'] : ['filter', {
  177. title: '搜索',
  178. layEvent: 'TABLE_SEARCH',
  179. icon: 'layui-icon-search',
  180. extend: 'data-table-id="' + options.id + '"'
  181. }];
  182. }
  183. options.searchInput = options.search ? yzn.parame(options.searchInput, true) : false;
  184. var tableDone = options.done || function() {};
  185. options.done = function(res, curr, count) {
  186. tableDone(res, curr, count);
  187. };
  188. // 判断元素对象是否有嵌套的
  189. options.cols = yznTable.formatCols(options.cols, options.init);
  190. // 初始化表格lay-filter
  191. $(options.elem).attr('lay-filter', options.layFilter);
  192. //自定义搜索
  193. if (options.search === true && options.searchFormTpl !== false) {
  194. data = options.tpldata || {}
  195. laytpl($('#' + options.searchFormTpl).html()).render(data, function (html) {
  196. $(options.elem).before(html);
  197. yznTable.listenTableSearch(options.id);
  198. })
  199. // 初始化form表单
  200. form.render();
  201. }
  202. // 初始化表格搜索
  203. if (options.search === true && options.searchFormTpl === false) {
  204. yznTable.renderSearch(options.cols, options.elem, options.id, options.searchFormVisible);
  205. }
  206. // 初始化表格左上方工具栏
  207. options.toolbar = options.toolbar || ['refresh', 'add', 'delete', 'export'];
  208. options.toolbar = yznTable.renderToolbar(options);
  209. var newTable = table.render(options);
  210. // 监听表格搜索开关显示
  211. yznTable.listenToolbar(options.layFilter, options.id);
  212. // 监听表格开关切换
  213. yznTable.renderSwitch(options.cols, options.init, options.id, options.modifyReload);
  214. // 监听表格文本框编辑
  215. yznTable.listenEdit(options.init, options.layFilter, options.id, options.modifyReload);
  216. return newTable;
  217. },
  218. renderToolbar: function(options) {
  219. var d = options.toolbar,
  220. tableId = options.id,
  221. searchInput = options.searchInput,
  222. elem = options.elem,
  223. init = options.init;
  224. d = d || [];
  225. var toolbarHtml = '';
  226. $.each(d, function(i, v) {
  227. if (v === 'refresh') {
  228. toolbarHtml += '<button class="layui-btn layui-btn-sm yzn-btn-primary" data-table-refresh="' + tableId + '"><i class="iconfont icon-shuaxin1"></i> </button>\n';
  229. } else if (v === 'add') {
  230. if (yzn.checkAuth('add', elem)) {
  231. toolbarHtml += '<button class="layui-btn layui-btn-normal layui-btn-sm" data-open="' + init.add_url + '" data-title="添加"><i class="iconfont icon-add"></i> 添加</button>\n';
  232. }
  233. } else if (v === 'delete') {
  234. if (yzn.checkAuth('delete', elem)) {
  235. toolbarHtml += '<button class="layui-btn layui-btn-sm layui-btn-danger" data-href="' + init.delete_url + '" data-batch-all="' + tableId + '"><i class="iconfont icon-trash"></i> 删除</button>\n';
  236. }
  237. } else if (v === 'recyclebin') {
  238. if (yzn.checkAuth('recyclebin', elem)) {
  239. toolbarHtml += '<button class="layui-btn layui-btn-warm layui-btn-sm" data-open="' + init.recyclebin_url + '" data-title="回收站"><i class="iconfont icon-recycle-line"></i> 回收站</button>\n';
  240. }
  241. } else if (v === 'restore') {
  242. if (yzn.checkAuth('restore', elem)) {
  243. toolbarHtml += '<button class="layui-btn layui-btn-sm confirm" data-href="' + init.restore_url + '" data-batch-all="' + tableId + '"><i class="iconfont icon-undo"></i> 还原</button>\n';
  244. }
  245. } else if (v === 'destroy') {
  246. if (yzn.checkAuth('destroy', elem)) {
  247. toolbarHtml += '<button class="layui-btn layui-btn-sm confirm layui-btn-danger" data-href="' + init.destroy_url + '" data-batch-all="' + tableId + '"><i class="iconfont icon-close"></i> 销毁</button>\n';
  248. }
  249. } else if (typeof v === "object") {
  250. $.each(v, function(ii, vv) {
  251. vv.class = vv.class || '';
  252. vv.icon = vv.icon || '';
  253. vv.auth = vv.auth || '';
  254. vv.url = vv.url || '';
  255. vv.method = vv.method || 'open';
  256. vv.title = vv.title || vv.text;
  257. vv.text = vv.text || vv.title;
  258. vv.extend = vv.extend || '';
  259. vv.checkbox = vv.checkbox || false;
  260. vv.html = vv.html || '';
  261. if (yzn.checkAuth(vv.auth, elem)) {
  262. toolbarHtml += yznTable.buildToolbarHtml(vv, tableId);
  263. }
  264. });
  265. }
  266. });
  267. if (searchInput) {
  268. toolbarHtml += '<input id="layui-input-search" value="" placeholder="搜索" class="layui-input layui-hide-xs" style="display:inline-block;width:auto;float: right;\n' + 'margin:2px 25px 0 0;height:28px;">\n'
  269. }
  270. return '<div>' + toolbarHtml + '</div>';
  271. },
  272. buildToolbarHtml: function(toolbar, tableId) {
  273. var html = '';
  274. toolbar.class = toolbar.class || '';
  275. toolbar.icon = toolbar.icon || '';
  276. toolbar.auth = toolbar.auth || '';
  277. toolbar.url = toolbar.url || '';
  278. toolbar.extend = toolbar.extend || '';
  279. toolbar.method = toolbar.method || 'open';
  280. toolbar.field = toolbar.field || 'id';
  281. toolbar.title = toolbar.title || toolbar.text;
  282. toolbar.text = toolbar.text || toolbar.title;
  283. toolbar.checkbox = toolbar.checkbox || false;
  284. toolbar.html = toolbar.html || '';
  285. if (toolbar.html !== '') {
  286. return toolbar.html;
  287. }
  288. var formatToolbar = toolbar;
  289. formatToolbar.icon = formatToolbar.icon !== '' ? '<i class="' + formatToolbar.icon + '"></i> ' : '';
  290. formatToolbar.class = formatToolbar.class !== '' ? 'class="' + formatToolbar.class + '" ' : '';
  291. if (toolbar.method === 'open') {
  292. formatToolbar.method = formatToolbar.method !== '' ? 'data-open="' + formatToolbar.url + '" data-title="' + formatToolbar.title + '" ' : '';
  293. } else {
  294. formatToolbar.method = formatToolbar.method !== '' ? 'data-request="' + formatToolbar.url + '" data-title="' + formatToolbar.title + '" ' : '';
  295. }
  296. formatToolbar.checkbox = toolbar.checkbox ? ' data-checkbox="true" ' : '';
  297. formatToolbar.tableId = tableId !== undefined ? ' data-table="' + tableId + '" ' : '';
  298. html = '<button ' + formatToolbar.class + formatToolbar.method + formatToolbar.extend + formatToolbar.checkbox + formatToolbar.tableId + '>' + formatToolbar.icon + formatToolbar.text + '</button>\n';
  299. return html;
  300. },
  301. renderSearch: function(cols, elem, tableId, searchFormVisible) {
  302. // TODO 只初始化第一个table搜索字段,如果存在多个(绝少数需求),得自己去扩展
  303. cols = cols[0] || {};
  304. var newCols = [];
  305. var formHtml = '';
  306. $.each(cols, function(i, d) {
  307. d.field = d.field || false;
  308. d.fieldAlias = yzn.parame(d.fieldAlias, d.field);
  309. d.title = d.title || d.field || '';
  310. d.selectList = d.selectList || {};
  311. d.search = yzn.parame(d.search, true);
  312. d.searchTip = d.searchTip || '请输入' + d.title || '';
  313. d.searchValue = d.searchValue || '';
  314. d.searchOp = d.searchOp || '=';
  315. d.timeType = d.timeType || 'datetime';
  316. d.extend = typeof d.extend === 'undefined' ? '' : d.extend;
  317. d.addClass = typeof d.addClass === 'undefined' ? (typeof d.addclass === 'undefined' ? 'layui-input' : 'layui-input ' + d.addclass) : 'layui-input ' + d.addClass;
  318. if (d.field !== false && d.search !== false) {
  319. ColumnsForSearch.push(d);
  320. switch (d.search) {
  321. case true:
  322. formHtml += '\t<div class="layui-form-item layui-inline">\n' +
  323. '<label class="layui-form-label">' + d.title + '</label>\n' +
  324. '<div class="layui-input-inline">\n' +
  325. '<input type="hidden" class="operate" name="' + d.fieldAlias + '-operate" data-name="' + d.fieldAlias + '" value="' + d.searchOp + '" readonly>\n' +
  326. '<input class="' + d.addClass + '" id="c-' + d.fieldAlias + '" name="' + d.fieldAlias + '" value="' + d.searchValue + '" placeholder="' + d.searchTip + '" ' + d.extend + '>\n' +
  327. '</div>\n' +
  328. '</div>';
  329. break;
  330. case 'select':
  331. //d.searchOp = '=';
  332. var selectHtml = '';
  333. $.each(d.selectList, function(sI, sV) {
  334. var selected = '';
  335. if (sI === d.searchValue) {
  336. selected = 'selected=""';
  337. }
  338. selectHtml += '<option value="' + sI + '" ' + selected + '>' + sV + '</option>/n';
  339. });
  340. formHtml += '\t<div class="layui-form-item layui-inline">\n' +
  341. '<label class="layui-form-label">' + d.title + '</label>\n' +
  342. '<div class="layui-input-inline">\n' +
  343. '<input type="hidden" class="operate" name="' + d.fieldAlias + '-operate" data-name="' + d.fieldAlias + '" value="' + d.searchOp + '" readonly>\n' +
  344. '<select class="layui-select" id="c-' + d.fieldAlias + '" name="' + d.fieldAlias + '" ' + d.extend + '>\n' +
  345. '<option value="">- 全部 -</option> \n' +
  346. selectHtml +
  347. '</select>\n' +
  348. '</div>\n' +
  349. '</div>';
  350. break;
  351. case 'range':
  352. d.searchOp = 'range';
  353. formHtml += '\t<div class="layui-form-item layui-inline">\n' +
  354. '<label class="layui-form-label">' + d.title + '</label>\n' +
  355. '<div class="layui-input-inline">\n' +
  356. '<input type="hidden" class="operate" name="' + d.fieldAlias + '-operate" data-name="' + d.fieldAlias + '" value="' + d.searchOp + '" readonly>\n' +
  357. '<input class="datetime ' + d.addClass + '" data-date-range="-" id="c-' + d.fieldAlias + '" name="' + d.fieldAlias + '" value="' + d.searchValue + '" placeholder="' + d.searchTip + '" ' + d.extend + '>\n' +
  358. '</div>\n' +
  359. '</div>';
  360. break;
  361. case 'time':
  362. d.searchOp = '=';
  363. formHtml += '\t<div class="layui-form-item layui-inline">\n' +
  364. '<label class="layui-form-label">' + d.title + '</label>\n' +
  365. '<div class="layui-input-inline">\n' +
  366. '<input class="datetime ' + d.addClass + '" id="c-' + d.fieldAlias + '" name="' + d.fieldAlias + '" data-search-op="' + d.searchOp + '" value="' + d.searchValue + '" placeholder="' + d.searchTip + '" ' + d.extend + '>\n' +
  367. '</div>\n' +
  368. '</div>';
  369. break;
  370. case 'between':
  371. d.searchOp = 'BETWEEN';
  372. formHtml += '\t<div class="layui-form-item layui-inline">\n' +
  373. '<input type="hidden" class="operate" name="' + d.fieldAlias + '-operate" data-name="' + d.fieldAlias + '" value="' + d.searchOp + '" readonly>\n' +
  374. '<label class="layui-form-label">' + d.title + '</label>\n' +
  375. '<div class="layui-input-inline" style="width: 80px;">\n' +
  376. '<input type="text" name="' + d.fieldAlias + '" id="' + d.fieldAlias + '-min" placeholder="' + d.searchTip + '" autocomplete="off" class="' + d.addClass + '" ' + d.extend + '>\n' +
  377. '</div>\n' +
  378. '<div class="layui-form-mid">-</div>\n' +
  379. '<div class="layui-input-inline" style="width: 80px;">\n' +
  380. '<input type="text" name="' + d.fieldAlias + '" id="' + d.fieldAlias + '-min" placeholder="' + d.searchTip + '" autocomplete="off" class="' + d.addClass + '" ' + d.extend + '>\n' +
  381. '</div>\n' +
  382. '</div>';
  383. break;
  384. }
  385. //newCols.push(d);
  386. }
  387. });
  388. if (formHtml !== '') {
  389. $(elem).before('<fieldset style="border:1px solid #ddd;" id="searchFieldset_' + tableId + '" class="table-search-fieldset ' + (searchFormVisible ? "" : "layui-hide") + '">\n' +
  390. '<legend>条件搜索</legend>\n' +
  391. '<form class="layui-form layui-form-pane form-search form-commonsearch">\n' +
  392. formHtml +
  393. '<div class="layui-form-item layui-inline" style="margin-left: 115px">\n' +
  394. '<button type="submit" class="layui-btn layui-btn-normal" data-type="tableSearch" data-table="' + tableId + '" lay-submit lay-filter="' + tableId + '_filter"> 搜 索</button>\n' +
  395. '<button type="reset" class="layui-btn layui-btn-primary" data-table-reset="' + tableId + '"> 重 置 </button>\n' +
  396. ' </div>' +
  397. '</form>' +
  398. '</fieldset>');
  399. yznTable.listenTableSearch(tableId);
  400. // 初始化form表单
  401. form.render();
  402. }
  403. },
  404. formatCols: function(cols, init) {
  405. for (i in cols) {
  406. var col = cols[i];
  407. for (index in col) {
  408. var val = col[index];
  409. // 判断是否包含初始化数据
  410. if (val.init === undefined) {
  411. cols[i][index]['init'] = init;
  412. }
  413. // 格式化列操作栏
  414. if (val.templet === yznTable.formatter.tool && val.operat === undefined) {
  415. cols[i][index]['operat'] = ['edit', 'delete'];
  416. }
  417. // 判断是否包含开关组件
  418. if (val.templet === yznTable.formatter.switch && val.filter === undefined) {
  419. cols[i][index]['filter'] = val.field;
  420. }
  421. // 判断是否含有搜索下拉列表
  422. if (val.selectList !== undefined && val.search === undefined) {
  423. cols[i][index]['search'] = 'select';
  424. }
  425. // 判断是否初始化对齐方式
  426. if (val.align === undefined) {
  427. cols[i][index]['align'] = 'left';
  428. }
  429. // 部分字段开启排序
  430. var sortDefaultFields = ['id', 'sort'];
  431. if (val.sort === undefined && sortDefaultFields.indexOf(val.field) >= 0) {
  432. cols[i][index]['sort'] = true;
  433. }
  434. // 初始化图片高度
  435. if (val.templet === yznTable.formatter.image && val.imageHeight === undefined) {
  436. cols[i][index]['imageHeight'] = 30;
  437. }
  438. // 判断是否多层对象
  439. if (val.field !== undefined && val.field.split(".").length > 1) {
  440. if (val.templet === undefined) {
  441. cols[i][index]['templet'] = yznTable.formatter.value;
  442. }
  443. }
  444. // 判断是否列表数据转换
  445. /*if (val.selectList !== undefined && val.templet === undefined) {
  446. cols[i][index]['templet'] = yznTable.formatter.list;
  447. }*/
  448. }
  449. }
  450. return cols;
  451. },
  452. listenTableSearch: function(tableId) {
  453. var that = this;
  454. that.$commonsearch = $(".table-search-fieldset");
  455. layui.define(['yznForm'], function(exports) {
  456. var yznForm = layui.yznForm;
  457. yznForm.bindevent(that.$commonsearch);
  458. })
  459. form.on('submit(' + tableId + '_filter)', function(data) {
  460. var searchQuery = yznTable.getSearchQuery(that, true);
  461. table.reload(tableId, {
  462. page: {
  463. curr: 1
  464. },
  465. where: {
  466. filter: JSON.stringify(searchQuery.filter),
  467. op: JSON.stringify(searchQuery.op)
  468. }
  469. });
  470. return false;
  471. })
  472. //表格点击搜索
  473. $(document).on("click", ".searchit", function () {
  474. var value = $(this).data("value");
  475. var field = $(this).data("field");
  476. var obj = $("form [name='" + field + "']", that.$commonsearch);
  477. if (obj.length > 0) {
  478. if (obj.is("select")) {
  479. $("option[value='" + value + "']", obj).prop("selected", true);
  480. form.render('select');
  481. } else if (obj.length > 1) {
  482. $("form [name='" + field + "'][value='" + value + "']", that.$commonsearch).prop("checked", true);
  483. } else {
  484. obj.val(value + "");
  485. }
  486. obj.trigger("change");
  487. $("form button[type='submit']",that.$commonsearch).trigger("click");
  488. }
  489. });
  490. //快速搜索
  491. $(document).on('blur', '#layui-input-search', function(event) {
  492. var text = $(this).val();
  493. table.reload(tableId, { where: { search: text } });
  494. $('#layui-input-search').prop("value", $(this).val());
  495. return false
  496. })
  497. },
  498. getSearchQuery: function(that, removeempty) {
  499. var op = {};
  500. var filter = {};
  501. var value = '';
  502. $("form.form-commonsearch .operate", that.$commonsearch).each(function(i) {
  503. var name = $(this).data("name");
  504. var sym = $(this).is("select") ? $("option:selected", this).val() : $(this).val().toUpperCase();
  505. var obj = $("[name='" + name + "']", that.$commonsearch);
  506. if (obj.length == 0)
  507. return true;
  508. var vObjCol = ColumnsForSearch[i];
  509. var process = vObjCol && typeof vObjCol.process == 'function' ? vObjCol.process : null;
  510. if (obj.length > 1) {
  511. if (/BETWEEN$/.test(sym)) {
  512. var value_begin = $.trim($("[name='" + name + "']:first", that.$commonsearch).val()),
  513. value_end = $.trim($("[name='" + name + "']:last", that.$commonsearch).val());
  514. if (value_begin.length || value_end.length) {
  515. if (process) {
  516. value_begin = process(value_begin, 'begin');
  517. value_end = process(value_end, 'end');
  518. }
  519. value = value_begin + ',' + value_end;
  520. } else {
  521. value = '';
  522. }
  523. //如果是时间筛选,将operate置为RANGE
  524. if ($("[name='" + name + "']:first", that.$commonsearch).hasClass("datetimepicker")) {
  525. sym = 'RANGE';
  526. }
  527. } else {
  528. value = $("[name='" + name + "']:checked", that.$commonsearch).val();
  529. value = process ? process(value) : value;
  530. }
  531. } else {
  532. value = process ? process(obj.val()) : obj.val();
  533. }
  534. if (removeempty && (value == '' || value == null || ($.isArray(value) && value.length == 0)) && !sym.match(/null/i)) {
  535. return true;
  536. }
  537. op[name] = sym;
  538. filter[name] = value;
  539. });
  540. return { op: op, filter: filter };
  541. },
  542. listenToolbar: function(layFilter, tableId) {
  543. table.on('toolbar(' + layFilter + ')', function(obj) {
  544. // 搜索表单的显示
  545. switch (obj.event) {
  546. case 'TABLE_SEARCH':
  547. var searchFieldsetId = 'searchFieldset_' + tableId;
  548. var _that = $("#" + searchFieldsetId);
  549. if (_that.hasClass("layui-hide")) {
  550. _that.removeClass('layui-hide');
  551. } else {
  552. _that.addClass('layui-hide');
  553. }
  554. break;
  555. }
  556. });
  557. },
  558. renderSwitch: function (cols, tableInit, tableId, modifyReload) {
  559. tableInit.modify_url = tableInit.modify_url || false;
  560. cols = cols[0] || {};
  561. tableId = tableId || init.table_render_id;
  562. if (cols.length > 0) {
  563. $.each(cols, function (i, v) {
  564. v.filter = v.filter || false;
  565. if (v.filter !== false && tableInit.modify_url !== false) {
  566. yznTable.listenSwitch({filter: v.filter, url: tableInit.modify_url, tableId: tableId, modifyReload: modifyReload});
  567. }
  568. });
  569. }
  570. },
  571. listenSwitch: function (option, ok) {
  572. option.filter = option.filter || '';
  573. option.url = option.url || '';
  574. option.field = option.field || option.filter || '';
  575. option.tableId = option.tableId || init.table_render_id;
  576. option.modifyReload = option.modifyReload || false;
  577. form.on('switch(' + option.filter + ')', function (obj) {
  578. var checked = obj.elem.checked ? 1 : 0;
  579. if (typeof ok === 'function') {
  580. return ok({
  581. id: obj.value,
  582. checked: checked,
  583. });
  584. } else {
  585. var data = {
  586. id: obj.value,
  587. param: option.field,
  588. value: checked,
  589. };
  590. yzn.request.post({
  591. url: option.url,
  592. prefix: true,
  593. data: data,
  594. }, function (data,res) {
  595. notice.success({ message: res.msg });
  596. if (option.modifyReload) {
  597. table.reload(option.tableId);
  598. }
  599. }, function (data,res) {
  600. yzn.msg.error(res.msg, function () {
  601. table.reload(option.tableId);
  602. });
  603. }, function () {
  604. table.reload(option.tableId);
  605. });
  606. }
  607. });
  608. },
  609. listenEdit: function(tableInit, layFilter, tableId, modifyReload) {
  610. tableInit.modify_url = tableInit.modify_url || false;
  611. tableId = tableId || init.table_render_id;
  612. if (tableInit.modify_url !== false) {
  613. table.on('edit(' + layFilter + ')', function(obj) {
  614. var value = obj.value,
  615. data = obj.data,
  616. id = data.id,
  617. field = obj.field;
  618. var _data = {
  619. id: id,
  620. param: field,
  621. value: value,
  622. };
  623. yzn.request.post({
  624. url: tableInit.modify_url,
  625. prefix: true,
  626. data: _data,
  627. }, function(data,res) {
  628. notice.success({ message: res.msg });
  629. if (modifyReload) {
  630. table.reload(tableId);
  631. }
  632. }, function(data,res) {
  633. yzn.msg.error(res.msg, function() {
  634. table.reload(tableId);
  635. });
  636. }, function() {
  637. table.reload(tableId);
  638. });
  639. });
  640. }
  641. },
  642. toolSpliceUrl(url, field, data) {
  643. data.id = typeof data.id !== 'undefined' ? data.id : 0;
  644. url = url == null || url.length === 0 ? '' : url.toString();
  645. //自动添加id参数
  646. url = !url.match(/(?=([?&]id=)|(\/id\/)|(\{id}))/i) ?
  647. url + (url.match(/(\?|&)+/) ? "&id=" : "?id=") + '{id}' : url;
  648. url = url.replace(/\{(.*?)\}/gi, function (matched) {
  649. matched = matched.substring(1, matched.length - 1);
  650. if (matched.indexOf(".") !== -1) {
  651. var temp = data;
  652. var arr = matched.split(/\./);
  653. for (var i = 0; i < arr.length; i++) {
  654. if (typeof temp[arr[i]] !== 'undefined') {
  655. temp = temp[arr[i]];
  656. }
  657. }
  658. return typeof temp === 'object' ? '' : temp;
  659. }
  660. return data[matched];
  661. });
  662. return url;
  663. },
  664. buildOperatHtml: function(operat) {
  665. var html = '';
  666. operat.class = operat.class || '';
  667. operat.icon = operat.icon || '';
  668. //operat.auth = operat.auth || '';
  669. operat.url = operat.url || '';
  670. operat.extend = operat.extend || '';
  671. operat.method = operat.method || 'open';
  672. operat.field = operat.field || 'id';
  673. operat.title = operat.title || operat.text;
  674. operat.text = operat.text || operat.title;
  675. var formatOperat = operat;
  676. formatOperat.icon = formatOperat.icon !== '' ? '<i class="' + formatOperat.icon + '"></i> ' : '';
  677. formatOperat.class = formatOperat.class !== '' ? 'class="' + formatOperat.class + '" ' : '';
  678. if (operat.method === 'open') {
  679. formatOperat.method = formatOperat.method !== '' ? 'data-open="' + formatOperat.url + '" data-title="' + formatOperat.title + '" ' : '';
  680. } else if (operat.method === 'href'){
  681. formatOperat.method = formatOperat.method !== '' ? 'href="' + formatOperat.url + '" data-title="' + formatOperat.title + '" ' : '';
  682. } else if (operat.method === 'none'){ // 常用于与extend配合,自定义监听按钮
  683. formatOperat.method = '';
  684. } else {
  685. formatOperat.method = formatOperat.method !== '' ? 'data-request="' + formatOperat.url + '" data-title="' + formatOperat.title + '" ' : '';
  686. }
  687. html = '<a ' + formatOperat.class + formatOperat.method + formatOperat.extend + '>' + formatOperat.icon + formatOperat.text + '</a>';
  688. return html;
  689. },
  690. getItemField: function (item, field) {
  691. var value = item;
  692. if (typeof field !== 'string' || item.hasOwnProperty(field)) {
  693. return item[field];
  694. }
  695. var props = field.split('.');
  696. for (var p in props) {
  697. if (props.hasOwnProperty(p)) {
  698. value = value && value[props[p]];
  699. }
  700. }
  701. return value;
  702. },
  703. formatter: {
  704. tool: function(data) {
  705. var that = this;
  706. that.operat = that.operat || ['edit', 'delete'];
  707. var elem = that.init.table_elem || init.table_elem;
  708. var html = '';
  709. $.each(that.operat, function(i, item) {
  710. if (typeof item === 'string') {
  711. switch (item) {
  712. case 'edit':
  713. var operat = {
  714. class: 'layui-btn layui-btn-success layui-btn-xs',
  715. method: 'open',
  716. field: 'id',
  717. icon: '',
  718. auth: 'edit',
  719. text: "<i class='iconfont icon-brush_fill'></i>",
  720. title: '编辑信息',
  721. url: that.init.edit_url,
  722. extend: ""
  723. };
  724. operat.url = yznTable.toolSpliceUrl(operat.url, operat.field, data);
  725. if (yzn.checkAuth(operat.auth, elem)) {
  726. html += yznTable.buildOperatHtml(operat);
  727. }
  728. break;
  729. case 'restore':
  730. var operat = {
  731. class: 'layui-btn layui-btn-xs',
  732. method: 'request',
  733. field: 'id',
  734. icon: '',
  735. auth: 'restore',
  736. text: "<i class='iconfont icon-undo'></i> 还原",
  737. title: '还原',
  738. url: that.init.restore_url,
  739. extend: ""
  740. };
  741. operat.url = yznTable.toolSpliceUrl(operat.url, operat.field, data);
  742. if (yzn.checkAuth(operat.auth, elem)) {
  743. html += yznTable.buildOperatHtml(operat);
  744. }
  745. break;
  746. case 'destroy':
  747. var operat = {
  748. class: 'layui-btn layui-btn-danger layui-btn-xs',
  749. method: 'request',
  750. field: 'id',
  751. icon: '销毁',
  752. auth: 'destroy',
  753. text: "<i class='iconfont icon-close'></i> 销毁",
  754. title: '',
  755. url: that.init.destroy_url,
  756. extend: ""
  757. };
  758. operat.url = yznTable.toolSpliceUrl(operat.url, operat.field, data);
  759. if (yzn.checkAuth(operat.auth, elem)) {
  760. html += yznTable.buildOperatHtml(operat);
  761. }
  762. break;
  763. case 'delete':
  764. var operat = {
  765. class: 'layui-btn layui-btn-danger layui-btn-xs layui-tr-del',
  766. method: 'href',
  767. field: 'id',
  768. icon: '',
  769. auth: 'delete',
  770. text: "<i class='iconfont icon-trash_fill'></i>",
  771. title: '',
  772. url: that.init.delete_url,
  773. extend: ""
  774. };
  775. operat.url = yznTable.toolSpliceUrl(operat.url, operat.field, data);
  776. if (yzn.checkAuth(operat.auth, elem)) {
  777. html += yznTable.buildOperatHtml(operat);
  778. }
  779. break;
  780. }
  781. } else if (typeof item === 'object') {
  782. $.each(item, function(i, operat) {
  783. hidden = typeof operat.hidden === 'function' ? operat.hidden.call(yznTable, data, operat) : (typeof operat.hidden !== 'undefined' ? operat.hidden : false);
  784. if (hidden) {
  785. return true;
  786. }
  787. url = operat.url || '';
  788. operat.class = operat.class || '';
  789. operat.icon = operat.icon || '';
  790. operat.auth = operat.auth || '';
  791. operat.method = operat.method || 'open';
  792. operat.field = operat.field || 'id';
  793. operat.text = typeof operat.text === 'function' ? operat.text.call(yznTable, data, operat) : operat.text ? operat.text : '';
  794. operat.title = typeof operat.title === 'function' ? operat.title.call(yznTable, data, operat) : operat.title ? operat.title : operat.text;
  795. operat.extend = operat.extend || '';
  796. operat.url = typeof url === 'function' ? url.call(yznTable, data, operat) : (url ? yznTable.toolSpliceUrl(url, operat.field, data) : 'javascript:;');
  797. disable = typeof operat.disable === 'function' ? operat.disable.call(yznTable, data, operat) : (typeof operat.disable !== 'undefined' ? operat.disable : false);
  798. if (disable) {
  799. operat.class = operat.class + ' layui-btn-disabled';
  800. }
  801. //operat.url = yznTable.toolSpliceUrl(operat.url, operat.field, data);
  802. if (yzn.checkAuth(operat.auth, elem)) {
  803. html += yznTable.buildOperatHtml(operat);
  804. }
  805. });
  806. }
  807. });
  808. return html;
  809. },
  810. status: function (data) {
  811. var custom = {normal: 'success', hidden: 'gray', deleted: 'danger', locked: 'info'};
  812. if (typeof this.custom !== 'undefined') {
  813. custom = $.extend(custom, this.custom);
  814. }
  815. this.custom = custom;
  816. this.icon = 'iconfont icon-circle-fill';
  817. return yznTable.formatter.normal.call(this, data);
  818. },
  819. normal: function (data) {
  820. var that = this;
  821. var colorArr = ["danger", "success", "primary", "warning", "info", "gray", "red", "yellow", "aqua", "blue", "navy", "teal", "olive", "lime", "fuchsia", "purple", "maroon"];
  822. var custom = {};
  823. if (typeof that.custom !== 'undefined') {
  824. custom = $.extend(custom, that.custom);
  825. }
  826. var field = that.field;
  827. try {
  828. var value = yznTable.getItemField(data,field);
  829. value = value == null || value.length === 0 ? '' : value.toString();
  830. } catch (e) {
  831. var value = undefined;
  832. }
  833. value = value == null || value.length === 0 ? '' : value.toString();
  834. var keys = typeof that.selectList === 'object' ? Object.keys(that.selectList) : [];
  835. var index = keys.indexOf(value);
  836. var color = value && typeof custom[value] !== 'undefined' ? custom[value] : null;
  837. var display = index > -1 ? that.selectList[value] : null;
  838. var icon = typeof that.icon !== 'undefined' ? that.icon : null;
  839. if (!color) {
  840. color = index > -1 && typeof colorArr[index] !== 'undefined' ? colorArr[index] : 'primary';
  841. }
  842. if (!display) {
  843. display = value.charAt(0).toUpperCase() + value.slice(1);
  844. }
  845. var html = '<span class="text-' + color + '">' + (icon ? '<i class="' + icon + '"></i>' : '') + display + '</span>';
  846. if (that.search != false) {
  847. html = '<a href="javascript:;" class="searchit" lay-tips="点击搜索 ' + display + '" data-field="' + this.field + '" data-value="' + value + '">' + html + '</a>';
  848. }
  849. return html;
  850. },
  851. flag: function (data) {
  852. var that = this;
  853. var field = that.field;
  854. try {
  855. var value = yznTable.getItemField(data,field);
  856. value = value == null || value.length === 0 ? '' : value.toString();
  857. } catch (e) {
  858. var value = undefined;
  859. }
  860. //赤色 墨绿 蓝色 藏青 雅黑 橙色
  861. var colorArr = {0:'red',1:'green',2:'blue',3:'cyan',4:'black',5:'orange'};
  862. //如果字段列有定义custom
  863. if (typeof that.custom !== 'undefined') {
  864. colorArr = $.extend(colorArr, that.custom);
  865. }
  866. if (typeof that.selectList === 'object' && typeof that.custom === 'undefined') {
  867. var i = 0;
  868. var searchValues = Object.values(colorArr);
  869. $.each(that.selectList, function (key, val) {
  870. if (typeof colorArr[key] == 'undefined') {
  871. colorArr[key] = searchValues[i];
  872. i = typeof searchValues[i + 1] === 'undefined' ? 0 : i + 1;
  873. }
  874. });
  875. }
  876. //渲染Flag
  877. var html = [];
  878. var arr = value != '' ? value.split(',') : [];
  879. var color, display, label;
  880. $.each(arr, function (i, value) {
  881. value = value == null || value.length === 0 ? '' : value.toString();
  882. if (value == '')
  883. return true;
  884. color = value && typeof colorArr[value] !== 'undefined' ? colorArr[value] : 'green';
  885. display = typeof that.selectList !== 'undefined' && typeof that.selectList[value] !== 'undefined' ? that.selectList[value] : value.charAt(0).toUpperCase() + value.slice(1);
  886. label = '<span class="layui-badge layui-bg-' + color + '">' + display + '</span>';
  887. if (that.search != false) {
  888. html.push('<a href="javascript:;" class="searchit" lay-tips="点击搜索 ' + display + '" data-field="' + field + '" data-value="' + value + '">' + label + '</a>');
  889. } else {
  890. html.push(label);
  891. }
  892. })
  893. return html.join(' ');
  894. },
  895. label: function (data) {
  896. return yznTable.formatter.flag.call(this, data);
  897. },
  898. switch: function (data) {
  899. var that = this;
  900. var field = that.field;
  901. that.filter = that.filter || that.field || null;
  902. that.checked = that.checked || 1;
  903. that.tips = that.tips || '开|关';
  904. try {
  905. var value = yznTable.getItemField(data,field);
  906. } catch (e) {
  907. var value = undefined;
  908. }
  909. var checked = value === that.checked ? 'checked' : '';
  910. return laytpl('<input type="checkbox" name="' + that.field + '" value="' + data.id + '" lay-skin="switch" lay-text="' + that.tips + '" lay-filter="' + that.filter + '" ' + checked + ' >').render(data);
  911. },
  912. image: function(data) {
  913. var that = this;
  914. that.imageWidth = that.imageWidth || 80;
  915. that.imageHeight = that.imageHeight || 30;
  916. that.imageSplit = that.imageSplit || ',';
  917. that.imageJoin = that.imageJoin || ' ';
  918. that.title = that.title || that.field;
  919. var field = that.field,
  920. title = data[that.title];
  921. try {
  922. var value = yznTable.getItemField(data,field);
  923. } catch (e) {
  924. var value = undefined;
  925. }
  926. if (!value) {
  927. return '';
  928. } else {
  929. var valuesHtml = [];
  930. valuesHtml.push('<img style="max-width: ' + that.imageWidth + 'px; max-height: ' + that.imageHeight + 'px;" src="' + value + '" data-image="' + title + '">');
  931. return valuesHtml.join(that.imageJoin);
  932. }
  933. },
  934. images: function (data) {
  935. var that = this;
  936. that.imageWidth = that.imageWidth || 80;
  937. that.imageHeight = that.imageHeight || 30;
  938. that.imageSplit = that.imageSplit || ',';
  939. that.imageJoin = that.imageJoin || ' ';
  940. that.title = that.title || that.field;
  941. var field = that.field,
  942. title = data[that.title];
  943. try {
  944. var value = yznTable.getItemField(data,field);
  945. } catch (e) {
  946. var value = undefined;
  947. }
  948. if (!value) {
  949. return '';
  950. } else {
  951. var values = value.split(that.imageSplit),
  952. valuesHtml = [];
  953. values.forEach((value, index) => {
  954. valuesHtml.push('<img style="max-width: ' + that.imageWidth + 'px; max-height: ' + that.imageHeight + 'px;" src="' + value + '" data-image="' + title + '">');
  955. });
  956. return valuesHtml.join(that.imageJoin);
  957. }
  958. },
  959. file: function(data) {
  960. var that = this;
  961. that.imageWidth = that.imageWidth || 80;
  962. that.imageHeight = that.imageHeight || 30;
  963. that.imageSplit = that.imageSplit || ',';
  964. that.imageJoin = that.imageJoin || ' ';
  965. that.title = that.title || that.field;
  966. var field = that.field,
  967. title = data[that.title];
  968. try {
  969. var value = yznTable.getItemField(data,field);
  970. } catch (e) {
  971. var value = undefined;
  972. }
  973. if (!value) {
  974. return '';
  975. } else {
  976. var values = value.split(that.imageSplit),
  977. valuesHtml = [];
  978. values.forEach((value, index) => {
  979. suffix = /[\.]?([a-zA-Z0-9]+)$/.exec(value);
  980. suffix = suffix ? suffix[1] : 'file';
  981. url = yzn.request.fixurl("admin/ajax/icon?suffix=" + suffix);
  982. valuesHtml.push('<img style="max-width: ' + that.imageWidth + 'px; max-height: ' + that.imageHeight + 'px;" src="' + url + '" data-image="' + title + '">');
  983. });
  984. return valuesHtml.join(that.imageJoin);
  985. }
  986. },
  987. files: function (data) {
  988. return yznTable.formatter.image.file(this, data);
  989. },
  990. url: function(data) {
  991. var field = this.field;
  992. try {
  993. var value = yznTable.getItemField(data,field);
  994. } catch (e) {
  995. var value = undefined;
  996. }
  997. return '<a class="layui-btn layui-btn-primary layui-btn-xs" href="' + value + '" target="_blank"><i class="iconfont icon-lianjie"></i></a></a>';
  998. },
  999. price: function(data) {
  1000. var field = this.field;
  1001. try {
  1002. var value = yznTable.getItemField(data,field);
  1003. } catch (e) {
  1004. var value = undefined;
  1005. }
  1006. return '<span>¥' + value + '</span>';
  1007. },
  1008. icon: function(data) {
  1009. var field = this.field;
  1010. try {
  1011. var value = yznTable.getItemField(data,field);
  1012. } catch (e) {
  1013. var value = undefined;
  1014. }
  1015. return '<i class="' + value + '"></i>';
  1016. },
  1017. text: function(data) {
  1018. var field = this.field;
  1019. try {
  1020. var value = yznTable.getItemField(data,field);
  1021. } catch (e) {
  1022. var value = undefined;
  1023. }
  1024. return '<span class="line-limit-length">' + value + '</span>';
  1025. },
  1026. value: function(data) {
  1027. var field = this.field;
  1028. try {
  1029. var value = yznTable.getItemField(data,field);
  1030. } catch (e) {
  1031. var value = undefined;
  1032. }
  1033. return '<span>' + value + '</span>';
  1034. },
  1035. datetime: function (data) {
  1036. var that = this;
  1037. var field = that.field;
  1038. try {
  1039. var value = yznTable.getItemField(data,field);
  1040. } catch (e) {
  1041. var value = undefined;
  1042. }
  1043. var datetimeFormat = typeof that.datetimeFormat === 'undefined' ? 'yyyy-MM-dd HH:mm:ss' : that.datetimeFormat;
  1044. if (value && isNaN(Date.parse(value))) {
  1045. return layui.util.toDateString(value * 1000, datetimeFormat)
  1046. } else if (value && !isNaN(Date.parse(value))) {
  1047. return layui.util.toDateString(Date.parse(value), datetimeFormat)
  1048. } else {
  1049. return '-';
  1050. }
  1051. }
  1052. },
  1053. }
  1054. exports(MOD_NAME, yznTable);
  1055. });