Author Topic: Adding Select All Checkbox in Header of Grid  (Read 8211 times)

sachin_jain139

  • Newbie
  • *
  • Posts: 4
    • View Profile
Adding Select All Checkbox in Header of Grid
« on: March 02, 2014, 05:11:59 pm »
Hi All,

I am creating an application where the requirement is to add a Checkbox column in grid and in header there will be check box which will select/unselect all checkboxes in that column. I have added that column successfully but the problem is with Select All checkbox. It is selecting only first 17-18 checkboxes. When I saw it in debugger I found that only top 17-18 checkboxes are loaded by grid and others will be loaded when I scroll down. But this behavior of grid is not acceptable in this case. Please suggest.

Thanks,
Sachin

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6310
    • View Profile
Re: Adding Select All Checkbox in Header of Grid
« Reply #1 on: March 02, 2014, 05:39:53 pm »
You shouldn't make direct change in pqGrid view; it won't persist as pqGrid refreshes its view frequently.

Keep a bool column to save the state (true/false) of a checkbox. Tell pqGrid how to refresh its column view by using column.render callback.

When you click on the header checkbox, save true/false in the column field.

sachin_jain139

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Adding Select All Checkbox in Header of Grid
« Reply #2 on: March 02, 2014, 07:58:37 pm »
Hi,

Thanks for your quick reply. I tried your suggestion. Below is my code:-

function selectAll(evt)  //function called on onchange of Select All Checkbox
        {
            var isChecked = $("#cbxAll").is(":checked");
            var rowIndex;
            var data = gridData.dataModel.data;
            for (var i = 0; i < data.length; i++)
            {
                data[0] = isChecked;
                $("#grid-json").pqGrid("refreshCell", { rowIndx: i, dataIndx: 0 });
            }
        }

I have also added render function as below:-

                success: function (data)
                {
                    gridData = data;
                    $.extend(data.colModel[0],
                    {
                        render: function (ui)
                        {
                            var rowData = ui.rowData, dataIndx = ui.dataIndx;
                            var val = rowData[dataIndx];
                            str = "";
                            if (val)
                            {
                                str = "checked='checked'";
                            }
                            return "<input type='checkbox' class='cbx' style='margin:0' " + str + "onChange='cbxChange(this)'/>";
                        }
                    });
                    $grid1 = $("#grid_json").pqGrid(data);

This render function runs properly first time when grid is loaded. But it should also run when I am calling refreshCell in SelectAll function which is not  happening. Please suggest.

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6310
    • View Profile
Re: Adding Select All Checkbox in Header of Grid
« Reply #3 on: March 02, 2014, 08:14:54 pm »
You are probably right. refreshCell doesn't call render method in base version.

You don't need to call refreshCell repeatedly in a loop for performance reasons.

Instead of that just call refresh method once.

http://paramquery.com/api#method-refresh

« Last Edit: March 02, 2014, 09:04:16 pm by paramquery »

sachin_jain139

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Adding Select All Checkbox in Header of Grid
« Reply #4 on: March 02, 2014, 08:45:18 pm »
Hi,

Is there any other alternate if this does not work in base version?


paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6310
    • View Profile
Re: Adding Select All Checkbox in Header of Grid
« Reply #5 on: March 02, 2014, 09:05:50 pm »
I've updated my previous message. It works in base version too. Please read it entirely.

sachin_jain139

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Adding Select All Checkbox in Header of Grid
« Reply #6 on: March 02, 2014, 10:48:52 pm »
Thanks a lot for your kind support. Its done.. :)