bootstrap-table-editable.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 () {return false;},
  10. onEditableSave: function (field, row, oldValue, $el) {return false;}
  11. });
  12. $.extend($.fn.bootstrapTable.Constructor.EVENTS, {
  13. 'editable-init.bs.table': 'onEditableInit',
  14. 'editable-save.bs.table': 'onEditableSave'
  15. });
  16. var BootstrapTable = $.fn.bootstrapTable.Constructor,
  17. _initTable = BootstrapTable.prototype.initTable,
  18. _initBody = BootstrapTable.prototype.initBody;
  19. BootstrapTable.prototype.initTable = function () {
  20. var that = this;
  21. _initTable.apply(this, Array.prototype.slice.apply(arguments));
  22. if (!this.options.editable) {
  23. return;
  24. }
  25. $.each(this.options.columns, function (i, column) {
  26. if (!column.editable) {
  27. return;
  28. }
  29. var _formatter = column.formatter;
  30. column.formatter = function (value, row, index) {
  31. var result = _formatter ? _formatter(value, row, index) : value;
  32. return ['<a href="javascript:void(0)"',
  33. ' data-name="' + column.field + '"',
  34. ' data-pk="' + row[that.options.idField] + '"',
  35. '>' + result + '</a>'
  36. ].join('');
  37. };
  38. });
  39. };
  40. BootstrapTable.prototype.initBody = function () {
  41. var that = this;
  42. _initBody.apply(this, Array.prototype.slice.apply(arguments));
  43. if (!this.options.editable) {
  44. return;
  45. }
  46. $.each(this.options.columns, function (i, column) {
  47. if (!column.editable) {
  48. return;
  49. }
  50. that.$body.find('a[data-name="' + column.field + '"]').editable(column.editable)
  51. .off('save').on('save', function (e, params) {
  52. var data = that.getData(),
  53. index = $(this).parents('tr[data-index]').data('index'),
  54. row = data[index],
  55. oldValue = row[column.field];
  56. row[column.field] = params.submitValue;
  57. that.trigger('editable-save', column.field, row, oldValue, $(this));
  58. });
  59. });
  60. this.trigger('editable-init');
  61. };
  62. }(jQuery);