bootstrap-table-editable.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. (function (global, factory) {
  2. if (typeof define === "function" && define.amd) {
  3. define([], factory);
  4. } else if (typeof exports !== "undefined") {
  5. factory();
  6. } else {
  7. var mod = {
  8. exports: {}
  9. };
  10. factory();
  11. global.bootstrapTableEditable = mod.exports;
  12. }
  13. })(this, function () {
  14. 'use strict';
  15. function _classCallCheck(instance, Constructor) {
  16. if (!(instance instanceof Constructor)) {
  17. throw new TypeError("Cannot call a class as a function");
  18. }
  19. }
  20. var _createClass = function () {
  21. function defineProperties(target, props) {
  22. for (var i = 0; i < props.length; i++) {
  23. var descriptor = props[i];
  24. descriptor.enumerable = descriptor.enumerable || false;
  25. descriptor.configurable = true;
  26. if ("value" in descriptor) descriptor.writable = true;
  27. Object.defineProperty(target, descriptor.key, descriptor);
  28. }
  29. }
  30. return function (Constructor, protoProps, staticProps) {
  31. if (protoProps) defineProperties(Constructor.prototype, protoProps);
  32. if (staticProps) defineProperties(Constructor, staticProps);
  33. return Constructor;
  34. };
  35. }();
  36. function _possibleConstructorReturn(self, call) {
  37. if (!self) {
  38. throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
  39. }
  40. return call && (typeof call === "object" || typeof call === "function") ? call : self;
  41. }
  42. var _get = function get(object, property, receiver) {
  43. if (object === null) object = Function.prototype;
  44. var desc = Object.getOwnPropertyDescriptor(object, property);
  45. if (desc === undefined) {
  46. var parent = Object.getPrototypeOf(object);
  47. if (parent === null) {
  48. return undefined;
  49. } else {
  50. return get(parent, property, receiver);
  51. }
  52. } else if ("value" in desc) {
  53. return desc.value;
  54. } else {
  55. var getter = desc.get;
  56. if (getter === undefined) {
  57. return undefined;
  58. }
  59. return getter.call(receiver);
  60. }
  61. };
  62. function _inherits(subClass, superClass) {
  63. if (typeof superClass !== "function" && superClass !== null) {
  64. throw new TypeError("Super expression must either be null or a function, not " + typeof superClass);
  65. }
  66. subClass.prototype = Object.create(superClass && superClass.prototype, {
  67. constructor: {
  68. value: subClass,
  69. enumerable: false,
  70. writable: true,
  71. configurable: true
  72. }
  73. });
  74. if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass;
  75. }
  76. /**
  77. * @author zhixin wen <wenzhixin2010@gmail.com>
  78. * extensions: https://github.com/vitalets/x-editable
  79. */
  80. (function ($) {
  81. var Utils = $.fn.bootstrapTable.utils;
  82. $.extend($.fn.bootstrapTable.defaults, {
  83. editable: true,
  84. onEditableInit: function onEditableInit() {
  85. return false;
  86. },
  87. onEditableSave: function onEditableSave(field, row, oldValue, $el) {
  88. return false;
  89. },
  90. onEditableShown: function onEditableShown(field, row, $el, editable) {
  91. return false;
  92. },
  93. onEditableHidden: function onEditableHidden(field, row, $el, reason) {
  94. return false;
  95. }
  96. });
  97. $.extend($.fn.bootstrapTable.Constructor.EVENTS, {
  98. 'editable-init.bs.table': 'onEditableInit',
  99. 'editable-save.bs.table': 'onEditableSave',
  100. 'editable-shown.bs.table': 'onEditableShown',
  101. 'editable-hidden.bs.table': 'onEditableHidden'
  102. });
  103. $.BootstrapTable = function (_$$BootstrapTable) {
  104. _inherits(_class, _$$BootstrapTable);
  105. function _class() {
  106. _classCallCheck(this, _class);
  107. return _possibleConstructorReturn(this, (_class.__proto__ || Object.getPrototypeOf(_class)).apply(this, arguments));
  108. }
  109. _createClass(_class, [{
  110. key: 'initTable',
  111. value: function initTable() {
  112. var _this2 = this;
  113. _get(_class.prototype.__proto__ || Object.getPrototypeOf(_class.prototype), 'initTable', this).call(this);
  114. if (!this.options.editable) {
  115. return;
  116. }
  117. $.each(this.columns, function (i, column) {
  118. if (!column.editable) {
  119. return;
  120. }
  121. var editableOptions = {};
  122. var editableDataMarkup = [];
  123. var editableDataPrefix = 'editable-';
  124. var processDataOptions = function processDataOptions(key, value) {
  125. // Replace camel case with dashes.
  126. var dashKey = key.replace(/([A-Z])/g, function ($1) {
  127. return '-' + $1.toLowerCase();
  128. });
  129. if (dashKey.indexOf(editableDataPrefix) === 0) {
  130. editableOptions[dashKey.replace(editableDataPrefix, 'data-')] = value;
  131. }
  132. };
  133. $.each(_this2.options, processDataOptions);
  134. column.formatter = column.formatter || function (value) {
  135. return value;
  136. };
  137. column._formatter = column._formatter ? column._formatter : column.formatter;
  138. column.formatter = function (value, row, index) {
  139. var result = Utils.calculateObjectValue(column, column._formatter, [value, row, index], value);
  140. $.each(column, processDataOptions);
  141. $.each(editableOptions, function (key, value) {
  142. editableDataMarkup.push(' ' + key + '="' + value + '"');
  143. });
  144. var _dont_edit_formatter = false;
  145. if (column.editable.hasOwnProperty('noeditFormatter')) {
  146. _dont_edit_formatter = column.editable.noeditFormatter(value, row, index);
  147. }
  148. if (_dont_edit_formatter === false) {
  149. return '<a href="javascript:void(0)"\n data-name="' + column.field + '"\n data-pk="' + row[_this2.options.idField] + '"\n data-value="' + result + '"\n ' + editableDataMarkup.join('') + '></a>';
  150. }
  151. return _dont_edit_formatter;
  152. };
  153. });
  154. }
  155. }, {
  156. key: 'initBody',
  157. value: function initBody(fixedScroll) {
  158. var _this3 = this;
  159. _get(_class.prototype.__proto__ || Object.getPrototypeOf(_class.prototype), 'initBody', this).call(this, fixedScroll);
  160. if (!this.options.editable) {
  161. return;
  162. }
  163. $.each(this.columns, function (i, column) {
  164. if (!column.editable) {
  165. return;
  166. }
  167. var data = _this3.getData();
  168. var $field = _this3.$body.find('a[data-name="' + column.field + '"]');
  169. $field.each(function (i, element) {
  170. var $element = $(element);
  171. var $tr = $element.closest('tr');
  172. var index = $tr.data('index');
  173. var row = data[index];
  174. var editableOpts = Utils.calculateObjectValue(column, column.editable, [index, row, $element], {});
  175. $element.editable(editableOpts);
  176. });
  177. $field.off('save').on('save', function (_ref, _ref2) {
  178. var currentTarget = _ref.currentTarget;
  179. var submitValue = _ref2.submitValue;
  180. var $this = $(currentTarget);
  181. var data = _this3.getData();
  182. var index = $this.parents('tr[data-index]').data('index');
  183. var row = data[index];
  184. var oldValue = row[column.field];
  185. $this.data('value', submitValue);
  186. row[column.field] = submitValue;
  187. _this3.trigger('editable-save', column.field, row, oldValue, $this);
  188. _this3.resetFooter();
  189. });
  190. $field.off('shown').on('shown', function (_ref3, editable) {
  191. var currentTarget = _ref3.currentTarget;
  192. var $this = $(currentTarget);
  193. var data = _this3.getData();
  194. var index = $this.parents('tr[data-index]').data('index');
  195. var row = data[index];
  196. _this3.trigger('editable-shown', column.field, row, $this, editable);
  197. });
  198. $field.off('hidden').on('hidden', function (_ref4, reason) {
  199. var currentTarget = _ref4.currentTarget;
  200. var $this = $(currentTarget);
  201. var data = _this3.getData();
  202. var index = $this.parents('tr[data-index]').data('index');
  203. var row = data[index];
  204. _this3.trigger('editable-hidden', column.field, row, $this, reason);
  205. });
  206. });
  207. this.trigger('editable-init');
  208. }
  209. }]);
  210. return _class;
  211. }($.BootstrapTable);
  212. })(jQuery);
  213. });