提交 b132eed5 authored 作者: pengxiaohui's avatar pengxiaohui

增加表格选中项导出excel功能

上级 62b5eb84
...@@ -4,14 +4,14 @@ ...@@ -4,14 +4,14 @@
<template #header-aside> <template #header-aside>
<el-button type="primary" size="mini" @click="drawerVisible = true" v-if="hasCreate">新增</el-button> <el-button type="primary" size="mini" @click="drawerVisible = true" v-if="hasCreate">新增</el-button>
<el-button type="primary" size="mini" @click="dialogVisible = true" v-if="hasImport">导入</el-button> <el-button type="primary" size="mini" @click="dialogVisible = true" v-if="hasImport">导入</el-button>
<el-dropdown size="small" @command="handleCommand" v-if="hasExport"> <!-- <el-dropdown size="small" @command="handleCommand" v-if="hasExport">
<el-button type="primary" size="mini" > <el-button type="primary" size="mini" >
导出<i class="el-icon-arrow-down el-icon--right"></i> 导出<i class="el-icon-arrow-down el-icon--right"></i>
</el-button> </el-button>
<el-dropdown-menu slot="dropdown"> <el-dropdown-menu slot="dropdown">
<el-dropdown-item command="all">导出全部</el-dropdown-item> <el-dropdown-item command="all">导出全部</el-dropdown-item>
</el-dropdown-menu> </el-dropdown-menu>
</el-dropdown> </el-dropdown> -->
</template> </template>
<!-- 课程Id --> <!-- 课程Id -->
<template v-slot:table_id="scope"> <template v-slot:table_id="scope">
...@@ -39,6 +39,15 @@ ...@@ -39,6 +39,15 @@
<div> <div>
<span v-if="hasDelete">已选中 {{multipleSelection.length}}</span> <span v-if="hasDelete">已选中 {{multipleSelection.length}}</span>
<el-button style="margin-left:15px;" size="mini" :disabled="!multipleSelection.length" @click="handleRemove" v-if="hasDelete">删除</el-button> <el-button style="margin-left:15px;" size="mini" :disabled="!multipleSelection.length" @click="handleRemove" v-if="hasDelete">删除</el-button>
<el-dropdown size="small" @command="handleCommand" v-if="hasExport">
<el-button type="primary" size="mini">
导出<i class="el-icon-arrow-down el-icon--right"></i>
</el-button>
<el-dropdown-menu slot="dropdown">
<el-dropdown-item command="all">导出全部</el-dropdown-item>
<el-dropdown-item command="selected" :disabled="!multipleSelection.length">导出选中项</el-dropdown-item>
</el-dropdown-menu>
</el-dropdown>
</div> </div>
</template> </template>
</table-list> </table-list>
...@@ -91,6 +100,7 @@ import TableList from '@/components/TableList' ...@@ -91,6 +100,7 @@ import TableList from '@/components/TableList'
import { getCourseList, createCourse, updateCourse, deleteCourse, importCourse, exportCourseList } from '@/api/course' import { getCourseList, createCourse, updateCourse, deleteCourse, importCourse, exportCourseList } from '@/api/course'
import { splitStrLast, funDownload } from '@/utils/utils' import { splitStrLast, funDownload } from '@/utils/utils'
import _ from 'lodash' import _ from 'lodash'
import XLSX from 'xlsx'
const defaultForm = { const defaultForm = {
chinese_name: '', chinese_name: '',
english_name: '', english_name: '',
...@@ -150,14 +160,13 @@ export default { ...@@ -150,14 +160,13 @@ export default {
{ prop: 'chinese_name', label: '课程名称', slots: 'table_name', minWidth: '130px' }, { prop: 'chinese_name', label: '课程名称', slots: 'table_name', minWidth: '130px' },
{ prop: 'english_name', label: '课程名称(英)', slots: 'table_en_name', minWidth: '160px' }, { prop: 'english_name', label: '课程名称(英)', slots: 'table_en_name', minWidth: '160px' },
{ prop: 'pubdate', label: '发布时间', minWidth: '120px' } { prop: 'pubdate', label: '发布时间', minWidth: '120px' }
], ]
bodyHeight: 'calc(100% - 50px)'
} }
} }
}, },
methods: { methods: {
handleSelectionChange(val) { handleSelectionChange(val) {
this.multipleSelection = val.map(item => item.id) this.multipleSelection = val
}, },
handleImport() {}, handleImport() {},
handleRemove() { handleRemove() {
...@@ -219,6 +228,37 @@ export default { ...@@ -219,6 +228,37 @@ export default {
}, },
handleCommand(type) { handleCommand(type) {
if (type === 'all') this.fetchExportAll() if (type === 'all') this.fetchExportAll()
if (type === 'selected') this.exportSelected()
},
exportSelected() {
const list = this.tableOptions.columns.filter(item => {
return item.prop
})
const headList = list.map(item => item.label)
const propList = list.map(item => item.prop)
const excelList = []
excelList.push(headList)
this.multipleSelection.forEach(item => {
const rowValArr = []
propList.forEach(key => {
rowValArr.push(item[key])
})
excelList.push(rowValArr)
})
const ws = XLSX.utils.aoa_to_sheet(excelList)
ws['!cols'] = [
{ wpx: 160 },
{ wpx: 180 },
{ wpx: 200 },
{ wpx: 160 }
]
const wb = XLSX.utils.book_new()
wb.SheetNames.push('Worksheet')
wb.Sheets.Worksheet = ws
const wopts = { bookType: 'xlsx', bookSST: false, type: 'array' }
const wbout = XLSX.write(wb, wopts)
const url = URL.createObjectURL(new window.Blob([wbout], { type: 'application/octet-stream' }))
funDownload(url, `课程信息表_${Date.now()}.xlsx`)
}, },
fetchExportAll() { fetchExportAll() {
exportCourseList(this.tableOptions.remote.params).then(res => { exportCourseList(this.tableOptions.remote.params).then(res => {
...@@ -254,7 +294,7 @@ export default { ...@@ -254,7 +294,7 @@ export default {
}) })
}, },
fetchDeleteCourse() { fetchDeleteCourse() {
const ids = this.multipleSelection.join() const ids = this.multipleSelection.map(item => item.id).join()
deleteCourse(ids).then(res => { deleteCourse(ids).then(res => {
const msg = '删除课程' const msg = '删除课程'
if (res.code === 0 && res.message === 'OK') { if (res.code === 0 && res.message === 'OK') {
......
...@@ -3,14 +3,14 @@ ...@@ -3,14 +3,14 @@
<table-list v-bind="tableOptions" ref="tabList" @selection-change="handleSelectionChange"> <table-list v-bind="tableOptions" ref="tabList" @selection-change="handleSelectionChange">
<template #header-aside> <template #header-aside>
<el-button type="primary" size="mini" @click="handleCreate" v-if="hasCreate">新增</el-button> <el-button type="primary" size="mini" @click="handleCreate" v-if="hasCreate">新增</el-button>
<el-dropdown size="small" @command="handleExportCommand" v-if="hasExport"> <!-- <el-dropdown size="small" @command="handleExportCommand" v-if="hasExport">
<el-button type="primary" size="mini"> <el-button type="primary" size="mini">
导出<i class="el-icon-arrow-down el-icon--right"></i> 导出<i class="el-icon-arrow-down el-icon--right"></i>
</el-button> </el-button>
<el-dropdown-menu slot="dropdown"> <el-dropdown-menu slot="dropdown">
<el-dropdown-item command="all">导出全部</el-dropdown-item> <el-dropdown-item command="all">导出全部</el-dropdown-item>
</el-dropdown-menu> </el-dropdown-menu>
</el-dropdown> </el-dropdown> -->
</template> </template>
<!-- 中文名 --> <!-- 中文名 -->
<template v-slot:table_name="scope"> <template v-slot:table_name="scope">
...@@ -47,6 +47,15 @@ ...@@ -47,6 +47,15 @@
<div> <div>
已选中 {{multipleSelection.length}} 已选中 {{multipleSelection.length}}
<el-button style="margin-left:15px;" size="mini" :disabled="!multipleSelection.length" @click="handleRemove" v-if="hasDelete">删除</el-button> <el-button style="margin-left:15px;" size="mini" :disabled="!multipleSelection.length" @click="handleRemove" v-if="hasDelete">删除</el-button>
<el-dropdown size="small" @command="handleExportCommand" v-if="hasExport">
<el-button type="primary" size="mini">
导出<i class="el-icon-arrow-down el-icon--right"></i>
</el-button>
<el-dropdown-menu slot="dropdown">
<el-dropdown-item command="all">导出全部</el-dropdown-item>
<el-dropdown-item command="selected" :disabled="!multipleSelection.length">导出选中项</el-dropdown-item>
</el-dropdown-menu>
</el-dropdown>
</div> </div>
</template> </template>
</table-list> </table-list>
...@@ -90,6 +99,7 @@ import TabStudent from './components/student.vue' ...@@ -90,6 +99,7 @@ import TabStudent from './components/student.vue'
import TabCourse from './components/course.vue' import TabCourse from './components/course.vue'
import InfoForm from './components/InfoForm' import InfoForm from './components/InfoForm'
import { getProjectList, createProject, deleteProject, exportProjectList } from '@api/project' import { getProjectList, createProject, deleteProject, exportProjectList } from '@api/project'
import XLSX from 'xlsx'
import { funDownload } from '@/utils/utils' import { funDownload } from '@/utils/utils'
const drawerTitleMap = { const drawerTitleMap = {
project: '新增项目', project: '新增项目',
...@@ -176,14 +186,14 @@ export default { ...@@ -176,14 +186,14 @@ export default {
return row.students.length return row.students.length
} }
}, },
{ prop: 'operate', label: '操作', slots: 'table-operate', minWidth: '120px', fixed: 'right' } { label: '操作', slots: 'table-operate', minWidth: '120px', fixed: 'right' }
] ]
} }
} }
}, },
methods: { methods: {
handleSelectionChange(val) { handleSelectionChange(val) {
this.multipleSelection = val.map(item => item.id) this.multipleSelection = val
}, },
handleCreate() { handleCreate() {
this.createDrawerVisible = true this.createDrawerVisible = true
...@@ -229,6 +239,46 @@ export default { ...@@ -229,6 +239,46 @@ export default {
}, },
handleExportCommand(type) { handleExportCommand(type) {
if (type === 'all') this.fetchExportAll() if (type === 'all') this.fetchExportAll()
if (type === 'selected') this.exportSelected()
},
exportSelected() {
const list = this.tableOptions.columns.filter(item => {
return item.prop && item.prop !== 'head_img'
})
const headList = list.map(item => item.label)
const propList = list.map(item => item.prop)
const excelList = []
excelList.push(headList)
this.multipleSelection.forEach(item => {
const rowValArr = []
propList.forEach(key => {
let val = item[key]
if (key === 'type') val = val === 1 ? '长期' : '短期'
if (['courses', 'students'].includes(key)) val = val.length
rowValArr.push(val)
})
excelList.push(rowValArr)
})
console.log(XLSX)
const ws = XLSX.utils.aoa_to_sheet(excelList)
ws['!cols'] = [
{ wpx: 180 },
{ wpx: 160 },
{ wpx: 180 },
{ wpx: 80 },
{ wpx: 160 },
{ wpx: 100 },
{ wpx: 160 },
{ wpx: 80 },
{ wpx: 80 }
]
const wb = XLSX.utils.book_new()
wb.SheetNames.push('Worksheet')
wb.Sheets.Worksheet = ws
const wopts = { bookType: 'xlsx', bookSST: false, type: 'array' }
const wbout = XLSX.write(wb, wopts)
const url = URL.createObjectURL(new window.Blob([wbout], { type: 'application/octet-stream' }))
funDownload(url, `项目信息表_${Date.now()}.xlsx`)
}, },
fetchExportAll() { fetchExportAll() {
exportProjectList(this.tableOptions.remote.params).then(res => { exportProjectList(this.tableOptions.remote.params).then(res => {
...@@ -251,7 +301,7 @@ export default { ...@@ -251,7 +301,7 @@ export default {
}) })
}, },
fetchDeleteProject() { fetchDeleteProject() {
const ids = this.multipleSelection.join() const ids = this.multipleSelection.map(item => item.id).join()
deleteProject(ids).then(res => { deleteProject(ids).then(res => {
const msg = '删除项目' const msg = '删除项目'
if (res.code === 0 && res.message === 'OK') { if (res.code === 0 && res.message === 'OK') {
......
...@@ -3,14 +3,6 @@ ...@@ -3,14 +3,6 @@
<table-list v-bind="tableOptions" ref="tabList" @selection-change="handleSelectionChange"> <table-list v-bind="tableOptions" ref="tabList" @selection-change="handleSelectionChange">
<template #header-aside> <template #header-aside>
<el-button type="primary" size="mini" @click="dialogVisible = true" v-if="hasImport">导入</el-button> <el-button type="primary" size="mini" @click="dialogVisible = true" v-if="hasImport">导入</el-button>
<el-dropdown size="small" @command="handleCommand" v-if="hasExport">
<el-button type="primary" size="mini">
导出<i class="el-icon-arrow-down el-icon--right"></i>
</el-button>
<el-dropdown-menu slot="dropdown">
<el-dropdown-item command="all">导出全部</el-dropdown-item>
</el-dropdown-menu>
</el-dropdown>
</template> </template>
<!-- 图片 --> <!-- 图片 -->
<template v-slot:table_img="{ row }"> <template v-slot:table_img="{ row }">
...@@ -55,6 +47,15 @@ ...@@ -55,6 +47,15 @@
<div> <div>
已选中 {{multipleSelection.length}} 已选中 {{multipleSelection.length}}
<el-button style="margin-left:15px;" size="mini" :disabled="!multipleSelection.length" @click="handleRemove" v-if="hasDelete">删除</el-button> <el-button style="margin-left:15px;" size="mini" :disabled="!multipleSelection.length" @click="handleRemove" v-if="hasDelete">删除</el-button>
<el-dropdown size="small" @command="handleCommand" v-if="hasExport">
<el-button type="primary" size="mini">
导出<i class="el-icon-arrow-down el-icon--right"></i>
</el-button>
<el-dropdown-menu slot="dropdown">
<el-dropdown-item command="all">导出全部</el-dropdown-item>
<el-dropdown-item command="selected" :disabled="!multipleSelection.length">导出选中项</el-dropdown-item>
</el-dropdown-menu>
</el-dropdown>
</div> </div>
</template> </template>
</table-list> </table-list>
...@@ -116,12 +117,9 @@ import TabDegree from './components/degree.vue' ...@@ -116,12 +117,9 @@ import TabDegree from './components/degree.vue'
import TabCourse from './components/course.vue' import TabCourse from './components/course.vue'
import { getStudentList, deleteStudent, importStudents, exportStudentList } from '@/api/student' import { getStudentList, deleteStudent, importStudents, exportStudentList } from '@/api/student'
import _ from 'lodash' import _ from 'lodash'
import XLSX from 'xlsx'
import { splitStrLast, funDownload } from '@/utils/utils' import { splitStrLast, funDownload } from '@/utils/utils'
const genderMap = { const genderMap = { 0: '女', 1: '男', 2: '未知' }
0: '女',
1: '男',
2: '未知'
}
export default { export default {
components: { TableList, TabInfo, TabDegree, TabCourse }, components: { TableList, TabInfo, TabDegree, TabCourse },
data() { data() {
...@@ -237,7 +235,7 @@ export default { ...@@ -237,7 +235,7 @@ export default {
}, },
methods: { methods: {
handleSelectionChange(val) { handleSelectionChange(val) {
this.multipleSelection = val.map(item => item.id) this.multipleSelection = val
}, },
handleRemove() { handleRemove() {
this.$confirm('确定删除选中项?', '提示', { this.$confirm('确定删除选中项?', '提示', {
...@@ -300,10 +298,54 @@ export default { ...@@ -300,10 +298,54 @@ export default {
}, },
handleCommand(type) { handleCommand(type) {
if (type === 'all') this.fetchExportAll() if (type === 'all') this.fetchExportAll()
if (type === 'selected') this.exportSelected()
},
exportSelected() {
const list = this.tableOptions.columns.filter(item => {
return item.prop && item.prop !== 'head_img'
})
const headList = list.map(item => item.label)
const propList = list.map(item => item.prop)
const excelList = []
excelList.push(headList)
this.multipleSelection.forEach(item => {
const rowValArr = []
propList.forEach(key => {
let val = item[key]
if (key === 'gender') val = genderMap[val]
if (key === 'is_student') val = val === 1 ? '有' : '无'
rowValArr.push(val)
})
excelList.push(rowValArr)
})
const ws = XLSX.utils.aoa_to_sheet(excelList)
ws['!cols'] = [
{ wpx: 160 },
{ wpx: 120 },
{ wpx: 120 },
{ wpx: 120 },
{ wpx: 140 },
{ wpx: 80 },
{ wpx: 80 },
{ wpx: 120 },
{ wpx: 120 },
{ wpx: 120 },
{ wpx: 100 },
{ wpx: 120 },
{ wpx: 160 },
{ wpx: 120 },
{ wpx: 160 }
]
const wb = XLSX.utils.book_new()
wb.SheetNames.push('Worksheet')
wb.Sheets.Worksheet = ws
const wopts = { bookType: 'xlsx', bookSST: false, type: 'array' }
const wbout = XLSX.write(wb, wopts)
const url = URL.createObjectURL(new window.Blob([wbout], { type: 'application/octet-stream' }))
funDownload(url, `学生信息表_${Date.now()}.xlsx`)
}, },
fetchExportAll() { fetchExportAll() {
exportStudentList(this.tableOptions.remote.params).then(res => { exportStudentList(this.tableOptions.remote.params).then(res => {
console.log(res)
if (res && res.type === 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet') { if (res && res.type === 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet') {
const url = URL.createObjectURL(res) const url = URL.createObjectURL(res)
funDownload(url, `学生信息表_${Date.now()}.xlsx`) funDownload(url, `学生信息表_${Date.now()}.xlsx`)
...@@ -311,7 +353,7 @@ export default { ...@@ -311,7 +353,7 @@ export default {
}) })
}, },
fetchDeleteStudents() { fetchDeleteStudents() {
const ids = this.multipleSelection.join() const ids = this.multipleSelection.map(item => item.id).join()
deleteStudent(ids).then(res => { deleteStudent(ids).then(res => {
if (res.code === 0 && res.message === 'OK') { if (res.code === 0 && res.message === 'OK') {
this.$message.success('删除学员成功') this.$message.success('删除学员成功')
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论