Search code examples
slickgrid

How can I set or change a value of a concrete cell programmatically?


Using a pure array as data source (not a DateView) to initialize a SlickGrid, I want to change a value after grid creation on a specific row / column (a cell).

Here is the simple example, that can also be found as a working JSFiddle and I want for example to change the value of "Duration" on row 3 ("Task 2") from "5 days" to "7 days".

<div id="myGrid" style="width:400px;height:400px;"></div>   

<script>
  var grid;
  var columns = [
    {id: "title", name: "Title", field: "title"},
    {id: "duration", name: "Duration", field: "duration"},
    {id: "%", name: "% Complete", field: "percentComplete"},
    {id: "start", name: "Start", field: "start"},
    {id: "finish", name: "Finish", field: "finish"},
    {id: "effort-driven", name: "Effort Driven", field: "effortDriven"}
  ];

  var options = {
    enableCellNavigation: true,
    enableColumnReorder: false
  };

  $(function () {
    var data = [];
    for (var i = 0; i < 500; i++) {
      data[i] = {
        title: "Task " + i,
        duration: "5 days",
        percentComplete: Math.round(Math.random() * 100),
        start: "01/01/2009",
        finish: "01/05/2009",
        effortDriven: (i % 5 == 0)
      };
    }

    grid = new Slick.Grid("#myGrid", data, columns, options);
    grid.setSelectionModel(new Slick.CellSelectionModel());

    /* needed method */
    grid.setCellValue(2, 1, "7 days");
    /* or */
    grid.setCellValue(2, "duration", "7 days");

  })
</script>

It looks like a method like

grid.setCellValue(row, column, value);

does not exist.

Is there a way (or existing method I didn't found yet) to achieve this without using a DataView?


Solution

  • Just use

    data[2].duration = "7 Days";
    

    and then refresh the grid:

    grid.invalidateAllRows();
    grid.render();
    

    The grid only has display related helper methods, although you can get the data with grid.getData(). Editors, if you use them, contain code to extract the value from a row (data[n]) and write it to the editor UI, and then go back the other way.