require-table.js 45 KB

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