Question

<div id="gridDiv"></div>
<button id="addRow"  data-dojo-type="dijit.form.Button">Add Row</button>


require(['dojo/_base/lang', 'dojox/grid/DataGrid' , 'dojo/data/ItemFileWriteStore' , 'dojo/dom' , 'dojo/domReady!'],
  function(lang, DataGrid, ItemFileWriteStore, Button, dom){
    /*set up data store*/
    var data = {
      identifier: "id",
      items: []
    };

    var store = new ItemFileWriteStore({data: data});

    /*set up layout*/
    var layout = [[
      {'name': 'Column 1', 'field': 'id', 'width': '100px'},
      {'name': 'Column 2', 'field': 'col2', 'width': '100px'},
      {'name': 'Column 3', 'field': 'col3', 'width': '200px'},
      {'name': 'Column 4', 'field': 'col4', 'width': '150px'}
    ]];

    /*create a new grid*/
    var grid = new DataGrid({
        id: 'grid',
        store: store,
        structure: layout,
        rowSelector: '20px'});

    /*append the new grid to the div*/
    grid.placeAt("gridDiv");

    /*Call startup() to render the grid*/
    grid.startup();
});
  • I have a programmatically created dojox/grid/DataGrid with no data(empty store) in it.. only layout.
  • And i have a button outside the grid.
  • My need is, on click of button, i need to add one empty row into grid. that empty row should not be from json. it should be from store.
  • it means onclick of button, an empty row should be added to store and then grid gets loaded with that store. is it possible?
Was it helpful?

Solution

This fiddle adds a new item to the store when the button is clicked. And the grid will update itself to reflect the store's new contents.

The meat of the code is this:

var id = 0;

var button = new Button({
    onClick: function () {
        store.newItem({
            id: id,
            col2: "col2-" + id,
            col3: "col3-" + id,
            col4: "col4-" + id
        });
        id++;
    }
}, "addRow");

Screenshot (after 2 button presses):

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top