iview如何将生成的序号导出到csv中?

以下是解决方法,使用es6 js数组的map方法

data()中:

columns2: [{
                        type: 'selection',
                        width: 50,
                        align: 'center'
                    },
                    {
                        type: 'index',
                        key:'lineIndex',
                        title: "序号",
                        width: 80,
                        align: 'center',
                        indexMethod: (params) => {
                            return params._index + (this.pageCurrent - 1) * this.pageSize + 1;
                        }
                    },
                    {
                        title: "车辆识别号",
                        key: "id"
                    },
                    {
                        title: "号牌",
                        key: "noplate",
                        width: 80,
                        render:(h,params)=>{
                            return h('div',this.tableData[params.index].noplate.name)
                        }
                    },

methods中方法:

exportData(type) {
                if (this.tableData.length > 0) {
                    this.$refs.myTable.exportCsv({
                        filename: "车辆信息_" + getCurrDate(1),
                        original:false,
                        columns: this.columns2.filter((col, index) => { //导出数据过滤
                            if (index > 0 && index < this.columns2.length - 1) {
                                return true;
                            }
                        }),
                        //表格中列使用render转换后显示的数据,需要转换才能将name值导出到excel中
                        data: this.tableData.map((v,index)=>
                            ({...v,
                                lineIndex:index+1,
                                noplate:v.noplate.name
                            })
                        )
                    });
                } else {
                    this.$Message.error('表格数据不能为空!');
                }

发布了80 篇原创文章 · 获赞 33 · 访问量 47万+

猜你喜欢

转载自blog.csdn.net/tower888/article/details/89221152