How do I return a specific cell value in SlickGrid

2019-05-31 15:29发布

问题:

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)