bootstrap-table-key-events.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /**
  2. * @author: Dennis Hernández
  3. * @webSite: http://djhvscf.github.io/Blog
  4. * @version: v1.0.0
  5. *
  6. * @update zhixin wen <wenzhixin2010@gmail.com>
  7. */
  8. !function ($) {
  9. 'use strict';
  10. $.extend($.fn.bootstrapTable.defaults, {
  11. keyEvents: false
  12. });
  13. var BootstrapTable = $.fn.bootstrapTable.Constructor,
  14. _init = BootstrapTable.prototype.init;
  15. BootstrapTable.prototype.init = function () {
  16. _init.apply(this, Array.prototype.slice.apply(arguments));
  17. this.initKeyEvents();
  18. };
  19. BootstrapTable.prototype.initKeyEvents = function () {
  20. if (this.options.keyEvents) {
  21. var that = this;
  22. $(document).off('keydown').on('keydown', function (e) {
  23. var $search = that.$toolbar.find('.search input'),
  24. $refresh = that.$toolbar.find('button[name="refresh"]'),
  25. $toggle = that.$toolbar.find('button[name="toggle"]'),
  26. $paginationSwitch = that.$toolbar.find('button[name="paginationSwitch"]');
  27. if (document.activeElement === $search.get(0) || !$.contains(document.activeElement ,that.$toolbar.get(0))) {
  28. return true;
  29. }
  30. switch (e.keyCode) {
  31. case 83: //s
  32. if (!that.options.search) {
  33. return;
  34. }
  35. $search.focus();
  36. return false;
  37. case 82: //r
  38. if (!that.options.showRefresh) {
  39. return;
  40. }
  41. $refresh.click();
  42. return false;
  43. case 84: //t
  44. if (!that.options.showToggle) {
  45. return;
  46. }
  47. $toggle.click();
  48. return false;
  49. case 80: //p
  50. if (!that.options.showPaginationSwitch) {
  51. return;
  52. }
  53. $paginationSwitch.click();
  54. return false;
  55. case 37: // left
  56. if (!that.options.pagination) {
  57. return;
  58. }
  59. that.prevPage();
  60. return false;
  61. case 39: // right
  62. if (!that.options.pagination) {
  63. return;
  64. }
  65. that.nextPage();
  66. return;
  67. }
  68. });
  69. }
  70. };
  71. }(jQuery);