剑道数据源,AngularJS -未定义属性



我想用一个从webservice返回的复杂json填充一个网格。我的json包含两个内容:

  • data:用于填充网格的记录数组
  • columns:包含网格配置(布局)的数组

通过指定schema.data.

,我已经成功地用"data"填充了网格。

我的问题是网格配置(布局)。我在数据源的requestEnd事件上获得列数组,并将其添加到customersSource(数据源)中,以便我可以在gridOptions中访问它。

问题是,即使当我记录customersSource对象时,我看到我添加的cols数组,存在并且填充了适当的数据,$scope.mainGridOptions.columns没有设置为customersSource.cols

我认为这可能与customersSource.cols是异步设置的事实有关,但不应该用它的数据绑定来处理这个问题吗?

我也在数据源vs. Angular中读到,我可能要把一些东西设置为可观察的,但我很困惑到底该怎么做。

我怎么能解决这个问题?

下面是我的代码:
var customersSource = new kendo.data.DataSource({
        transport: {
            read: {
                url: "http://....",
                dataType: "json"
            }
        },
        schema: {
            data: "data"
        },
        requestEnd: function (e) {
            this.cols = e.response.columns;
        }
    });
$scope.mainGridOptions = {
        dataSource: customersSource, // OK
        columns: customersDataSource.cols, // undefined - uses default
        height: 500,
        scrollable: true,
        selectable: true
    };

这是我的JSON

{
  "data": [
    {
      "id": 0,
      "firstname": "Dalton",
      "lastname": "Holden",
      "gender": "male",
      "email": "daltonholden@tellifly.com",
      "phone": "871-407-2973",
      "address": "22 National Drive, Brenton, Louisiana",
      "birthday": "21/04/1965",
      "currency": "GBP"
    },
    {
      "id": 1,
      "firstname": "Allyson",
      "lastname": "Odom",
      "gender": "female",
      "email": "allysonodom@tellifly.com",
      "phone": "922-548-2725",
      "address": "44 Quincy Street, Thynedale, Georgia",
      "birthday": "28/08/1961",
      "currency": "CHF"
    },
    {
      "id": 2,
      "firstname": "Sweet",
      "lastname": "Branch",
      "gender": "male",
      "email": "sweetbranch@tellifly.com",
      "phone": "880-593-2244",
      "address": "81 Fenimore Street, Veguita, Missouri",
      "birthday": "08/08/1953",
      "currency": "AUD"
    }
  ],
  "columns": [
    {
      "field": "firstname",
      "title": "Frist Name",
      "width": 200,
      "attributes": {
        "class": "",
        "style": "text-align: left;"
      },
      "headerAttributes": {
        "class": "table-header-cell",
        "style": "text-align: left;"
      }
    },
    {
      "field": "lastname",
      "title": "Last Name",
      "attributes": {
        "class": "",
        "style": "text-align: left;"
      },
      "headerAttributes": {
        "class": "table-header-cell",
        "style": "text-align: left;"
      }
    },
    {
      "field": "gender",
      "title": "Gender",
      "attributes": {
        "class": "",
        "style": "text-align: left;"
      },
      "headerAttributes": {
        "class": "table-header-cell",
        "style": "text-align: left;"
      }
    },
    {
      "field": "email",
      "title": "e-mail",
      "attributes": {
        "class": "",
        "style": "text-align: left;"
      },
      "headerAttributes": {
        "class": "table-header-cell",
        "style": "text-align: left;"
      }
    },
    {
      "field": "phone",
      "title": "Phone Number",
      "attributes": {
        "class": "",
        "style": "text-align: right;"
      },
      "headerAttributes": {
        "class": "table-header-cell",
        "style": "text-align: right;"
      }
    },
    {
      "field": "address",
      "title": "Address",
      "attributes": {
        "class": "",
        "style": "text-align: left;"
      },
      "headerAttributes": {
        "class": "table-header-cell",
        "style": "text-align: left;"
      }
    },
    {
      "field": "birthday",
      "title": "Birthday",
      "attributes": {
        "class": "",
        "style": "text-align: center;"
      },
      "headerAttributes": {
        "class": "table-header-cell",
        "style": "text-align: center;"
      }
    },
    {
      "field": "currency",
      "title": "Currency",
      "attributes": {
        "class": "",
        "style": "text-align: center;"
      },
      "headerAttributes": {
        "class": "table-header-cell",
        "style": "text-align: center;"
      }
    }
  ]
}

编辑我为我的测试项目创建了一个柱塞。正如你所看到的,我可以填充网格,但我有一个问题与mainGridOptions.columns。任何帮助将非常感激!http://plnkr.co/edit/5pjFQGkgTivqVkxsFBse

这是因为angularjs不知道第三方所做的更改,所以你错过了神奇的双向数据绑定。虽然我认为承诺对你来说会更有效

requestEnd: function (e) {
  $scope.$apply(function(){
    $scope.mainGridOptions.columns = e.response.columns
  })
}

最新更新