You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

161 lines
5.9KB

  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 <qiang.xue@gmail.com>
  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 gridData = {};
  28. var gridEvents = {
  29. /**
  30. * beforeFilter event is triggered before filtering the grid.
  31. * The signature of the event handler should be:
  32. * function (event)
  33. * where
  34. * - event: an Event object.
  35. *
  36. * If the handler returns a boolean false, it will stop filter form submission after this event. As
  37. * a result, afterFilter event will not be triggered.
  38. */
  39. beforeFilter: 'beforeFilter',
  40. /**
  41. * afterFilter event is triggered after filtering the grid and filtered results are fetched.
  42. * The signature of the event handler should be:
  43. * function (event)
  44. * where
  45. * - event: an Event object.
  46. */
  47. afterFilter: 'afterFilter'
  48. };
  49. var methods = {
  50. init: function (options) {
  51. return this.each(function () {
  52. var $e = $(this);
  53. var settings = $.extend({}, defaults, options || {});
  54. gridData[$e.prop('id')] = {settings: settings};
  55. var enterPressed = false;
  56. $(document).off('change.yiiGridView keydown.yiiGridView', settings.filterSelector)
  57. .on('change.yiiGridView keydown.yiiGridView', settings.filterSelector, function (event) {
  58. if (event.type === 'keydown') {
  59. if (event.keyCode !== 13) {
  60. return; // only react to enter key
  61. } else {
  62. enterPressed = true;
  63. }
  64. } else {
  65. // prevent processing for both keydown and change events
  66. if (enterPressed) {
  67. enterPressed = false;
  68. return;
  69. }
  70. }
  71. methods.applyFilter.apply($e);
  72. return false;
  73. });
  74. });
  75. },
  76. applyFilter: function () {
  77. var $grid = $(this), event;
  78. var settings = gridData[$grid.prop('id')].settings;
  79. var data = {};
  80. $.each($(settings.filterSelector).serializeArray(), function () {
  81. data[this.name] = this.value;
  82. });
  83. $.each(yii.getQueryParams(settings.filterUrl), function (name, value) {
  84. if (data[name] === undefined) {
  85. data[name] = value;
  86. }
  87. });
  88. var pos = settings.filterUrl.indexOf('?');
  89. var url = pos < 0 ? settings.filterUrl : settings.filterUrl.substring(0, pos);
  90. $grid.find('form.gridview-filter-form').remove();
  91. var $form = $('<form action="' + url + '" method="get" class="gridview-filter-form" style="display:none" data-pjax></form>').appendTo($grid);
  92. $.each(data, function (name, value) {
  93. $form.append($('<input type="hidden" name="t" value="" />').attr('name', name).val(value));
  94. });
  95. event = $.Event(gridEvents.beforeFilter);
  96. $grid.trigger(event);
  97. if (event.result === false) {
  98. return;
  99. }
  100. $form.submit();
  101. $grid.trigger(gridEvents.afterFilter);
  102. },
  103. setSelectionColumn: function (options) {
  104. var $grid = $(this);
  105. var id = $(this).prop('id');
  106. gridData[id].selectionColumn = options.name;
  107. if (!options.multiple) {
  108. return;
  109. }
  110. var checkAll = "#" + id + " input[name='" + options.checkAll + "']";
  111. var inputs = "#" + id + " input[name='" + options.name + "']";
  112. $(document).off('click.yiiGridView', checkAll).on('click.yiiGridView', checkAll, function () {
  113. $grid.find("input[name='" + options.name + "']:enabled").prop('checked', this.checked);
  114. });
  115. $(document).off('click.yiiGridView', inputs + ":enabled").on('click.yiiGridView', inputs + ":enabled", function () {
  116. var all = $grid.find("input[name='" + options.name + "']").length == $grid.find("input[name='" + options.name + "']:checked").length;
  117. $grid.find("input[name='" + options.checkAll + "']").prop('checked', all);
  118. });
  119. },
  120. getSelectedRows: function () {
  121. var $grid = $(this);
  122. var data = gridData[$grid.prop('id')];
  123. var keys = [];
  124. if (data.selectionColumn) {
  125. $grid.find("input[name='" + data.selectionColumn + "']:checked").each(function () {
  126. keys.push($(this).parent().closest('tr').data('key'));
  127. });
  128. }
  129. return keys;
  130. },
  131. destroy: function () {
  132. return this.each(function () {
  133. $(window).unbind('.yiiGridView');
  134. $(this).removeData('yiiGridView');
  135. });
  136. },
  137. data: function () {
  138. var id = $(this).prop('id');
  139. return gridData[id];
  140. }
  141. };
  142. })(window.jQuery);