Vuex如何在传输读取中将存储绑定到KendoUi-Vue Grid

时间:2018-05-21 18:54:14

标签: vuex kendo-ui-vue

我正在尝试将Kendo UI上的数据与来自Vuex getter的数据绑定。

我尝试过以下但没有运气。请帮助我在vuex或kendo上遗漏了什么。

Kendo Extensions:

<kendo-grid :data-source="kendoDataSource">
</kendo-grid>

组件:

  computed: {
    customers() {
      return this.$store.getters.customers;
    }
  },
  data() {
    return {
      kendoDataSource: {
        schema: {
          data: function(response) {
            return response;
          },
          model: {
            id: "CustomerID",
            fields: {
              CompanyName: { type: "string" },
            }
          }
        },
        transport: {
          read: function(options) {
            options.success(this.customers);
          }
        }
      }    
     };

我收到错误。 TypeError: Cannot read property 'length' of undefined

当我尝试调试kendo传输中的this.customers时,对象this.customers始终为空。

数据的格式如下所示:

[
    {
      "CustomerID": "ALFKI",
      "CompanyName": "Alfreds Futterkiste"
    },
    {
      "CustomerID": "ANATR",
      "CompanyName": "Ana Trujillo Emparedados y helados"
    }
]

Store.js

export default {
  state: {
    customers: JSON.parse(JSON.stringify(customers))
  },
  getters: {
    customers(state) {
      return state.customers;
    }
  }
};

当我尝试直接在options.success(this.customers);

上绑定数据时

与下面显示的方式一样,网格成功填充了数据。但是当我尝试使用getters进行绑定时,会出现错误。

TypeError: Cannot read property 'length' of undefined

  options.success([
        {
          "CustomerID": "ALFKI",
          "CompanyName": "Alfreds Futterkiste",
        },
        {
          "CustomerID": "ANATR",
          "CompanyName": "Ana Trujillo Emparedados y helados",
        }
    ]);

1 个答案:

答案 0 :(得分:1)

我认为你想使用计算属性而不是数据。

computed: {
    customers() {
      return this.$store.getters.customers;
    },
    kendoDataSource() {
      const customers = this.customers
      return {
        schema: {
          data: function(response) {
            return response;
          },
          model: {
            id: "CustomerID",
            fields: {
              CompanyName: { type: "string" },
            }
          }
        },
        transport: {
          read: function(options) {
            options.success(customers);
          }
        }
      }
    }
  }
}