Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jqGrid multiselect, checked-box persist when move to the next page

Tags:

jqgrid

If you see jqGrid demo :

http://www.trirand.com/blog/jqgrid/jqgrid.html

Section : Advanced --> Multiselect

You'll see that the checked checkbox is not persist when i move to the next page, and back to the previouse page again ..

How to make it persist ?

It's my scenario, in my applications there is a group functionality where i can add several customer, i'm using jqgrid to display thousands of customers.

I want to check every customer i want, and then submit this selected customer and add it to the specified group ..

How can i do this ? (make jqgrid, multiselect persist ?)

Thanks.

like image 637
Wildan Maulana Avatar asked May 12 '11 18:05

Wildan Maulana


3 Answers

This is fairly simple to do using the gridComplete and onPaging events plus the jquery .data() method. This is much simpler than a lot of the stuff I've seen floating around the net, so I thought I'd share it. The selector for my grid is '#employeerolegrid'.

       gridComplete: function () {

            var currentPage = $(this).getGridParam('page').toString();

            //retrieve any previously stored rows for this page and re-select them
            var retrieveSelectedRows = $(this).data(currentPage);
            if (retrieveSelectedRows) {
                $.each(retrieveSelectedRows, function (index, value) {
                    $('#employeerolegrid').setSelection(value, false);
                });
            }
        },
        onPaging: function () {
            var saveSelectedRows = $(this).getGridParam('selarrrow');
            var page = $(this).getGridParam('page') - 1;

            //Store any selected rows
            $(this).data(page.toString(), saveSelectedRows);
        }
like image 180
Tom Mooney Avatar answered Oct 26 '22 23:10

Tom Mooney


Look at the event list here http://www.trirand.com/jqgridwiki/doku.php?id=wiki:events

The logic is: everytime the "onPaging" event is fired, you should iterate through each row and store the unique id of each row to an array, also iterate through your array of id and check all of the select box everytime the "onPaging" is fired.

like image 44
laychinhan Avatar answered Oct 27 '22 00:10

laychinhan


My solution: (define variable current_page and set in Event loadBeforeSend) because

var page = $(this).getGridParam('page') - 1; not work.

var current_page=0;

...

   gridComplete: function () {

        var currentPage = $(this).getGridParam('page').toString();

        //retrieve any previously stored rows for this page and re-select them
        var retrieveSelectedRows = $(this).data(currentPage);
        if (retrieveSelectedRows) {
            $.each(retrieveSelectedRows, function (index, value) {
                $('#employeerolegrid').setSelection(value, false);
            });
        }
    },
    onPaging: function () {
        var saveSelectedRows = $(this).getGridParam('selarrrow');
        //Store any selected rows
        $(this).data(current_page, saveSelectedRows);
    },
    loadBeforeSend:function(){
                    //set current page
        current_page = $(this).getGridParam('page').toString();
}

Function get multi select Values array

function getSelectedValues(){

    var saveSelectedRows = $("#YourGrid").getGridParam('selarrrow');

    $("#YourGrid").data(current_page, saveSelectedRows);

    var retrieveSelectedRows = $("#YourGrid").data();       

    var arr_values = new Array();

    if (retrieveSelectedRows) {
        $.each(retrieveSelectedRows, function (index, value) {
            $.each(value, function (index, sub_value) {
                if(typeof(sub_value)=='string')
                arr_values.push(sub_value);
            });
        });
    }

    return arr_values;
}
like image 22
Juanes Avatar answered Oct 27 '22 00:10

Juanes