您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

slick.headerbuttons.js 5.0 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. (function ($) {
  2. // register namespace
  3. $.extend(true, window, {
  4. "Slick": {
  5. "Plugins": {
  6. "HeaderButtons": HeaderButtons
  7. }
  8. }
  9. });
  10. /***
  11. * A plugin to add custom buttons to column headers.
  12. *
  13. * USAGE:
  14. *
  15. * Add the plugin .js & .css files and register it with the grid.
  16. *
  17. * To specify a custom button in a column header, extend the column definition like so:
  18. *
  19. * var columns = [
  20. * {
  21. * id: 'myColumn',
  22. * name: 'My column',
  23. *
  24. * // This is the relevant part
  25. * header: {
  26. * buttons: [
  27. * {
  28. * // button options
  29. * },
  30. * {
  31. * // button options
  32. * }
  33. * ]
  34. * }
  35. * }
  36. * ];
  37. *
  38. * Available button options:
  39. * cssClass: CSS class to add to the button.
  40. * image: Relative button image path.
  41. * tooltip: Button tooltip.
  42. * showOnHover: Only show the button on hover.
  43. * handler: Button click handler.
  44. * command: A command identifier to be passed to the onCommand event handlers.
  45. *
  46. * The plugin exposes the following events:
  47. * onCommand: Fired on button click for buttons with 'command' specified.
  48. * Event args:
  49. * grid: Reference to the grid.
  50. * column: Column definition.
  51. * command: Button command identified.
  52. * button: Button options. Note that you can change the button options in your
  53. * event handler, and the column header will be automatically updated to
  54. * reflect them. This is useful if you want to implement something like a
  55. * toggle button.
  56. *
  57. *
  58. * @param options {Object} Options:
  59. * buttonCssClass: a CSS class to use for buttons (default 'slick-header-button')
  60. * @class Slick.Plugins.HeaderButtons
  61. * @constructor
  62. */
  63. function HeaderButtons(options) {
  64. var _grid;
  65. var _self = this;
  66. var _handler = new Slick.EventHandler();
  67. var _defaults = {
  68. buttonCssClass: "slick-header-button"
  69. };
  70. function init(grid) {
  71. options = $.extend(true, {}, _defaults, options);
  72. _grid = grid;
  73. _handler
  74. .subscribe(_grid.onHeaderCellRendered, handleHeaderCellRendered)
  75. .subscribe(_grid.onBeforeHeaderCellDestroy, handleBeforeHeaderCellDestroy);
  76. // Force the grid to re-render the header now that the events are hooked up.
  77. _grid.setColumns(_grid.getColumns());
  78. }
  79. function destroy() {
  80. _handler.unsubscribeAll();
  81. }
  82. function handleHeaderCellRendered(e, args) {
  83. var column = args.column;
  84. if (column.header && column.header.buttons) {
  85. // Append buttons in reverse order since they are floated to the right.
  86. var i = column.header.buttons.length;
  87. while (i--) {
  88. var button = column.header.buttons[i];
  89. var btn = $("<div></div>")
  90. .addClass(options.buttonCssClass)
  91. .data("column", column)
  92. .data("button", button);
  93. if (button.showOnHover) {
  94. btn.addClass("slick-header-button-hidden");
  95. }
  96. if (button.image) {
  97. btn.css("backgroundImage", "url(" + button.image + ")");
  98. }
  99. if (button.cssClass) {
  100. btn.addClass(button.cssClass);
  101. }
  102. if (button.tooltip) {
  103. btn.attr("title", button.tooltip);
  104. }
  105. if (button.command) {
  106. btn.data("command", button.command);
  107. }
  108. if (button.handler) {
  109. btn.bind("click", button.handler);
  110. }
  111. btn
  112. .bind("click", handleButtonClick)
  113. .appendTo(args.node);
  114. }
  115. }
  116. }
  117. function handleBeforeHeaderCellDestroy(e, args) {
  118. var column = args.column;
  119. if (column.header && column.header.buttons) {
  120. // Removing buttons via jQuery will also clean up any event handlers and data.
  121. // NOTE: If you attach event handlers directly or using a different framework,
  122. // you must also clean them up here to avoid memory leaks.
  123. $(args.node).find("." + options.buttonCssClass).remove();
  124. }
  125. }
  126. function handleButtonClick(e) {
  127. var command = $(this).data("command");
  128. var columnDef = $(this).data("column");
  129. var button = $(this).data("button");
  130. if (command != null) {
  131. _self.onCommand.notify({
  132. "grid": _grid,
  133. "column": columnDef,
  134. "command": command,
  135. "button": button
  136. }, e, _self);
  137. // Update the header in case the user updated the button definition in the handler.
  138. _grid.updateColumnHeader(columnDef.id);
  139. }
  140. // Stop propagation so that it doesn't register as a header click event.
  141. e.preventDefault();
  142. e.stopPropagation();
  143. }
  144. $.extend(this, {
  145. "init": init,
  146. "destroy": destroy,
  147. "onCommand": new Slick.Event()
  148. });
  149. }
  150. })(jQuery);