动态创建数据源(Kendo)中未调用更新

时间:2019-01-17 11:35:40

标签: vue.js

我有带有动态列和字段的剑道 vue 网格

一切正常,除了更新命令不起作用

我的代码:

 getSettings() {
        var vm = this;
         axios.get("http://localhost:54069/api/provider/GetUsers", { headers: { 'Authorization': 'Bearer ' + this.$store.state.authentication.user } })
          .then((response) => {
         try {
              vm.localDataSource = vm.getItems(response.data);
              vm.schemaModel = vm.generateModel(response.data);
              vm.columns = vm.generateColumns(response.data);
              var dataSource = vm.generateDataSource('/api/provider/', vm.schemaModel, vm.localDataSource);

              var grid = this.$refs.usersGrid.kendoWidget();
              //grid.columns = vm.columns;
              grid.dataSource = dataSource;
         }
         catch (e) {
              alert(e);
         }

          }, (error) => {
            this.loading = false;
          })
      },,
      parameterMap: function (options, operation) {
        alert('qqq');
        if (operation !== 'read' && options.models) {
          return { models: kendo.stringify(options.models) }
        }
      },
      generateDataSource(baseURL, model, response) {
        var vm = this;
        var dataSource = new kendo.data.DataSource({
          transport: {
            read: function (options) {
              options.success(response);
            },
            create: { url: '/api/provider/CreateProvider', beforeSend: vm.onBeforeSend, type: 'POST', dataType: 'json' }, 
            update: { url: '/api/provider/UpdateProvider', beforeSend: vm.onBeforeSend, type: 'PUT', dataType: 'json' },
              parameterMap: function(options, operation) {
              if (operation !== "read" && options.models) {
                return {models: kendo.stringify(options.models)};
              }
            }
          },
          batch: true,
          schema: {
            model: model,
            id: "Id"
          },
          pageSize: 10
        });

        return dataSource;
      },
      getItems(data) {
        return data.Data;
      },


      generateColumns(data) {
        var vm = this;
        var sampleDataItem = data.Data[0];
        var columnNames = Object.keys(sampleDataItem);

           return columnNames.map(function (name) {
          var isIdField = name=== "Id";
         // return { field: "Id", title: "Stat", width: "75px" };
          return {
            field: name,
            width: (isIdField ? 40 : 50),
            title: (isIdField ? "Id" : name),
            format: (vm.isDateField[name] ? "{0:dd-MM-yy}" : "")
          };
        });



      },
      generateModel(data) {
        //var dddd = JSON.parse(data.Data);
        var vm = this;
       // var len = data.Data.length;
        var sampleDataItem = data.Data[0];

        var model = {};
        var fields = {};

        for (var property in sampleDataItem) {


          if (property=== "Id") {
            model["Id"] = property;
          }
          var propType = typeof sampleDataItem[property];

          if (propType === "number") {
            fields[property] = {
              type: "number",
              validation: {
                required: true
              }
            };
            if (model.Id === property) {
              fields[property].editable = false;
              fields[property].validation.required = false;
            }
          } else if (propType === "boolean") {
            fields[property] = {
              type: "boolean"
            };
          } else if (propType === "string") {
            var parsedDate = kendo.parseDate(sampleDataItem[property]);

            if (parsedDate) {
              fields[property] = {
                type: "date",
                validation: {
                  required: true
                }
              };
              vm.isDateField[property] = true;
            } else {
              fields[property] = {
                validation: {
                  required: true
                }
              };
            }
          } else {
            fields[property] = {
              validation: {
                required: true
              }
            };
          }


        }

        model.fields = fields;

        return model;
      },

0 个答案:

没有答案