how to get cell value when row and column index is available in slickgrid -
i trying edit cell value in slickgrid should automatically update row sum column sum forms last row , last column of grid. here using approach index of edited row , column.also know index of row , column of cell row2 & column2, has updated. unable value row2 & column2. in short, how can value of cell row index column index known. grid.getcellnode() , datacontext = grid.getdataitem(row); not working. can me resolve issue plz.
thank in advance.
you'll need lookup row using grid.getdataitem(row);
. given know column index, you'll need access either columns
object used initialize grid or access grid instance retrieve columns via getcolumns.
together 2 functions provide access data value providing access field
property within column definition. field
value should correspond individual properties within data object.
so should able retrieve data value using:
var row = grid.getdataitem(rowindex); var field = grid.getcolumns()[columnindex].field value = row[field]
note in following example field
property set numeric index. i've repurposed example old example required row selected display data values. use comboboxes select row/column pair select checkbox next row , value appear.
.centered { text-align: center }
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script> <link rel="stylesheet" type="text/css" href="http://mleibman.github.io/slickgrid/slick.grid.css"> <script src="http://code.jquery.com/jquery-2.1.1.min.js"></script> <script src="http://code.jquery.com/ui/1.11.0/jquery-ui.min.js"></script> <script src="http://mleibman.github.io/slickgrid/slick.dataview.js"></script> <script src='http://mleibman.github.io/slickgrid/lib/jquery.event.drag-2.2.js'></script> <script src='http://mleibman.github.io/slickgrid/slick.core.js'></script> <script src='http://mleibman.github.io/slickgrid/slick.grid.js'></script> <script src='http://mleibman.github.io/slickgrid/plugins/slick.checkboxselectcolumn.js'></script> <script src='http://mleibman.github.io/slickgrid/plugins/slick.rowselectionmodel.js'></script> <script src='http://mleibman.github.io/slickgrid/controls/slick.columnpicker.js'></script> <script src='http://mleibman.github.io/slickgrid/slick.editors.js'></script> <table> <tr class='centered'> <td>row</td> <td>column</td> <td>value</td> </tr> <tr> <td> <select data-bind='options: items, optionscaption: "select index", optionsvalue: "id", optionstext: "0", value: rowindex'></select> </td> <td> <select data-bind='options: columns, optionscaption: "select index", optionsvalue: "field", optionstext: "name", value: columnindex'></select> </td> <td> <span data-bind='text: value'></span> </td> </tr></table> <div id="mygrid" style="width:600px;height:500px;"></div> <script> var vm = {items:ko.observablearray(), rowindex: ko.observable(), columnindex: ko.observable()} var grid; var data = []; var options = { editable: false, enablecellnavigation: true, asynceditorloading: false, autoedit: false, forcefitcolumns: true }; var columns = []; var datacontainer = {} $(function() { var checkboxselector = new slick.checkboxselectcolumn({ cssclass: "slick-cell-checkboxsel" }); columns.push(checkboxselector.getcolumndefinition()); (var = 0; < 5; i++) { columns.push({ id: i, name: string.fromcharcode("a".charcodeat(0) + i), field: i, width: 100 }); } (function generatedata() { (var = 0; < 10; i++) { datacontainer[i] = {} var d = (data[i] = {}); (x in columns) { datacontainer[i][x] = math.floor((math.random() * 100) + 1); } d[0] = "row " + i; d.id = } })(); grid = new slick.grid("#mygrid", data, columns, options); grid.setselectionmodel(new slick.rowselectionmodel({selectactiverow: false})); grid.registerplugin(checkboxselector); var columnpicker = new slick.controls.columnpicker(columns, grid, options); var context = {}; grid.onmouseenter.subscribe(function(evt, args) { var cell = grid.getcellfromevent(evt) context.rowindex = cell.row; context.row = grid.getdataitem(cell.row); }) grid.onmouseleave.subscribe(function(evt, args) { context = {} }) grid.onselectedrowschanged.subscribe(function(evt, args) { //console.log(args.grid.getselectedrows()) if (!context.row) { var rows = grid.getdata(); (r in rows) { var row = rows[r] (i = 1; < columns.length; ++i) { row[i] = args.rows.length == 0 ? '' : datacontainer[r][i] } } grid.invalidateallrows(); } else { var display = args.rows.indexof(context.rowindex) >= 0; (i = 1; < columns.length; ++i) { context.row[i] = display ? datacontainer[context.rowindex][i] : '' } grid.invalidaterow(context.rowindex); } grid.render(); }); grid.onselectedrowschanged.subscribe(function(e, args){vm.columnindex.valuehasmutated()}) vm.items = data.slice() vm.columns = columns.slice(); vm.columns.splice(0,1) vm.value = ko.computed(function(){ if(vm.rowindex() > -1 && vm.columnindex() > -1) { var row = grid.getdataitem(vm.rowindex()); var field = vm.columnindex() return row[field] } return "" }) ko.applybindings(vm) }) </script>
Comments
Post a Comment