Search code examples
javascriptslickgrid

Slickgrid- getting selected cell value , id and field?


i'm working with slickgrid and i'm quit new in slickgrid. i want to know is there any function through which i can get the complete info of all the cell to a specific row where user will click ??? also i want to get values before and after editing in the specific cell so that i can measure the change in the cell. for getting the active cell (i.e. where user clicked) i'm using

ifkaar_scenarioConfigTable.onClick.subscribe(cellClicked);

and i'm checking where the cell is my desired cell(i.e. where user is allowed to do editing/modification) as following

function cellClicked(e) {
var cell = ifkaar_scenarioConfigTable.getCellFromEvent(e);
if (col[cell.cell].id == "orderqty") {
    console.log("orderqty pressed");
    }
}

this is working fine , i.e. when i click on any cell , it tell whether it is "orderqty" or not , but further i want to get its value and other cells' value in order to calculate the changes. I've searched but couldn't find any clear article (or i can't understood properly). any help will be highly appreciated. Thanks


Solution

  • the onClick event passes the row as an argument

    Get the data item for a clicked row

    function cellClicked(e, args) {
        var item = grid.getDataItem(args.row);
    }
    

    Check if a click happened in a specific column

    function cellClicked(e, args) {
        if (args.cell == grid.getColumnIndex('orderqty')) {
            console.log("orderqty pressed");
        }
    }
    

    You could even pull this filtering functionality out into its own function and pass a callback when a click happens in that column

    function forColumn(row, cell, columnID, fn) {
        var cellNode = grid.getCellNode(row, cell);
        var dataContext = grid.getDataItem(row);
        if (cellNode && grid.getColumns()[cell].id === columnID) {
            fn.call(this, cellNode, dataContext, row, cell);
        }
    }
    function cellClicked(e, args) {
        forColumn(args.row, args.cell, 'orderqty', function (cellNode, dataContext, row, cell) {
            console.log("orderqty pressed");
        });
    }
    

    Values before and after edit

    To get the values of a cell before and after an edit you will need to handle this in the isValueChanged function in the editor for a column.