5

In Jqgrid, I want to restrict user to select only one check box at any time. When user selects multiple check box only last selected to be in 'selected' state, remaining should be automatically un-selected.

I have set multi select attribute to true. But I am not able to do un-select previously selected item. Is it possible to do if so how?

Thanks

user669789
  • 554
  • 4
  • 10
  • 23

4 Answers4

9

You can use the event beforeSelectRow and reset the selection:

beforeSelectRow: function(rowid, e)
{
    jQuery("#list47").jqGrid('resetSelection');
    return(true);
}

I've got a fiddle for you so you can check how it works.

LeftyX
  • 35,328
  • 21
  • 132
  • 193
  • 2
    you need to add this : var myGrid = $("#list47"); $("#cb_"+myGrid[0].id).hide(); to hide the multiselect button as described here : http://stackoverflow.com/questions/6212571/jqgrid-multiselect-check-all-in-header-how-to-hide-it/6213082#6213082 – Matthieu Sep 09 '11 at 13:51
  • 1
    Also it won't work if grouping is active (at least in this form). – Matthieu Sep 09 '11 at 18:09
  • 1
    This does not allow unselect on clicking again, for that fix would be: beforeSelectRow: function(rowid, e) { jQuery('#list47').jqGrid('getGridParam','selarrrow') != rowid) jQuery("#list47").jqGrid('resetSelection'); return(true); } – Azghanvi Sep 29 '19 at 05:04
3

You have to do some more stuff:

1. Set multiboxonly to true and multiselect to true

2. Define the events onSelectRow and beforeSelectRow:

3. Define global variable: var lastSel;


The OnSelectRow and beforeSelectRow implementation:

onSelectRow: function (rowId, status, e) {
        if (rowId == lastSel) {
            $(this).jqGrid("resetSelection");
            lastSel = undefined;
            status = false;
        } else {
            lastSel = rowId;
        }
    },
beforeSelectRow: function (rowId, e) {
            $(this).jqGrid("resetSelection");
            return true;
        }
2

I know this question is old, but I found a better solution in this Stack Post.

All you have to do is set the following two properties of the jqGrid:

multiselect:true // multi-select checkboxes appear

multiboxonly:true // checkboxes act like radio buttons where only one is selected at a time

I have updated the fiddle made by LeftyX to simply use the multiboxonly setting at this JsFiddle

This was what I needed. It may help someone else.

Community
  • 1
  • 1
2

Having a checkbox in each column implies that you can click more than one at a time. What you are asking for is basically the multiselect: false behavior but with the checkbox column - are you sure you really want the checkboxes in this case?

Justin Ethier
  • 131,333
  • 52
  • 229
  • 284
  • 1
    +1, good to know also that the onSelectRow event pops if you click on a row without multiselect active, so you can still handle the selection. – Matthieu Sep 09 '11 at 18:11