Vuex How to bind store to KendoUi-Vue Grid in transport read

╄→尐↘猪︶ㄣ 提交于 2019-12-11 23:18:58

问题


I am trying to bind the data on Kendo UI with the data from Vuex getters.

I have tried the following but no luck. Please help whether where i am missing something on the vuex or on the kendo.

Kendo Extensions:

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

Components:

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

I am getting the error. TypeError: Cannot read property 'length' of undefined

When i tried to debug the this.customers in the transport of kendo the object this.customers is always null.

The data are in the format as shown in below:

[
    {
      "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;
    }
  }
};

When i try to bind the data directly on options.success(this.customers);

Like the way shown below the grid get populated with the data successfully. But while i tried to bind using the getters there is the error.

TypeError: Cannot read property 'length' of undefined

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

回答1:


I think you want to use computed properties instead of data.

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


来源:https://stackoverflow.com/questions/50454989/vuex-how-to-bind-store-to-kendoui-vue-grid-in-transport-read

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!