Monday, 17 March 2014

Yii CGridView Update onkeyup

By default Yii filters the table in cgridview, when a user entering data on search textfield and press Enter key or clicking oustside the grid.
I had a strange situation, i want to apply filter on keyup in cgridview for all pages,

Below is the code to apply filter on keyup in  yii cgridview.

    $(function() {
        $('body').on('keyup', '.filters > td > input', function() {
           
            var focusedId = $(document.activeElement).attr('id');
            var grid_id = $('#' + focusedId + '.form').closest("div").attr('id');
           
            $('#' + grid_id).yiiGridView('update', {
                data: $(this).serialize(),
                complete: function(jqXHR, status) {
                   
                    if (status == 'success') {
                        var tmpStr = $('#' + focusedId + '.form').val();
                        $('#' + focusedId + '.form').focus();
                        $('#' + focusedId + '.form').val('');
                        $('#' + focusedId + '.form').val(tmpStr);
                    }
                   
                }
            });
            return false;
        });

    });