bootstrap-table-editable.js 1.9 KB

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