bootstrap-table-editable.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. ' data-vlaue="' + result + '"',
  36. '>' + '</a>'
  37. ].join('');
  38. };
  39. });
  40. };
  41. BootstrapTable.prototype.initBody = function () {
  42. var that = this;
  43. _initBody.apply(this, Array.prototype.slice.apply(arguments));
  44. if (!this.options.editable) {
  45. return;
  46. }
  47. $.each(this.options.columns, function (i, column) {
  48. if (!column.editable) {
  49. return;
  50. }
  51. that.$body.find('a[data-name="' + column.field + '"]').editable(column.editable)
  52. .off('save').on('save', function (e, params) {
  53. var data = that.getData(),
  54. index = $(this).parents('tr[data-index]').data('index'),
  55. row = data[index],
  56. oldValue = row[column.field];
  57. row[column.field] = params.submitValue;
  58. that.trigger('editable-save', column.field, row, oldValue, $(this));
  59. });
  60. });
  61. this.trigger('editable-init');
  62. };
  63. }(jQuery);