yii.gridView.js 5.94 KB
Newer Older
Qiang Xue committed
1 2 3 4 5 6 7 8 9 10 11 12
/**
 * Yii GridView widget.
 *
 * This is the JavaScript widget used by the yii\grid\GridView widget.
 *
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
(function ($) {
Qiang Xue committed
13 14 15 16 17 18 19 20 21 22
    $.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;
        }
    };
Qiang Xue committed
23

Qiang Xue committed
24 25 26 27
    var defaults = {
        filterUrl: undefined,
        filterSelector: undefined
    };
Qiang Xue committed
28

Qiang Xue committed
29
    var gridData = {};
Antonio Ramirez committed
30

31 32 33 34
    var gridEvents = {
        /**
         * beforeFilter event is triggered before filtering the grid.
         * The signature of the event handler should be:
35
         *     function (event)
36 37 38
         * where
         *  - event: an Event object.
         *
39
         * If the handler returns a boolean false, it will stop filter form submission after this event. As
40
         * a result, afterFilter event will not be triggered.
41
         */
42
        beforeFilter: 'beforeFilter',
43 44 45
        /**
         * afterFilter event is triggered after filtering the grid and filtered results are fetched.
         * The signature of the event handler should be:
46
         *     function (event)
47 48 49
         * where
         *  - event: an Event object.
         */
50
        afterFilter: 'afterFilter'
51 52
    };
    
Qiang Xue committed
53 54 55 56 57 58
    var methods = {
        init: function (options) {
            return this.each(function () {
                var $e = $(this);
                var settings = $.extend({}, defaults, options || {});
                gridData[$e.prop('id')] = {settings: settings};
59

Qiang Xue committed
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
                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; // only react to enter key
                            } else {
                                enterPressed = true;
                            }
                        } else {
                            // prevent processing for both keydown and change events
                            if (enterPressed) {
                                enterPressed = false;
                                return;
                            }
                        }
76

Qiang Xue committed
77
                        methods.applyFilter.apply($e);
78

Qiang Xue committed
79 80 81 82
                        return false;
                    });
            });
        },
Qiang Xue committed
83

Qiang Xue committed
84
        applyFilter: function () {
85
            var $grid = $(this), event;
Qiang Xue committed
86 87 88 89 90
            var settings = gridData[$grid.prop('id')].settings;
            var data = {};
            $.each($(settings.filterSelector).serializeArray(), function () {
                data[this.name] = this.value;
            });
91

Qiang Xue committed
92 93 94 95 96
            $.each(yii.getQueryParams(settings.filterUrl), function (name, value) {
                if (data[name] === undefined) {
                    data[name] = value;
                }
            });
97

Qiang Xue committed
98 99
            var pos = settings.filterUrl.indexOf('?');
            var url = pos < 0 ? settings.filterUrl : settings.filterUrl.substring(0, pos);
100

Qiang Xue committed
101 102 103 104 105
            $grid.find('form.gridview-filter-form').remove();
            var $form = $('<form action="' + url + '" method="get" class="gridview-filter-form" style="display:none" data-pjax></form>').appendTo($grid);
            $.each(data, function (name, value) {
                $form.append($('<input type="hidden" name="t" value="" />').attr('name', name).val(value));
            });
106 107
            
            event = $.Event(gridEvents.beforeFilter);
108
            $grid.trigger(event);
109 110 111 112
            if (event.result === false) {
                return;
            }

Qiang Xue committed
113
            $form.submit();
114
            
115
            $grid.trigger(gridEvents.afterFilter);
Qiang Xue committed
116
        },
117

Qiang Xue committed
118 119 120 121 122 123 124
        setSelectionColumn: function (options) {
            var $grid = $(this);
            var id = $(this).prop('id');
            gridData[id].selectionColumn = options.name;
            if (!options.multiple) {
                return;
            }
125 126 127
            var checkAll = "#" + id + " input[name='" + options.checkAll + "']";
            var inputs = "#" + id + " input[name='" + options.name + "']";
            $(document).off('click.yiiGridView', checkAll).on('click.yiiGridView', checkAll, function () {
Qiang Xue committed
128 129 130 131 132 133 134
                $grid.find("input[name='" + options.name + "']:enabled").prop('checked', this.checked);
            });
            $(document).off('click.yiiGridView', inputs + ":enabled").on('click.yiiGridView', inputs + ":enabled", function () {
                var all = $grid.find("input[name='" + options.name + "']").length == $grid.find("input[name='" + options.name + "']:checked").length;
                $grid.find("input[name='" + options.checkAll + "']").prop('checked', all);
            });
        },
Qiang Xue committed
135

Qiang Xue committed
136 137 138 139 140 141 142 143 144 145 146
        getSelectedRows: function () {
            var $grid = $(this);
            var data = gridData[$grid.prop('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;
        },
Qiang Xue committed
147

Qiang Xue committed
148 149 150 151 152 153
        destroy: function () {
            return this.each(function () {
                $(window).unbind('.yiiGridView');
                $(this).removeData('yiiGridView');
            });
        },
Qiang Xue committed
154

Qiang Xue committed
155 156 157 158 159
        data: function () {
            var id = $(this).prop('id');
            return gridData[id];
        }
    };
Qiang Xue committed
160
})(window.jQuery);