I have and igGrid and am trying to find use right way of getting the selected row(s), its index, and values of cells in that row. My GRID has this features :
features: [{ name: "RowSelectors", enableCheckBoxes: false, enableRowNumbering: true },{ name: "Selection", mode: "row", multipleSelection: false //rowSelectionChanged:rowSelection },{ name: "Sorting", firstSortDirection: "ascending", type: "local" }]
I went to this site example : http://www.igniteui.com/grid/grid-api-events and copied this code exactly :
var rows = $("#grid").igGridSelection("selectedRows"); apiViewer.log("The number of selected rows is: " + rows.length); $.each(rows, function (i, val) { apiViewer.log("Row with index " + val.index + " is selected"); });
and it doesnt work at all. I tried many things :
//var idx = document.getElementById("rsGrid")._selectedRow.index; // _selected_row undefined //var idx = $("#rsGrid")._selectedRow.index; // _selected_row undefined
var rows = $("#grid").igGridSelection("selectedRows"); // works?? $.each(rows, function (i, val) { var aa = "Row with index " + val.index + " is selected"; // val.index is undefined var cc = "Row with index " + val.index() + " is selected"; // 'index' is not a function var ID = $("#rsGrid").getCellValue(val.index, "idField"); // fails });
Other things that do NOT work :
var rows = $("#rsGrid").igGridSelection("selectedRows"); if ( rows.length != 1 ) { } // rows.length is ALWAYS 1
$('#rsGrid').igGridSelection('clearSelection'); // does NOT clear the selection
The ONLY thing I could get to work is this :
var grid = $("#rsGrid").data("igGrid"); var idx = grid._selectedRow.index; var ID = grid.getCellValue(grid._selectedRow.index, "idField");
which is not the right way of coding it, as it uses an undocumented attribute and will NOT work for a multiple selection grid.
What am I doing wrong and why doesnt this work??
Thank you.
Hello,
In order to obtain the selected row when multiple selection is disabled you have to use the selectedRow method:
$("#grid").igGridSelection("selectedRow");
If you enable multiple selection then the correct method to use will be selectedRows.
In the former case you'll get an object which will contain the row element selected, its ID if you have a primary key defined and its index. You can then use it to call getCellValue. As it is a public method you don't need to get the grid instance to use it. Instead you could do:
$("#grid").igGrid("getCellValue", <row id / row index>, <col key>);
Please, refer to the API documentation for more information regarding these functions. It's important to note that getCellValue will always consider the first parameter to be a row ID if you define a primary key for the grid (and index otherwise).
I hope this helps! Please, let me know if you have any other questions or concerns!
Best regards,
Stamen Stoychev
Well, I tried this and it didn't work either.
var row = $("#rsGrid").igGridSelection("selectedRow"); // works??
var idx = row.index; // idx = a function
var idx = row.index(); // sets to '8' no matter which row is selected
var ID = $("#rsGrid").igGrid("getCellValue", idx, "releaseSetId"); // Gets the ID of row 8, which wasn't selected.
$("#rsGrid").igGridSelection("clearSelection"); // This still doesn't work.
Why is this not working?
Thanks for any help.
I am attaching a sample demonstrating how to use the object selectedRow returns as input for getCellValue, as well as a working clearSelection call. Could you please modify the sample to better match your scenario and possibly reproduce the issue you are having so I can take a look?
I am looking forward to hearing from you!
Well, I finally found it! When your sample worked within my app I thought for sure that it was because we're using an older version of infragistics code. But that wasn't it either.
Turns out the problem was that my GRID was contained in a DIV and NOT a TABLE element! I inherited this app and ALL the grids are in DIVs. Once I knew that was the issue, I found :
http://help.infragistics.com/Doc/jQuery/2013.1/CLR4.0?page=Known_Issues_and_Limitations_2013_Volume_1.html
http://www.infragistics.com/community/forums/t/83786.aspx
http://www.infragistics.com/community/blogs/craig_shoemaker/archive/2013/02/04/avoid-this-obscure-error-when-using-the-iggrid-control-by-selecting-the-right-root-element.aspx
Now I expect all the APIs to work, but there is still a 'problem' with using the TABLE element. Currently my grids are within FIELDSET elements. But the grid TABEL element does not seem to work within a FIELDSET - it is always rendered outside of the Fieldset box.
Can you help with this?
We still wrap the table element inside a div with id = "<table id>_container". It's just that when the grid is initialized on a table element, the feature widgets are also initialized on the same table element. This means you can make igGrid calls and i.e. igGridSelection calls with the same selector. When you initialize on a div, it'll work as the outermost container while the feature widgets are initialized on the table created internally. There aren't other major differences between the two and when you are aware that you need to modify your selector to use the feature widgets you shouldn't have issues using divs.
With this said I tried adding a few grids initialized on a table to a fieldset and they were staying inside. I may need to look at a sample of your project to pinpoint what could be causing this issue. However, if initializing on divs works for your project, I don't see a reason to change it to tables.