|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
-
- (function ($) {
- $.fn.yiiGridView = function (method) {
- if (methods[method]) {
- return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
- } else if (typeof method === 'object' || !method) {
- return methods.init.apply(this, arguments);
- } else {
- $.error('Method ' + method + ' does not exist on jQuery.yiiGridView');
- return false;
- }
- };
-
- var defaults = {
- filterUrl: undefined,
- filterSelector: undefined
- };
-
- var gridData = {};
-
- var gridEvents = {
-
-
- beforeFilter: 'beforeFilter',
-
-
- afterFilter: 'afterFilter'
- };
-
- var methods = {
- init: function (options) {
- return this.each(function () {
- var $e = $(this);
- var settings = $.extend({}, defaults, options || {});
- var id = $e.attr('id');
- if (gridData[id] === undefined) {
- gridData[id] = {};
- }
-
- gridData[id] = $.extend(gridData[id], {settings: settings});
-
- var enterPressed = false;
- $(document).off('change.yiiGridView keydown.yiiGridView', settings.filterSelector)
- .on('change.yiiGridView keydown.yiiGridView', settings.filterSelector, function (event) {
- if (event.type === 'keydown') {
- if (event.keyCode !== 13) {
- return;
- } else {
- enterPressed = true;
- }
- } else {
-
- if (enterPressed) {
- enterPressed = false;
- return;
- }
- }
-
- methods.applyFilter.apply($e);
-
- return false;
- });
- });
- },
-
- applyFilter: function () {
- var $grid = $(this), event;
- var settings = gridData[$grid.attr('id')].settings;
- var data = {};
- $.each($(settings.filterSelector).serializeArray(), function () {
- if (!(this.name in data)) {
- data[this.name] = [];
- }
- data[this.name].push(this.value);
- });
-
- var namesInFilter = Object.keys(data);
-
- $.each(yii.getQueryParams(settings.filterUrl), function (name, value) {
- if (namesInFilter.indexOf(name) === -1 && namesInFilter.indexOf(name.replace(/\[\]$/, '')) === -1) {
- if (!$.isArray(value)) {
- value = [value];
- }
- if (!(name in data)) {
- data[name] = value;
- } else {
- $.each(value, function (i, val) {
- if ($.inArray(val, data[name])) {
- data[name].push(val);
- }
- });
- }
- }
- });
-
- var pos = settings.filterUrl.indexOf('?');
- var url = pos < 0 ? settings.filterUrl : settings.filterUrl.substring(0, pos);
-
- $grid.find('form.gridview-filter-form').remove();
- var $form = $('<form/>', {
- action: url,
- method: 'get',
- 'class': 'gridview-filter-form',
- style: 'display:none',
- 'data-pjax': ''
- }).appendTo($grid);
- $.each(data, function (name, values) {
- $.each(values, function (index, value) {
- $form.append($('<input/>').attr({type: 'hidden', name: name, value: value}));
- });
- });
-
- event = $.Event(gridEvents.beforeFilter);
- $grid.trigger(event);
- if (event.result === false) {
- return;
- }
-
- $form.submit();
-
- $grid.trigger(gridEvents.afterFilter);
- },
-
- setSelectionColumn: function (options) {
- var $grid = $(this);
- var id = $(this).attr('id');
- if (gridData[id] === undefined) {
- gridData[id] = {};
- }
- gridData[id].selectionColumn = options.name;
- if (!options.multiple || !options.checkAll) {
- return;
- }
- var checkAll = "#" + id + " input[name='" + options.checkAll + "']";
- var inputs = options.class ? "input." + options.class : "input[name='" + options.name + "']";
- var inputsEnabled = "#" + id + " " + inputs + ":enabled";
- $(document).off('click.yiiGridView', checkAll).on('click.yiiGridView', checkAll, function () {
- $grid.find(inputs + ":enabled").prop('checked', this.checked);
- });
- $(document).off('click.yiiGridView', inputsEnabled).on('click.yiiGridView', inputsEnabled, function () {
- var all = $grid.find(inputs).length == $grid.find(inputs + ":checked").length;
- $grid.find("input[name='" + options.checkAll + "']").prop('checked', all);
- });
- },
-
- getSelectedRows: function () {
- var $grid = $(this);
- var data = gridData[$grid.attr('id')];
- var keys = [];
- if (data.selectionColumn) {
- $grid.find("input[name='" + data.selectionColumn + "']:checked").each(function () {
- keys.push($(this).parent().closest('tr').data('key'));
- });
- }
- return keys;
- },
-
- destroy: function () {
- return this.each(function () {
- $(window).unbind('.yiiGridView');
- $(this).removeData('yiiGridView');
- });
- },
-
- data: function () {
- var id = $(this).attr('id');
- return gridData[id];
- }
- };
- })(window.jQuery);
|