对象的使用处理,作用域的和ajax中this的理解

首先,封装类,理解清楚你需要用的哪几个变量,然后声明,然后在类里封装函数,其中,constructor就是存放初始变量的地方。

这里还是datatable的处理解决,

constructor(table) {
        this.data = {};
        this.table = table

    }
inittable(table) {
        for (var i = 0; i < this.data.rowdata.length; i++) {
            this.data.rowdata[i].submit_status = 0
        }
        this.data.col_define[this.data.col_define.length] = {
            'targets': this.data.col_define.length,
            'title': '上传状态',
            'data': 'submit_status',
            'render': function (data, type, full, meta) {
                if (1 === data) {
                    return '上传成功'
                } else if (2 === data) {
                    return '上传失败'
                } else {
                    return '未上传'
                }
            }
        };
        console.log(this.data.col_define);
        console.log(this.data.rowdata);
        this.table = $(table).DataTable({
            'language': lan,
            "dom": 'lB<"top"f><"toolbar">rt<"bottom"ip><"clear">',
            "paging": true,
            "lengthChange": true,
            "info": true,
            'destroy': true,
            "deferRender": true,
            'columns': this.data.col_define,
            'data': this.data.rowdata,
        })
    }

在功能里配置表格的配置,然后之前的res结果中的data改成this即可

 success: function (res) {
            if ('ok' === res['code']) {
                if (department === 1) {
                    salary_table = $('#primary_excel_table')
                } else if (department === 2) {
                    salary_table = $('#middle_excel_table')
                } else if (department === 3) {
                    salary_table = $('#logistics_excel_table')
                }
                alert('解析完成!');
                console.log('返回数据是', res['data']);
                sal.data = res['data'];
                console.log(sal.data);
                sal.inittable(modal_table);

这样子,就能利用类,来动态定义表格,而且submit里也直接配置了,不用再用之前博客里的render功能,

还有就是,字符串的拼接

comcat_string(salary_info) {
        var items = [];
        Object.keys(salary_info).forEach(function (key) {
            if (key !== '名字' && key !== '部门' && key !== '身份证' && key !== 'submit_status') {
                items.push([key, salary_info[key]].join(':'));
            }
        });
        return items.join('|')
    }

用到forEach方法,JS遍历可以查询的到方法类。

再者,就是ajax中,如果想要调用this下的变量,需要提前在外部声明

_this=this

然后类似

    _this.data.rowdata[a]['submit_status'] = 1;

自己看的,不喜勿喷,前端实习第11天,我也忘了具体,反正差不多是11把,头很痛。。。。啦啦啦啦啦德玛西亚

 

posted @ 2018-12-29 15:59  梦里一米七八  阅读(583)  评论(0编辑  收藏  举报