yii.gridView.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /**
  2. * Yii GridView widget.
  3. *
  4. * This is the JavaScript widget used by the yii\grid\GridView widget.
  5. *
  6. * @link http://www.yiiframework.com/
  7. * @copyright Copyright (c) 2008 Yii Software LLC
  8. * @license http://www.yiiframework.com/license/
  9. * @author Qiang Xue <[email protected]>
  10. * @since 2.0
  11. */
  12. (function ($) {
  13. $.fn.yiiGridView = function (method) {
  14. if (methods[method]) {
  15. return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
  16. } else if (typeof method === 'object' || !method) {
  17. return methods.init.apply(this, arguments);
  18. } else {
  19. $.error('Method ' + method + ' does not exist on jQuery.yiiGridView');
  20. return false;
  21. }
  22. };
  23. var defaults = {
  24. filterUrl: undefined,
  25. filterSelector: undefined
  26. };
  27. var methods = {
  28. init: function (options) {
  29. return this.each(function () {
  30. var $e = $(this);
  31. var settings = $.extend({}, defaults, options || {});
  32. $e.data('yiiGridView', {
  33. settings: settings
  34. });
  35. var enterPressed = false;
  36. $(document).on('change.yiiGridView keydown.yiiGridView', settings.filterSelector, function (event) {
  37. if (event.type === 'keydown') {
  38. if (event.keyCode !== 13) {
  39. return; // only react to enter key
  40. } else {
  41. enterPressed = true;
  42. }
  43. } else {
  44. // prevent processing for both keydown and change events
  45. if (enterPressed) {
  46. enterPressed = false;
  47. return;
  48. }
  49. }
  50. var data = $(settings.filterSelector).serialize();
  51. var url = settings.filterUrl;
  52. if (url.indexOf('?') >= 0) {
  53. url += '&' + data;
  54. } else {
  55. url += '?' + data;
  56. }
  57. window.location.href = url;
  58. return false;
  59. });
  60. });
  61. },
  62. setSelectionColumn: function (options) {
  63. var $grid = $(this);
  64. var data = $grid.data('yiiGridView');
  65. data.selectionColumn = options.name;
  66. if (!options.multiple) {
  67. return;
  68. }
  69. $grid.on('click.yiiGridView', "input[name='" + options.checkAll + "']", function () {
  70. $grid.find("input[name='" + options.name + "']:enabled").prop('checked', this.checked);
  71. });
  72. $grid.on('click.yiiGridView', "input[name='" + options.name + "']:enabled", function () {
  73. var all = $grid.find("input[name='" + options.name + "']").length == $grid.find("input[name='" + options.name + "']:checked").length;
  74. $grid.find("input[name='" + options.checkAll + "']").prop('checked', all);
  75. });
  76. },
  77. getSelectedRows: function () {
  78. var $grid = $(this);
  79. var data = $grid.data('yiiGridView');
  80. var keys = [];
  81. if (data.selectionColumn) {
  82. $grid.find("input[name='" + data.selectionColumn + "']:checked").each(function () {
  83. keys.push($(this).parent().closest('tr').data('key'));
  84. });
  85. }
  86. return keys;
  87. },
  88. destroy: function () {
  89. return this.each(function () {
  90. $(window).unbind('.yiiGridView');
  91. $(this).removeData('yiiGridView');
  92. });
  93. },
  94. data: function() {
  95. return this.data('yiiGridView');
  96. }
  97. };
  98. var enterPressed = false;
  99. var filterChanged = function (event) {
  100. if (event.type === 'keydown') {
  101. if (event.keyCode !== 13) {
  102. return; // only react to enter key
  103. } else {
  104. enterPressed = true;
  105. }
  106. } else {
  107. // prevent processing for both keydown and change events
  108. if (enterPressed) {
  109. enterPressed = false;
  110. return;
  111. }
  112. }
  113. var data = $(settings.filterSelector).serialize();
  114. if (settings.pageVar !== undefined) {
  115. data += '&' + settings.pageVar + '=1';
  116. }
  117. if (settings.enableHistory && settings.ajaxUpdate !== false && window.History.enabled) {
  118. // Ajaxify this link
  119. var url = $('#' + id).yiiGridView('getUrl'),
  120. params = $.deparam.querystring($.param.querystring(url, data));
  121. delete params[settings.ajaxVar];
  122. window.History.pushState(null, document.title, decodeURIComponent($.param.querystring(url.substr(0, url.indexOf('?')), params)));
  123. } else {
  124. $('#' + id).yiiGridView('update', {data: data});
  125. }
  126. return false;
  127. };
  128. })(window.jQuery);