require-table.js 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785
  1. define(['jquery', 'bootstrap', 'moment', 'moment/locale/zh-cn', 'bootstrap-table', 'bootstrap-table-lang', 'bootstrap-table-export', 'bootstrap-table-commonsearch', 'bootstrap-table-template'], function ($, undefined, Moment) {
  2. var Table = {
  3. list: {},
  4. // Bootstrap-table 基础配置
  5. defaults: {
  6. url: '',
  7. sidePagination: 'server',
  8. method: 'get', //请求方法
  9. toolbar: ".toolbar", //工具栏
  10. search: true, //是否启用快速搜索
  11. cache: false,
  12. commonSearch: true, //是否启用通用搜索
  13. searchFormVisible: false, //是否始终显示搜索表单
  14. titleForm: '', //为空则不显示标题,不定义默认显示:普通搜索
  15. idTable: 'commonTable',
  16. showExport: true,
  17. exportDataType: "all",
  18. // exportTypes: ['json', 'xml', 'csv', 'txt', 'doc', 'excel'],
  19. exportTypes: ['csv','doc','excel','','',''],
  20. exportOptions: {
  21. fileName: 'export_' + Moment().format("YYYY-MM-DD"),
  22. ignoreColumn: [0, 'operate'] //默认不导出第一列(checkbox)与操作(operate)列
  23. },
  24. pageSize: 10,
  25. pageList: [10, 15, 25, 50, 'All'],
  26. pagination: true,
  27. clickToSelect: true, //是否启用点击选中
  28. dblClickToEdit: true, //是否启用双击编辑
  29. singleSelect: false, //是否启用单选
  30. showRefresh: false,
  31. locale: 'zh-CN',
  32. showToggle: true,
  33. showColumns: true,
  34. pk: 'id',
  35. sortName: 'id',
  36. sortOrder: 'desc',
  37. paginationFirstText: __("First"),
  38. paginationPreText: __("Previous"),
  39. paginationNextText: __("Next"),
  40. paginationLastText: __("Last"),
  41. cardView: false, //卡片视图
  42. checkOnInit: true, //是否在初始化时判断
  43. escape: true, //是否对内容进行转义
  44. extend: {
  45. index_url: '',
  46. add_url: '',
  47. edit_url: '',
  48. del_url: '',
  49. import_url: '',
  50. multi_url: '',
  51. dragsort_url: 'ajax/weigh',
  52. }
  53. },
  54. // Bootstrap-table 列配置
  55. columnDefaults: {
  56. align: 'center',
  57. valign: 'middle',
  58. },
  59. config: {
  60. // firsttd: 'tbody tr td:first-child:not(:has(div.card-views))',
  61. firsttd: false,
  62. toolbar: '.toolbar',
  63. refreshbtn: '.btn-refresh',
  64. addbtn: '.btn-add',
  65. editbtn: '.btn-edit',
  66. delbtn: '.btn-del',
  67. importbtn: '.btn-import',
  68. multibtn: '.btn-multi',
  69. disabledbtn: '.btn-disabled',
  70. editonebtn: '.btn-editone',
  71. restoreonebtn: '.btn-restoreone',
  72. destroyonebtn: '.btn-destroyone',
  73. restoreallbtn: '.btn-restoreall',
  74. destroyallbtn: '.btn-destroyall',
  75. dragsortfield: 'weigh',
  76. },
  77. button: {
  78. edit: {
  79. name: 'edit',
  80. icon: 'fa fa-pencil',
  81. title: __('Edit'),
  82. extend: 'data-toggle="tooltip"',
  83. classname: 'btn btn-xs btn-success btn-editone'
  84. },
  85. del: {
  86. name: 'del',
  87. icon: 'fa fa-trash',
  88. title: __('Del'),
  89. extend: 'data-toggle="tooltip"',
  90. classname: 'btn btn-xs btn-danger btn-delone'
  91. },
  92. dragsort: {
  93. name: 'dragsort',
  94. icon: 'fa fa-arrows',
  95. title: __('Drag to sort'),
  96. extend: 'data-toggle="tooltip"',
  97. classname: 'btn btn-xs btn-primary btn-dragsort'
  98. }
  99. },
  100. api: {
  101. init: function (defaults, columnDefaults, locales) {
  102. defaults = defaults ? defaults : {};
  103. columnDefaults = columnDefaults ? columnDefaults : {};
  104. locales = locales ? locales : {};
  105. // 如果是iOS设备则启用卡片视图
  106. if (navigator.userAgent.match(/(iPod|iPhone|iPad)/)) {
  107. Table.defaults.cardView = true;
  108. }
  109. // 写入bootstrap-table默认配置
  110. $.extend(true, $.fn.bootstrapTable.defaults, Table.defaults, defaults);
  111. // 写入bootstrap-table column配置
  112. $.extend($.fn.bootstrapTable.columnDefaults, Table.columnDefaults, columnDefaults);
  113. // 写入bootstrap-table locale配置
  114. $.extend($.fn.bootstrapTable.locales[Table.defaults.locale], {
  115. formatCommonSearch: function () {
  116. return __('Common search');
  117. },
  118. formatCommonSubmitButton: function () {
  119. return __('Submit');
  120. },
  121. formatCommonResetButton: function () {
  122. return __('Reset');
  123. },
  124. formatCommonCloseButton: function () {
  125. return __('Close');
  126. },
  127. formatCommonChoose: function () {
  128. return __('Choose');
  129. }
  130. }, locales);
  131. if (typeof defaults.exportTypes != 'undefined') {
  132. $.fn.bootstrapTable.defaults.exportTypes = defaults.exportTypes;
  133. }
  134. },
  135. // 绑定事件
  136. bindevent: function (table) {
  137. //Bootstrap-table的父元素,包含table,toolbar,pagnation
  138. var parenttable = table.closest('.bootstrap-table');
  139. //Bootstrap-table配置
  140. var options = table.bootstrapTable('getOptions');
  141. //Bootstrap操作区
  142. var toolbar = $(options.toolbar, parenttable);
  143. //当刷新表格时
  144. table.on('load-error.bs.table', function (status, res, e) {
  145. if (e.status === 0) {
  146. return;
  147. }
  148. Toastr.error(__('Unknown data format'));
  149. });
  150. //当加载数据成功时
  151. table.on('load-success.bs.table', function (e, data) {
  152. if (typeof data.rows === 'undefined' && typeof data.code != 'undefined') {
  153. Toastr.error(data.msg);
  154. }
  155. });
  156. //当刷新表格时
  157. table.on('refresh.bs.table', function (e, settings, data) {
  158. $(Table.config.refreshbtn, toolbar).find(".fa").addClass("fa-spin");
  159. });
  160. if (options.dblClickToEdit) {
  161. //当双击单元格时
  162. table.on('dbl-click-row.bs.table', function (e, row, element, field) {
  163. $(Table.config.editonebtn, element).trigger("click");
  164. });
  165. }
  166. //当内容渲染完成后
  167. table.on('post-body.bs.table', function (e, settings, json, xhr) {
  168. $(Table.config.refreshbtn, toolbar).find(".fa").removeClass("fa-spin");
  169. $(Table.config.disabledbtn, toolbar).toggleClass('disabled', true);
  170. if ($(Table.config.firsttd, table).find("input[type='checkbox'][data-index]").size() > 0) {
  171. // 挺拽选择,需要重新绑定事件
  172. require(['drag', 'drop'], function () {
  173. $(Table.config.firsttd, table).drag("start", function (ev, dd) {
  174. return $('<div class="selection" />').css('opacity', .65).appendTo(document.body);
  175. }).drag(function (ev, dd) {
  176. $(dd.proxy).css({
  177. top: Math.min(ev.pageY, dd.startY),
  178. left: Math.min(ev.pageX, dd.startX),
  179. height: Math.abs(ev.pageY - dd.startY),
  180. width: Math.abs(ev.pageX - dd.startX)
  181. });
  182. }).drag("end", function (ev, dd) {
  183. $(dd.proxy).remove();
  184. });
  185. $(Table.config.firsttd, table).drop("start", function () {
  186. Table.api.toggleattr(this);
  187. }).drop(function () {
  188. Table.api.toggleattr(this);
  189. }).drop("end", function () {
  190. Table.api.toggleattr(this);
  191. });
  192. $.drop({
  193. multi: true
  194. });
  195. });
  196. }
  197. });
  198. // 处理选中筛选框后按钮的状态统一变更
  199. table.on('check.bs.table uncheck.bs.table check-all.bs.table uncheck-all.bs.table', function () {
  200. var ids = Table.api.selectedids(table);
  201. $(Table.config.disabledbtn, toolbar).toggleClass('disabled', !ids.length);
  202. });
  203. // 绑定TAB事件
  204. $('.panel-heading ul[data-field] li a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
  205. var field = $(this).closest("ul").data("field");
  206. var value = $(this).data("value");
  207. $("select[name='" + field + "'] option[value='" + value + "']", table.closest(".bootstrap-table").find(".commonsearch-table")).prop("selected", true);
  208. table.bootstrapTable('refresh', {pageNumber: 1});
  209. return false;
  210. });
  211. // 刷新按钮事件
  212. $(toolbar).on('click', Table.config.refreshbtn, function () {
  213. table.bootstrapTable('refresh');
  214. });
  215. // 添加按钮事件
  216. $(toolbar).on('click', Table.config.addbtn, function () {
  217. var ids = Table.api.selectedids(table);
  218. var url = options.extend.add_url;
  219. if (url.indexOf("{ids}") !== -1) {
  220. url = Table.api.replaceurl(url, {ids: ids.length > 0 ? ids.join(",") : 0}, table);
  221. }
  222. Fast.api.open(url, __('Add'), $(this).data() || {});
  223. });
  224. // 导入按钮事件
  225. if ($(Table.config.importbtn, toolbar).size() > 0) {
  226. require(['upload'], function (Upload) {
  227. Upload.api.plupload($(Table.config.importbtn, toolbar), function (data, ret) {
  228. Fast.api.ajax({
  229. url: options.extend.import_url,
  230. data: {file: data.url},
  231. }, function (data, ret) {
  232. table.bootstrapTable('refresh');
  233. });
  234. });
  235. });
  236. }
  237. // 批量编辑按钮事件
  238. $(toolbar).on('click', Table.config.editbtn, function () {
  239. var that = this;
  240. //循环弹出多个编辑框
  241. $.each(table.bootstrapTable('getSelections'), function (index, row) {
  242. var url = options.extend.edit_url;
  243. row = $.extend({}, row ? row : {}, {ids: row[options.pk]});
  244. var url = Table.api.replaceurl(url, row, table);
  245. Fast.api.open(url, __('Edit'), $(that).data() || {});
  246. });
  247. });
  248. //清空回收站
  249. $(document).on('click', Table.config.destroyallbtn, function () {
  250. var that = this;
  251. Layer.confirm(__('Are you sure you want to truncate?'), function () {
  252. var url = $(that).data("url") ? $(that).data("url") : $(that).attr("href");
  253. Fast.api.ajax(url, function () {
  254. Layer.closeAll();
  255. table.bootstrapTable('refresh');
  256. }, function () {
  257. Layer.closeAll();
  258. });
  259. });
  260. return false;
  261. });
  262. //还原或删除
  263. $(document).on('click', Table.config.restoreallbtn + ',' + Table.config.restoreonebtn + ',' + Table.config.destroyonebtn, function () {
  264. var that = this;
  265. var url = $(that).data("url") ? $(that).data("url") : $(that).attr("href");
  266. Fast.api.ajax(url, function () {
  267. table.bootstrapTable('refresh');
  268. });
  269. return false;
  270. });
  271. // 批量操作按钮事件
  272. $(toolbar).on('click', Table.config.multibtn, function () {
  273. var ids = Table.api.selectedids(table);
  274. Table.api.multi($(this).data("action"), ids, table, this);
  275. });
  276. // 批量删除按钮事件
  277. $(toolbar).on('click', Table.config.delbtn, function () {
  278. var that = this;
  279. var ids = Table.api.selectedids(table);
  280. Layer.confirm(
  281. __('Are you sure you want to delete the %s selected item?', ids.length),
  282. {icon: 3, title: __('Warning'), offset: 0, shadeClose: true},
  283. function (index) {
  284. Table.api.multi("del", ids, table, that);
  285. Layer.close(index);
  286. }
  287. );
  288. });
  289. // 拖拽排序
  290. require(['dragsort'], function () {
  291. //绑定拖动排序
  292. $("tbody", table).dragsort({
  293. itemSelector: 'tr:visible',
  294. dragSelector: "a.btn-dragsort",
  295. dragEnd: function (a, b) {
  296. var element = $("a.btn-dragsort", this);
  297. var data = table.bootstrapTable('getData');
  298. var current = data[parseInt($(this).data("index"))];
  299. var options = table.bootstrapTable('getOptions');
  300. //改变的值和改变的ID集合
  301. var ids = $.map($("tbody tr:visible", table), function (tr) {
  302. return data[parseInt($(tr).data("index"))][options.pk];
  303. });
  304. var changeid = current[options.pk];
  305. var pid = typeof current.pid != 'undefined' ? current.pid : '';
  306. var params = {
  307. url: table.bootstrapTable('getOptions').extend.dragsort_url,
  308. data: {
  309. ids: ids.join(','),
  310. changeid: changeid,
  311. pid: pid,
  312. field: Table.config.dragsortfield,
  313. orderway: options.sortOrder,
  314. table: options.extend.table,
  315. pk: options.pk
  316. }
  317. };
  318. Fast.api.ajax(params, function (data, ret) {
  319. var success = $(element).data("success") || $.noop;
  320. if (typeof success === 'function') {
  321. if (false === success.call(element, data, ret)) {
  322. return false;
  323. }
  324. }
  325. table.bootstrapTable('refresh');
  326. }, function (data, ret) {
  327. var error = $(element).data("error") || $.noop;
  328. if (typeof error === 'function') {
  329. if (false === error.call(element, data, ret)) {
  330. return false;
  331. }
  332. }
  333. table.bootstrapTable('refresh');
  334. });
  335. },
  336. placeHolderTemplate: ""
  337. });
  338. });
  339. $(table).on("click", "input[data-id][name='checkbox']", function (e) {
  340. var ids = $(this).data("id");
  341. var row = Table.api.getrowbyid(table, ids);
  342. table.trigger('check.bs.table', [row, this]);
  343. });
  344. $(table).on("click", "[data-id].btn-change", function (e) {
  345. e.preventDefault();
  346. Table.api.multi($(this).data("action") ? $(this).data("action") : '', [$(this).data("id")], table, this);
  347. });
  348. $(table).on("click", "[data-id].btn-edit", function (e) {
  349. e.preventDefault();
  350. var ids = $(this).data("id");
  351. var row = Table.api.getrowbyid(table, ids);
  352. row.ids = ids;
  353. var url = Table.api.replaceurl(options.extend.edit_url, row, table);
  354. Fast.api.open(url, __('Edit'), $(this).data() || {});
  355. });
  356. $(table).on("click", "[data-id].btn-del", function (e) {
  357. e.preventDefault();
  358. var id = $(this).data("id");
  359. var that = this;
  360. Layer.confirm(
  361. __('Are you sure you want to delete this item?'),
  362. {icon: 3, title: __('Warning'), shadeClose: true},
  363. function (index) {
  364. Table.api.multi("del", id, table, that);
  365. Layer.close(index);
  366. }
  367. );
  368. });
  369. var id = table.attr("id");
  370. Table.list[id] = table;
  371. return table;
  372. },
  373. // 批量操作请求
  374. multi: function (action, ids, table, element) {
  375. var options = table.bootstrapTable('getOptions');
  376. var data = element ? $(element).data() : {};
  377. var ids = ($.isArray(ids) ? ids.join(",") : ids);
  378. var url = typeof data.url !== "undefined" ? data.url : (action == "del" ? options.extend.del_url : options.extend.multi_url);
  379. url = this.replaceurl(url, {ids: ids}, table);
  380. var params = typeof data.params !== "undefined" ? (typeof data.params == 'object' ? $.param(data.params) : data.params) : '';
  381. var options = {url: url, data: {action: action, ids: ids, params: params}};
  382. Fast.api.ajax(options, function (data, ret) {
  383. var success = $(element).data("success") || $.noop;
  384. if (typeof success === 'function') {
  385. if (false === success.call(element, data, ret)) {
  386. return false;
  387. }
  388. }
  389. table.bootstrapTable('refresh');
  390. }, function (data, ret) {
  391. var error = $(element).data("error") || $.noop;
  392. if (typeof error === 'function') {
  393. if (false === error.call(element, data, ret)) {
  394. return false;
  395. }
  396. }
  397. });
  398. },
  399. // 单元格元素事件
  400. events: {
  401. operate: {
  402. 'click .btn-editone': function (e, value, row, index) {
  403. e.stopPropagation();
  404. e.preventDefault();
  405. var table = $(this).closest('table');
  406. var options = table.bootstrapTable('getOptions');
  407. var ids = row[options.pk];
  408. row = $.extend({}, row ? row : {}, {ids: ids});
  409. var url = options.extend.edit_url;
  410. Fast.api.open(Table.api.replaceurl(url, row, table), __('Edit'), $(this).data() || {});
  411. },
  412. 'click .btn-delone': function (e, value, row, index) {
  413. e.stopPropagation();
  414. e.preventDefault();
  415. var that = this;
  416. var top = $(that).offset().top - $(window).scrollTop();
  417. var left = $(that).offset().left - $(window).scrollLeft() - 260;
  418. if (top + 154 > $(window).height()) {
  419. top = top - 154;
  420. }
  421. if ($(window).width() < 480) {
  422. top = left = undefined;
  423. }
  424. Layer.confirm(
  425. __('Are you sure you want to delete this item?'),
  426. {icon: 3, title: __('Warning'), offset: [top, left], shadeClose: true},
  427. function (index) {
  428. var table = $(that).closest('table');
  429. var options = table.bootstrapTable('getOptions');
  430. Table.api.multi("del", row[options.pk], table, that);
  431. Layer.close(index);
  432. }
  433. );
  434. }
  435. },//单元格图片预览
  436. image: {
  437. 'click .img-center': function (e, value, row, index) {
  438. var data = [];
  439. value = value.split(",");
  440. $.each(value, function (index, value) {
  441. data.push({
  442. src: Fast.api.cdnurl(value),
  443. });
  444. });
  445. Layer.photos({
  446. photos: {
  447. "start": $(this).parent().index(),
  448. "data": data
  449. },
  450. anim: 5 //0-6的选择,指定弹出图片动画类型,默认随机(请注意,3.0之前的版本用shift参数)
  451. });
  452. },
  453. }
  454. },
  455. // 单元格数据格式化
  456. formatter: {
  457. icon: function (value, row, index) {
  458. if (!value)
  459. return '';
  460. value = value === null ? '' : value.toString();
  461. value = value.indexOf(" ") > -1 ? value : "fa fa-" + value;
  462. //渲染fontawesome图标
  463. return '<i class="' + value + '"></i> ' + value;
  464. },
  465. image: function (value, row, index) {
  466. value = value ? value : '/assets/img/blank.gif';
  467. var classname = typeof this.classname !== 'undefined' ? this.classname : 'img-sm img-center';
  468. return '<a href="javascript:"><img class="' + classname + '" src="' + Fast.api.cdnurl(value) + '" /></a>';
  469. },
  470. images: function (value, row, index) {
  471. value = value === null ? '' : value.toString();
  472. var classname = typeof this.classname !== 'undefined' ? this.classname : 'img-sm img-center';
  473. var arr = value.split(',');
  474. var html = [];
  475. $.each(arr, function (i, value) {
  476. value = value ? value : '/assets/img/blank.gif';
  477. html.push('<a href="javascript:"><img class="' + classname + '" src="' + Fast.api.cdnurl(value) + '" /></a>');
  478. });
  479. return html.join(' ');
  480. },
  481. // fastadmin老版
  482. // content: function (value, row, index) {
  483. // var width = this.width != undefined ? this.width : 250;
  484. // return "<div style='white-space: nowrap; text-overflow:ellipsis; overflow: hidden; max-width:" + width + "px;' title='" + value + "'>" + value + "</div>";
  485. // },
  486. // zskk老版
  487. // content: function (value, row, index) {
  488. // var width = this.width != undefined ? this.width : 250;
  489. // return "<div style='white-space: nowrap; text-overflow:ellipsis; overflow: hidden; max-width:" + width + "px;' title='" + value + "'>" + value + "</div>";
  490. // },
  491. // fastadmin新版
  492. // content: function (value, row, index) {
  493. // var width = this.width != undefined ? (this.width.toString().match(/^\d+$/) ? this.width + "px" : this.width) : "250px";
  494. // var hover = this.hover != undefined && this.hover ? "autocontent-hover" : "";
  495. // return "<div class='autocontent-item " + hover + "' style='white-space: nowrap; text-overflow:ellipsis; overflow: hidden; max-width:" + width + ";'>" + value + "</div>";
  496. // },
  497. // zskk新版
  498. content: function (value, row, index) {
  499. var width = this.width != undefined ? (this.width.toString().match(/^\d+$/) ? this.width + "px" : this.width) : "250px";
  500. return "<div style='white-space: nowrap; text-overflow:ellipsis; overflow: hidden; max-width:" + width + ";' title='" + value + "'>" + value + "</div>";
  501. },
  502. status: function (value, row, index) {
  503. var custom = {normal: 'success', hidden: 'gray', deleted: 'danger', locked: 'info'};
  504. if (typeof this.custom !== 'undefined') {
  505. custom = $.extend(custom, this.custom);
  506. }
  507. this.custom = custom;
  508. this.icon = 'fa fa-circle';
  509. return Table.api.formatter.normal.call(this, value, row, index);
  510. },
  511. normal: function (value, row, index) {
  512. var colorArr = ["primary", "success", "danger", "warning", "info", "gray", "red", "yellow", "aqua", "blue", "navy", "teal", "olive", "lime", "fuchsia", "purple", "maroon"];
  513. var custom = {};
  514. if (typeof this.custom !== 'undefined') {
  515. custom = $.extend(custom, this.custom);
  516. }
  517. value = value === null ? '' : value.toString();
  518. var keys = typeof this.searchList === 'object' ? Object.keys(this.searchList) : [];
  519. var index = keys.indexOf(value);
  520. var color = value && typeof custom[value] !== 'undefined' ? custom[value] : null;
  521. var display = index > -1 ? this.searchList[value] : null;
  522. var icon = typeof this.icon !== 'undefined' ? this.icon : null;
  523. if (!color) {
  524. color = index > -1 && typeof colorArr[index] !== 'undefined' ? colorArr[index] : 'primary';
  525. }
  526. if (!display) {
  527. display = __(value.charAt(0).toUpperCase() + value.slice(1));
  528. }
  529. var html = '<span class="text-' + color + '">' + (icon ? '<i class="' + icon + '"></i> ' : '') + display + '</span>';
  530. if (this.operate != false) {
  531. html = '<a href="javascript:;" class="searchit" data-toggle="tooltip" title="' + __('Click to search %s', display) + '" data-field="' + this.field + '" data-value="' + value + '">' + html + '</a>';
  532. }
  533. return html;
  534. },
  535. toggle: function (value, row, index) {
  536. var color = typeof this.color !== 'undefined' ? this.color : 'success';
  537. var yes = typeof this.yes !== 'undefined' ? this.yes : 1;
  538. var no = typeof this.no !== 'undefined' ? this.no : 0;
  539. var url = typeof this.url !== 'undefined' ? this.url : '';
  540. var disable = false;
  541. if (typeof this.disable !== "undefined") {
  542. disable = typeof this.disable === "function" ? this.disable.call(this, value, row, index) : this.disable;
  543. }
  544. return "<a href='javascript:;' data-toggle='tooltip' title='" + __('Click to toggle') + "' class='btn-change " + (disable ? 'btn disabled' : '') + "' data-id='"
  545. + row.id + "' " + (url ? "data-url='" + url + "'" : "") + " data-params='" + this.field + "=" + (value == yes ? no : yes) + "'><i class='fa fa-toggle-on " + (value == yes ? 'text-' + color : 'fa-flip-horizontal text-gray') + " fa-2x'></i></a>";
  546. },
  547. url: function (value, row, index) {
  548. return '<div class="input-group input-group-sm" style="width:250px;margin:0 auto;"><input type="text" class="form-control input-sm" value="' + value + '"><span class="input-group-btn input-group-sm"><a href="' + value + '" target="_blank" class="btn btn-default btn-sm"><i class="fa fa-link"></i></a></span></div>';
  549. },
  550. search: function (value, row, index) {
  551. var field = this.field;
  552. if (typeof this.customField !== 'undefined' && typeof row[this.customField] !== 'undefined') {
  553. value = row[this.customField];
  554. field = this.customField;
  555. }
  556. return '<a href="javascript:;" class="searchit" data-toggle="tooltip" title="' + __('Click to search %s', value) + '" data-field="' + field + '" data-value="' + value + '">' + value + '</a>';
  557. },
  558. addtabs: function (value, row, index) {
  559. var url = Table.api.replaceurl(this.url, row, this.table);
  560. var title = this.atitle ? this.atitle : __("Search %s", value);
  561. return '<a href="' + Fast.api.fixurl(url) + '" class="addtabsit" data-value="' + value + '" title="' + title + '">' + value + '</a>';
  562. },
  563. dialog: function (value, row, index) {
  564. var url = Table.api.replaceurl(this.url, row, this.table);
  565. var title = this.atitle ? this.atitle : __("View %s", value);
  566. return '<a href="' + Fast.api.fixurl(url) + '" class="dialogit" data-value="' + value + '" title="' + title + '">' + value + '</a>';
  567. },
  568. flag: function (value, row, index) {
  569. var that = this;
  570. value = value === null ? '' : value.toString();
  571. var colorArr = {index: 'success', hot: 'warning', recommend: 'danger', 'new': 'info'};
  572. //如果字段列有定义custom
  573. if (typeof this.custom !== 'undefined') {
  574. colorArr = $.extend(colorArr, this.custom);
  575. }
  576. var field = this.field;
  577. if (typeof this.customField !== 'undefined' && typeof row[this.customField] !== 'undefined') {
  578. value = row[this.customField];
  579. field = this.customField;
  580. }
  581. //渲染Flag
  582. var html = [];
  583. var arr = value.split(',');
  584. var color, display, label;
  585. $.each(arr, function (i, value) {
  586. value = value === null ? '' : value.toString();
  587. if (value == '')
  588. return true;
  589. color = value && typeof colorArr[value] !== 'undefined' ? colorArr[value] : 'primary';
  590. display = typeof that.searchList !== 'undefined' && typeof that.searchList[value] !== 'undefined' ? that.searchList[value] : __(value.charAt(0).toUpperCase() + value.slice(1));
  591. label = '<span class="label label-' + color + '">' + display + '</span>';
  592. if (that.operate) {
  593. html.push('<a href="javascript:;" class="searchit" data-toggle="tooltip" title="' + __('Click to search %s', display) + '" data-field="' + field + '" data-value="' + value + '">' + label + '</a>');
  594. } else {
  595. html.push(label);
  596. }
  597. });
  598. return html.join(' ');
  599. },
  600. label: function (value, row, index) {
  601. return Table.api.formatter.flag.call(this, value, row, index);
  602. },
  603. datetime: function (value, row, index) {
  604. var datetimeFormat = typeof this.datetimeFormat === 'undefined' ? 'YYYY-MM-DD HH:mm:ss' : this.datetimeFormat;
  605. if (isNaN(value)) {
  606. return value ? Moment(value).format(datetimeFormat) : __('None');
  607. } else {
  608. return value ? Moment(parseInt(value) * 1000).format(datetimeFormat) : __('None');
  609. }
  610. },
  611. operate: function (value, row, index) {
  612. var table = this.table;
  613. // 操作配置
  614. var options = table ? table.bootstrapTable('getOptions') : {};
  615. // 默认按钮组
  616. var buttons = $.extend([], this.buttons || []);
  617. // 所有按钮名称
  618. var names = [];
  619. buttons.forEach(function (item) {
  620. names.push(item.name);
  621. });
  622. if (options.extend.dragsort_url !== '' && names.indexOf('dragsort') === -1) {
  623. buttons.push(Table.button.dragsort);
  624. }
  625. if (options.extend.edit_url !== '' && names.indexOf('edit') === -1) {
  626. Table.button.edit.url = options.extend.edit_url;
  627. buttons.push(Table.button.edit);
  628. }
  629. if (options.extend.del_url !== '' && names.indexOf('del') === -1) {
  630. buttons.push(Table.button.del);
  631. }
  632. return Table.api.buttonlink(this, buttons, value, row, index, 'operate');
  633. }
  634. ,
  635. buttons: function (value, row, index) {
  636. // 默认按钮组
  637. var buttons = $.extend([], this.buttons || []);
  638. return Table.api.buttonlink(this, buttons, value, row, index, 'buttons');
  639. },
  640. bold: function (value, row, index) {
  641. return "<span style='font-weight: bold'>"+ value +"</span>";
  642. },
  643. italic: function (value, row, index) {
  644. value = value == null ? '-' : value;
  645. return "<span style='font-style: italic'>"+ value +"</span>";
  646. },
  647. TJNum: function (value) {
  648. if(value === 0){
  649. return value;
  650. }
  651. return '<strong style="font-style:italic; font-size: 15px; color: #408eba;">'+ value +'</strong>';
  652. },
  653. },
  654. buttonlink: function (column, buttons, value, row, index, type) {
  655. var table = column.table;
  656. type = typeof type === 'undefined' ? 'buttons' : type;
  657. var options = table ? table.bootstrapTable('getOptions') : {};
  658. var html = [];
  659. var hidden, visible, disable, url, classname, icon, text, title, refresh, confirm, extend,
  660. dropdown, link;
  661. var fieldIndex = column.fieldIndex;
  662. var dropdowns = {};
  663. $.each(buttons, function (i, j) {
  664. if (type === 'operate') {
  665. if (j.name === 'dragsort' && typeof row[Table.config.dragsortfield] === 'undefined') {
  666. return true;
  667. }
  668. if (['add', 'edit', 'del', 'multi', 'dragsort'].indexOf(j.name) > -1 && !options.extend[j.name + "_url"]) {
  669. return true;
  670. }
  671. }
  672. var attr = table.data(type + "-" + j.name);
  673. if (typeof attr === 'undefined' || attr) {
  674. hidden = typeof j.hidden === 'function' ? j.hidden.call(table, row, j) : (typeof j.hidden !== 'undefined' ? j.hidden : false);
  675. if (hidden) {
  676. return true;
  677. }
  678. visible = typeof j.visible === 'function' ? j.visible.call(table, row, j) : (typeof j.visible !== 'undefined' ? j.visible : true);
  679. if (!visible) {
  680. return true;
  681. }
  682. dropdown = j.dropdown ? j.dropdown : '';
  683. url = j.url ? j.url : '';
  684. url = typeof url === 'function' ? url.call(table, row, j) : (url ? Fast.api.fixurl(Table.api.replaceurl(url, row, table)) : 'javascript:;');
  685. classname = j.classname ? j.classname : 'btn-primary btn-' + name + 'one';
  686. icon = j.icon ? j.icon : '';
  687. text = typeof j.text === 'function' ? j.text.call(table, row, j) : j.text ? j.text : '';
  688. title = typeof j.title === 'function' ? j.title.call(table, row, j) : j.title ? j.title : text;
  689. refresh = j.refresh ? 'data-refresh="' + j.refresh + '"' : '';
  690. confirm = typeof j.confirm === 'function' ? j.confirm.call(table, row, j) : (typeof j.confirm !== 'undefined' ? j.confirm : false);
  691. confirm = confirm ? 'data-confirm="' + confirm + '"' : '';
  692. extend = j.extend ? j.extend : '';
  693. disable = typeof j.disable === 'function' ? j.disable.call(table, row, j) : (typeof j.disable !== 'undefined' ? j.disable : false);
  694. if (disable) {
  695. classname = classname + ' disabled';
  696. }
  697. link = '<a href="' + url + '" class="' + classname + '" ' + (confirm ? confirm + ' ' : '') + (refresh ? refresh + ' ' : '') + extend + ' title="' + title + '" data-table-id="' + (table ? table.attr("id") : '') + '" data-field-index="' + fieldIndex + '" data-row-index="' + index + '" data-button-index="' + i + '"><i class="' + icon + '"></i>' + (text ? ' ' + text : '') + '</a>';
  698. if (dropdown) {
  699. if (typeof dropdowns[dropdown] == 'undefined') {
  700. dropdowns[dropdown] = [];
  701. }
  702. dropdowns[dropdown].push(link);
  703. } else {
  704. html.push(link);
  705. }
  706. }
  707. });
  708. if (!$.isEmptyObject(dropdowns)) {
  709. var dropdownHtml = [];
  710. $.each(dropdowns, function (i, j) {
  711. dropdownHtml.push('<div class="btn-group"><button type="button" class="btn btn-primary dropdown-toggle btn-xs" data-toggle="dropdown">' + i + '</button><button type="button" class="btn btn-primary dropdown-toggle btn-xs" data-toggle="dropdown"><span class="caret"></span></button><ul class="dropdown-menu pull-right"><li>' + j.join('</li><li>') + '</li></ul></div>');
  712. });
  713. html.unshift(dropdownHtml);
  714. }
  715. return html.join(' ');
  716. },
  717. //替换URL中的数据
  718. replaceurl: function (url, row, table) {
  719. var options = table ? table.bootstrapTable('getOptions') : null;
  720. var ids = options ? row[options.pk] : 0;
  721. row.ids = ids ? ids : (typeof row.ids !== 'undefined' ? row.ids : 0);
  722. //自动添加ids参数
  723. url = !url.match(/\{ids\}/i) ? url + (url.match(/(\?|&)+/) ? "&ids=" : "/ids/") + '{ids}' : url;
  724. url = url.replace(/\{(.*?)\}/gi, function (matched) {
  725. matched = matched.substring(1, matched.length - 1);
  726. if (matched.indexOf(".") !== -1) {
  727. var temp = row;
  728. var arr = matched.split(/\./);
  729. for (var i = 0; i < arr.length; i++) {
  730. if (typeof temp[arr[i]] !== 'undefined') {
  731. temp = temp[arr[i]];
  732. }
  733. }
  734. return typeof temp === 'object' ? '' : temp;
  735. }
  736. return row[matched];
  737. });
  738. return url;
  739. },
  740. // 获取选中的条目ID集合
  741. selectedids: function (table) {
  742. var options = table.bootstrapTable('getOptions');
  743. if (options.templateView) {
  744. return $.map($("input[data-id][name='checkbox']:checked"), function (dom) {
  745. return $(dom).data("id");
  746. });
  747. } else {
  748. return $.map(table.bootstrapTable('getSelections'), function (row) {
  749. return row[options.pk];
  750. });
  751. }
  752. },
  753. // 切换复选框状态
  754. toggleattr: function (table) {
  755. $("input[type='checkbox']", table).trigger('click');
  756. },
  757. // 根据行索引获取行数据
  758. getrowdata: function (table, index) {
  759. index = parseInt(index);
  760. var data = table.bootstrapTable('getData');
  761. return typeof data[index] !== 'undefined' ? data[index] : null;
  762. },
  763. // 根据行索引获取行数据
  764. getrowbyindex: function (table, index) {
  765. return Table.api.getrowdata(table, index);
  766. },
  767. // 根据主键ID获取行数据
  768. getrowbyid: function (table, id) {
  769. var row = {};
  770. var options = table.bootstrapTable("getOptions");
  771. $.each(table.bootstrapTable('getData'), function (i, j) {
  772. if (j[options.pk] == id) {
  773. row = j;
  774. return false;
  775. }
  776. });
  777. return row;
  778. }
  779. },
  780. };
  781. return Table;
  782. });