How do I return a specific cell value in SlickGrid

2019-05-31 15:22发布

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;
                }
            }

        });
    });

3条回答
你好瞎i
2楼-- · 2019-05-31 15:50

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楼-- · 2019-05-31 16:08

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

查看更多
倾城 Initia
4楼-- · 2019-05-31 16:13

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)

查看更多
登录 后发表回答