How do I return a specific cell value in SlickGrid?

匿名 (未验证) 提交于 2019-12-03 09:14:57

问题:

For the life of me I can't seem to return a specific cell value in SlickGrid. There is an example on SO here. But this did not work either. I am using the SlickGrid tutorial #7 as my example. All I am trying to do is add a click event to a cell that returns that cells value.

Note: I am loading my data via JSON request and all works well. My click event fires but the value is undefined. For brevity sake I will omit my grid columns and options code. Here is the code and thanks. All and all this is a great grid.

var sortcol = "id";     var sortdir = -1;     $(function () {         $.getJSON(baseURL() + 'programs', function (data) {             dataView = new Slick.Data.DataView();             grid = new Slick.Grid($("#program-grid"), data, programColumns, programOptions);              grid.onSort = function (sortCol, sortAsc) {                 sortdir = sortAsc ? 1 : -1;                 sortcol = sortCol.field;                  if (sortAsc == true) {                     data.sort(compare);                 }                 else {                     data.reverse(compare);                 }                 grid.render();             };             grid.onClick = function (e, row, cell) {                 if (programColumns[cell].id == "id") {                     var x = data[row][cell].field;//This is where I am stuck                     window.location.href = "http://google.com/" + x;                 }             }          });     }); 

回答1:

You are almost there, you should use data[row].fieldname. Do you have a field in your data called "field"?



回答2:

If you're using dataView, it's simple:

// Get the object containing row and cell index of a clicked row. var gridObject = roundGrid.getCellFromEvent(e);   var row = gridObject.row; // get selected row index var row_values = roundDataView.getItem(row); // get the row object   // Get the actual value. var cellValue = row_values[roundGrid.getColumns()[gridObject.cell].field];  


回答3:

Bind to the onCellChange event

grid.onCellChange.subscribe( function( e, args ) {      console.log( args.item[ grid.getColumns()[ args.cell ].field ] );  } ); 

Returns

"{some value}" etc. "This is the newly changed value"

Docs

onCellChange ({ row: number, cell: number, item: any })

Grid Events (onCellChange)



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!