0
votes

I have the code below where I want to add a record to the webix datatable. I'm a JS novice so not sure of the correct definition to make sure the variable scope is valid.

Basically I have a pager and datatable like so which works fine:

    webix.ready(function(){

        webix.ui({
            id: "pagerA",
            view: "pager",
            template:"{common.prev()} {common.pages()} {common.next()}",
            container: "page_section",
            size: 15,
            group: 5
        });

        webix.ui({
            container:"gasforecast",
            rows: [
            {type: "header", template: "Gas Forecast"},
            gasgrid = {
            view:"datatable",
            editable: true,
            navigation: true,
            pager: "pagerA",
            columns:[
                { id:"PKey",            header:"",          hidden:true},
                { id:"SiteName",        header:"Site",      width:250, sort:"string"},
                { id:"PercentageChange",header:"% Change",  width:100, editor: "text", sort:"int"},
                { id:"kWChange",        header:"kW Change", width:100, editor: "text", sort:"int"}
            ],
            autoheight:true,
            autowidth:true,
            select:"row",

            save: "data/gasforecastdata_save.php",
            url: "data/gasforecastdata.php"
        }]});
    });

Now I want to be able to have a button which when clicked adds a row to the datatable, so I add:

        cmdAddRow = webix.ui({
            container:"addbutton",
            view:"button",
            label: "Add Site",
            click: function() {
                var data = {"SiteName": "X", "PercentageChange": 1, "kWChange": 0};
                gasgrid.add(data);
            }

        });

However when the button is clicked it generates a 'Uncaught TypeError: gasgrid.add is not a function'

I have tried adding an id: property to the datatable and reference that, but I still get an error. Not sure what to do?

Thanks

Gary

1

1 Answers

1
votes

Yep, usage of ID is the correct approach.

If you change the init code like next

{ type: "header", template: "Gas Forecast"},
{ view:"datatable", id:"gasgrid", editable: true

Later you will be able to reference the datatable and call its API

webix.ui({
    container:"addbutton",
    view:"button",
    label: "Add Site",
    click: function() {
       var data = {"SiteName": "X", "PercentageChange": 1, "kWChange": 0};
       $$("gasgrid").add(data);
    }
})