Select row with code

4,657 views
Skip to first unread message

Jason

unread,
May 5, 2010, 4:30:00 PM5/5/10
to SlickGrid
I am having difficulty selecting a row through code. Can someone
please assist?

I currently have a column that uses a formatter to add a check box.

Column description:
{ id: "chk", name: "", field: "id", formatter: fmtChecker, cssClass:
"aligncenter", width: 50, resizable: false, unselectable: false,
sortable: false },

Formatter function:
var fmtChecker = function (row, cell, value, columnDef, dataContext) {
if (value == "") {
return "<i>ERROR!</i>"
} else {
return "<input type='checkbox' onClick='SelectRow(" + row +
");' value='" + value + "' />"
}
};

SelectRow function:
function SelectRow(rowidx) {
grid.setSelectedRows(rowidx)
}




I cannot get the row to set to select. However, when a row is already
selected, my clicking the checkbox will unselect it. But then I have
to click the row itself twice to select it again.

I know I can ctrl+click to select multiple rows, but I am trying to
make more intuitive for some users.

Thank you for your help!
--jason

Jason

unread,
May 5, 2010, 6:10:46 PM5/5/10
to SlickGrid
I have figured it out. In case anyone else wants the code:

Column description:
{ id: "chk", name: "", field: "id", formatter: fmtChecker, cssClass:
"aligncenter", width: 50, resizable: false, unselectable: false,
sortable: false },



Formatter function:
var fmtChecker = function (row, cell, value, columnDef, dataContext) {
if (value == "") {
return "<i>ERROR!</i>"
} else {

var isSelected = false
for (var i = 0; i < selectedRowIds.length; i++) {
if (selectedRowIds[i] == value) { isSelected = true;
break; }
}
// recheck the checkbox on redraw of cell after scroll if
prev. selected
if (isSelected == false) {
return "<input type='checkbox' onClick='SelectRow(" +
value + ");' value='" + value + "' />"
} else {
return "<input type='checkbox' checked='true'
onClick='SelectRow(" + value + ");' value='" + value + "' />"

}
}
};



SelectRow function:
function SelectRow(val) {
var isSelected = false;
for (var i = 0; i < selectedRowIds.length; i++) {
if (selectedRowIds[i] == val) {
// remove row; unchecked
selectedRowIds.splice(i, 1);
isSelected = true;
break;
}
}

if (isSelected == false) {
selectedRowIds.push(val);
}

var rows = [];
if (selectedRowIds.length > 0) {

for (var i = 0; i < selectedRowIds.length; i++) {
var idx = dataView.getRowById(selectedRowIds[i]);
if (idx != undefined) {
rows.push(idx);
}
}

}
//reselect appropiate grid rows
grid.setSelectedRows(rows);

}



Amit Jain

unread,
Aug 9, 2013, 3:17:25 PM8/9/13
to slic...@googlegroups.com
Hi Jason,
 
I am new to slickgrid so could you please let me know how to add the checkbox in columns in a list. Also please suggest me if you know any tutorial where i can find out this.
 
 
Thank You!
 
Regards,
Amit Jain

Michael Leibman

unread,
Aug 9, 2013, 3:25:27 PM8/9/13
to slickgrid
Just FYI, what you're doing already exists as a plugin - see http://mleibman.github.io/SlickGrid/examples/example-checkbox-row-select.html example.


--
You received this message because you are subscribed to the Google Groups "SlickGrid" group.
To unsubscribe from this group and stop receiving emails from it, send an email to slickgrid+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Jason E. Parrish

unread,
Aug 9, 2013, 3:43:16 PM8/9/13
to slic...@googlegroups.com
The newest version has an addin for that functionality.

When I needed it a couple of years ago, I created a formatter function for my column.

var fmtChecker = function (row, cell, value, columnDef, dataContext) {
    if (value == "") {
        return "<i>ERROR!</i>"
    } else {

        var isSelected = false
        for (var i = 0; i < selectedRowIds.length; i++) {
            if (selectedRowIds[i] == value) { isSelected = true; break; }
        }

        if (isSelected == false) {
            return "<input type='checkbox' onClick='SelectRow(event, " + value + ");' value='" + value + "' />"
        } else {
            return "<input type='checkbox' checked='true' onClick='SelectRow(event, " + value + ");' value='" + value + "' />"
        }
       
    }
};



Jason E. Parrish
Tel: 01.901.751.8693 | Mobile: 01.901.340.9964
Facebook Twitter LinkedIn Google Plus Page pinterest


--
You received this message because you are subscribed to a topic in the Google Groups "SlickGrid" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/slickgrid/uKq2aVU5luo/unsubscribe.
To unsubscribe from this group and all its topics, send an email to slickgrid+...@googlegroups.com.

vinay kumar

unread,
Aug 28, 2013, 9:22:41 AM8/28/13
to slic...@googlegroups.com
Hi,

You may use the following piece of code.

                 _grid.setActiveCell(lineNumber, 0);
 _grid.onClick.notify({row: lineNumber, cell: 0}, null, _grid);
Reply all
Reply to author
Forward
0 new messages