将按钮单击附加到 webix 数据表

Attach button click to webix datatable

我有下面的代码,我想在其中向 webix 数据表添加一条记录。我是 JS 新手,所以不确定正确的定义以确保变量范围有效。

基本上我有一个像这样的寻呼机和数据表,工作正常:

    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"
        }]});
    });

现在我希望能够有一个按钮,单击该按钮会向数据表添加一行,因此我添加:

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

        });

但是当点击按钮时它会生成一个 'Uncaught TypeError: gasgrid.add is not a function'

我已经尝试添加一个 id: 属性 到数据表并引用它,但我仍然收到错误。不确定该怎么做?

谢谢

加里

是的,使用 ID 是正确的方法。

如果像接下来那样更改初始化代码

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

稍后您将能够引用数据表并调用其 API

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