6

Multiselect in JQGrid only allows either multiple selection or single selections and the shift functionality isn't what I'd expect the shift select to do. I also don't like that we need comboboxes with multiselect.

What other solution could I use for multiselect?

user229044
  • 232,980
  • 40
  • 330
  • 338
Bob
  • 3,074
  • 11
  • 43
  • 62
  • 4
    I dont think SO is really the place to be displaying your wares. – wmitchell Nov 15 '10 at 16:34
  • 1
    SO is a QA site...you didn't ask a question... Write a blog. I Vote to close – Chris Kooken Nov 15 '10 at 16:39
  • 1
    not trying to be m8 it looks good i think tho there might be better places to put it. ie at the Jquery plugin site. While its good to see enthusiasm I just dont want SO to turn into an openair market. – wmitchell Nov 15 '10 at 16:39
  • 3
    @Byron: This should just be presented in the normal Q&A format. Ask the question that led you to this solution, then post your solution as an answer. – Bill the Lizard Nov 15 '10 at 16:48
  • 3
    As Bill says, from the FAQ: `It's also perfectly fine to ask and answer your own question, as long as you pretend you're on Jeopardy: phrase it in the form of a question.` – Justin Ethier Nov 15 '10 at 16:54
  • Done - I rarely use other sites while code, and expect the same from many others - hence the reason I put a solution up here. – Bob Nov 15 '10 at 17:00
  • @Byron - sorry my comments sounded a little harsh I wasnt trying to offend. – wmitchell Nov 15 '10 at 17:02

1 Answers1

28

[Oct 2011] Updated to use 4.0 API, corrected shift-selection bugs, simplified selection loop. Tested in 4.2.0.


If like me, you needed a proper multiselect in the jqgrid - where ctrl selects a single row at a time, select selects multiple rows and neither clear the selection and selects 1 row - You've found it.

First things first: set multiselect: true in the grid definition (I didn't set any other multiselect options)

Next: In gridComplete: function () {} set grid.jqGrid('hideCol', 'cb'); - this hides the checkboxes if you don't want them.

Finally: The main part

beforeSelectRow: function (rowid, e) {
    if (!e.ctrlKey && !e.shiftKey) {
        $("#grid").jqGrid('resetSelection');
    }
    else if (e.shiftKey) {
        var initialRowSelect = $("#grid").jqGrid('getGridParam', 'selrow');
        $("#grid").jqGrid('resetSelection');

        var CurrentSelectIndex = $("#grid").jqGrid('getInd', rowid);
        var InitialSelectIndex = $("#grid").jqGrid('getInd', initialRowSelect);
        var startID = "";
        var endID = "";
        if (CurrentSelectIndex > InitialSelectIndex) {
            startID = initialRowSelect;
            endID = rowid;
        }
        else {
            startID = rowid;
            endID = initialRowSelect;
        }

        var shouldSelectRow = false;
        $.each($("#grid").getDataIDs(), function(_, id){
            if ((shouldSelectRow = id == startID || shouldSelectRow)){
              $("#grid").jqGrid('setSelection', id, false);
            }
            return id != endID;                        
        });
    }
    return true;
}

The End - Hope that helps

Cody
  • 2,467
  • 2
  • 21
  • 30
Bob
  • 3,074
  • 11
  • 43
  • 62
  • Thanks for posting this, however it would be slightly more useful if the code was properly tested... – Justin Ethier Nov 15 '10 at 20:21
  • 1
    Thanks, One note: you should replace all occurrences of $('#grid') with $(this). – brianray Sep 03 '11 at 20:05
  • @Byron Cobb: Excellent solution, +1 from me. Selection also highlights text in grid. How to prevent text highlighting? How to allow to add/remove selected from to/from selection if shift+up/down arrow key is pressed. – Andrus Sep 04 '11 at 13:58
  • @Byron-Cobb: `gridComplete: function () { $('#grid').jqGrid('hideCol', 'cb') }` decrases last column width also so it cannot used – Andrus Sep 04 '11 at 14:59
  • 1
    `$grid.jqGrid("getDataIDs")` should used to allow to work if legacy calls are disabled – Andrus Apr 03 '12 at 18:41
  • Works fine but has kind of an off by one error where the clicked row does not get selected. Add this after the each loop: grid.jqGrid('setSelection', rowid, false); – GP24 Mar 29 '17 at 15:05