require-table.js 61 KB

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