bootstrap-table-editable.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /**
  2. * @author zhixin wen <wenzhixin2010@gmail.com>
  3. * extensions: https://github.com/vitalets/x-editable
  4. */
  5. !function ($) {
  6. 'use strict';
  7. $.extend($.fn.bootstrapTable.defaults, {
  8. editable: true,
  9. onEditableInit: function () {
  10. return false;
  11. },
  12. onEditableSave: function (field, row, oldValue, $el) {
  13. return false;
  14. }
  15. });
  16. $.extend($.fn.bootstrapTable.Constructor.EVENTS, {
  17. 'editable-init.bs.table': 'onEditableInit',
  18. 'editable-save.bs.table': 'onEditableSave'
  19. });
  20. var BootstrapTable = $.fn.bootstrapTable.Constructor,
  21. _initTable = BootstrapTable.prototype.initTable,
  22. _initBody = BootstrapTable.prototype.initBody;
  23. BootstrapTable.prototype.initTable = function () {
  24. var that = this;
  25. _initTable.apply(this, Array.prototype.slice.apply(arguments));
  26. if (!this.options.editable) {
  27. return;
  28. }
  29. $.each(this.options.columns, function (i, column) {
  30. if (!column.editable) {
  31. return;
  32. }
  33. var _formatter = column.formatter;
  34. column.formatter = function (value, row, index) {
  35. var result = _formatter ? _formatter(value, row, index) : value;
  36. return ['<a href="javascript:void(0)"',
  37. ' data-name="' + column.field + '"',
  38. ' data-pk="' + row[that.options.idField] + '"',
  39. ' data-value="' + result + '"',
  40. '>' + '</a>'
  41. ].join('');
  42. };
  43. });
  44. };
  45. BootstrapTable.prototype.initBody = function () {
  46. var that = this;
  47. _initBody.apply(this, Array.prototype.slice.apply(arguments));
  48. if (!this.options.editable) {
  49. return;
  50. }
  51. $.each(this.options.columns, function (i, column) {
  52. if (!column.editable) {
  53. return;
  54. }
  55. that.$body.find('a[data-name="' + column.field + '"]').editable(column.editable)
  56. .off('save').on('save', function (e, params) {
  57. var data = that.getData(),
  58. index = $(this).parents('tr[data-index]').data('index'),
  59. row = data[index],
  60. oldValue = row[column.field];
  61. row[column.field] = params.submitValue;
  62. that.trigger('editable-save', column.field, row, oldValue, $(this));
  63. });
  64. });
  65. this.trigger('editable-init');
  66. };
  67. }(jQuery);