优学在线教育之课程分类功能模块
课程目标
1)课程分类Excel导入实现
2)课程分类列表展示
1、 分类上传前端实现
1. Excel模板
1.1. 编辑Excel模板
1.2. 将文件上传至阿里云OSS
2. 配置路由
2.1. 添加路由
位置:src/router/index.js
// 课程分类管理 { path: '/ebs/subject', component: Layout, redirect: '/ebs/subject/list', name: 'Subject', meta: { title: '课程分类管理', icon: 'nested' }, children: [ { path: 'list', name: 'EbsSubjectList', component: () => import('@/views/ebs/subject/list'), meta: { title: '课程分类列表' } }, { path: 'import', name: 'EbsSubjectImport', component: () => import('@/views/ebs/subject/import'), meta: { title: '导入课程分类' } } ] }, |
2.2. 添加vue组件
3. 表单组件import.vue
3.1. js定义数据
<script> export default { data() { return { BASE_API: process.env.BASE_API, // 接口API地址 OSS_PATH: process.env.OSS_PATH, // 阿里云OSS地址 fileUploadBtnText: '上传到服务器', // 按钮文字 importBtnDisabled: false, // 按钮是否禁用, loading: false } } } </script> |
3.2. template
<template> <div class="app-container"> <el-form label-width="120px"> <el-form-item label="信息描述"> <el-tag type="info">excel模版说明</el-tag> <el-tag> <i class="el-icon-download"/> <a :href="OSS_PATH + '/excel/%E8%AF%BE%E7%A8%8B%E5%88%86%E7%B1%BB%E5%88%97%E8%A1%A8%E6%A8%A1%E6%9D%BF.xls'">点击下载模版</a> </el-tag> </el-form-item> <el-form-item label="选择Excel"> <el-upload ref="upload" :auto-upload="false" :on-success="fileUploadSuccess" :on-error="fileUploadError" :disabled="importBtnDisabled" :limit="1" :action="BASE_API+'/admin/edu/subject/import'" name="file" accept="application/vnd.ms-excel"> <el-button slot="trigger" size="small" type="primary">选取文件</el-button> <el-button :loading="loading" style="margin-left: 10px;" size="small" type="success" @click="submitUpload">{{ fileUploadBtnText }}</el-button> </el-upload> </el-form-item> </el-form> </div> </template> |
3.3. js上传方法
methods: { submitUpload() { this.fileUploadBtnText = '正在上传' this.importBtnDisabled = true this.loading = true //图片开始上传 this.$refs.upload.submit() }, fileUploadSuccess(response) { }, fileUploadError(response) { } } |
3.4. 回调函数
fileUploadSuccess(response) { if (response.success === true) { this.fileUploadBtnText = '导入成功' this.loading = false this.$message({ type: 'success', message: response.message }) } else { this.fileUploadBtnText = '导入失败' this.loading = false const messages = response.data.messageList let msgString = '<ul>' messages.forEach(msg => { msgString += `<li>${msg}</li>` }) msgString += '</ul>' this.$alert(msgString, response.message, { dangerouslyUseHTMLString: true }) } }, fileUploadError(response) { this.fileUploadBtnText = '导入失败' this.loading = false this.$message({ type: 'error', message: '导入失败' }) } |
2、 分类管理接口
1. 依赖
yxzx-ebs中添加依赖
1.1. parent锁定依赖版本
<poi.version>3.9</poi.version> <commons-fileupload.version>1.3.1</commons-fileupload.version> <!--xls--> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>${poi.version}</version> </dependency> <!--xlsx--> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>${poi.version}</version> </dependency> <!--文件上传--> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>${commons-fileupload.version}</version> </dependency> |
1.2. yxzx-common配置依赖
子项目可选<optional>true</optional>
<!--xls--> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <optional>true</optional> </dependency> <!--xlsx--> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <optional>true</optional> </dependency> |
1.3. yxzx-ebs配置依赖
<!--xls--> |
2. 业务处理
成功导入:
问题导入:
2.1. EbsSubjectController
package com.yxzx.ebs.controller; |
2.2. EbsSubjectService
接口
List<String> batchImport(MultipartFile file); |
实现V1:获取Excel记录并逐条导入
/** |
实现Final
service层辅助方法
//判断数据库表是否存在二级分类
|
// 判断数据库表是否存在一级分类
private EbsSubject existOneSubject(String name) { |
3、 分类列表展示
1. 前端实现
1.1. 参考 views/tree/index.vue
1.2. 创建api
api/ebs/subject.js
import request from '@/utils/request' const api_name = '/ebs/subject' export default { getNestedTreeList() { return request({ url: `${api_name}`, method: 'get' }) } } |
1.3. list.vue
<template> <div class="app-container"> <el-input v-model="filterText" placeholder="Filter keyword" style="margin-bottom:30px;" /> <el-tree ref="subjectTree" :data="subjectList" :props="defaultProps" :filter-node-method="filterNode" class="filter-tree" default-expand-all /> </div> </template> <script> import subject from '@/api/ebs/subject' export default { data() { return { filterText: '', subjectList: [], defaultProps: { children: 'children', label: 'title' } } }, watch: { filterText(val) { this.$refs.subjectTree.filter(val) } }, created() { this.fetchNodeList() }, methods: { fetchNodeList() { subject.getNestedTreeList().then(response => { if (response.success === true) { this.subjectList = response.data.items } }) }, filterNode(value, data) { if (!value) return true return data.title.indexOf(value) !== -1 } } } </script> |
2. 后端实现
后端接口数据:
2.1. 创建vo
package com.yxzx.ebs.entity.vo;
@Data |
@Data |
2.2. 创建controller
@ApiOperation(value = "嵌套数据列表") |
2.3. 创建service
接口 EbsSubjectService
/** |
实现v1:获取一级分类列表
/** |
实现v2:完善二级分类记录获取和填充
|
3. 优化前端过滤功能
filterNode(value, data) { if (!value) return true return data.title.toLowerCase().indexOf(value.toLowerCase()) !== -1 } |
filter-node-method |
对树节点进行筛选时执行的方法,返回 true 表示这个节点可以显示,返回 false 则表示这个节点会被隐藏 |
Function(value, data, node) |
搜索实现步骤:
第一步:定义组件
<el-input placeholder="输入关键字进行过滤" v-model="filterText">
data() {
return {
filterText: ''//记录搜索关键字
}
第二步:设置监听
watch: { filterText(val) { this.$refs.tree.filter(val); } },
第三步:实现过滤函数
filterNode(value, data) { if (!value) return true; return data.label.indexOf(value) !== -1; }
- 点赞
- 收藏
- 关注作者
评论(0)