jqgrid single select checkbox
Asked Answered
O

4

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

Orpha answered 20/7, 2011 at 1:48 Comment(0)
E
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.

Eshman answered 21/7, 2011 at 8:38 Comment(3)
you need to add this : var myGrid = $("#list47"); $("#cb_"+myGrid[0].id).hide(); to hide the multiselect button as described here : #6213071Hint
Also it won't work if grouping is active (at least in this form).Hint
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); }Nightcap
A
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;
        }
Addams answered 2/4, 2014 at 23:1 Comment(0)
L
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?

Limb answered 20/7, 2011 at 1:53 Comment(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.Hint
C
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.

Charmaincharmaine answered 30/11, 2012 at 20:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.