Kendo datasource 本地Crud

https://docs.telerik.com/kendo-ui/framework/datasource/crud#setting-the-local-crud-operations

  

    var LocalDs = kendo.data.DataSource.extend({
        _currId: 1,
        _localArr: [],
        _getVistIndexById: function (id) {
            var l = this._localArr.length;

            for (var j = 0; j < l; j++) {
                if (this._localArr[j].id == id) {
                    return j;
                }
            }
            return null;
        },
        init: function (op) {
            var that = this;
            op.transport = {
                read: function (e) {
                    e.success(that._localArr);
                },
                create: function (e) {
                    e.data.id = that._currId++;
                    that._localArr.push(e.data);
                    e.success(e.data);
                },
                update: function (e) {
                    that._localArr[that._getVistIndexById(e.data.id)] = e.data;
                    e.success();
                },
                destroy: function (e) {
                    that._localArr.splice(that._getVistIndexById(e.data.id), 1);
                    e.success();
                }
            };
            op.error = function (e) {
                alert("保错,请看f12控制台");
                console.log(e);
            };
            op.batch = false;
            this._setLocalData(op.data);
            delete op.data;
            kendo.data.DataSource.fn.init.call(this, op);
            this.read();
        },
        _setLocalData: function (data) {
            //设置data和自增id
            if (data) {
                this._localArr = data;
                if (data.length) {
                    this._currId = data[data.length - 1].id + 1;
                     
                    if (isNaN(this._currId)) {
                        this._currId = 1;
                        var that = this;
                        that._localArr.forEach(function (n) {
                            n.id = that._currId++;
                        });
                    }
                } else {
                    this._currId = 1;
                }

            }
        },
        data: function (data) {
            this._setLocalData(data);
            return kendo.data.DataSource.fn.data.call(this, data);
        }
    });

 

posted on 2021-04-20 18:38  RedQ-Alright  阅读(42)  评论(0编辑  收藏  举报

导航