一、分页列表

1、定义api

创建文件 src/api/edu/teacher.js

  1. import request from '@/utils/request'
  2. const api_name = '/admin/edu/teacher'
  3. export default {
  4. getPageList(page, limit, searchObj) {
  5. return request({
  6. url: `${api_name}/${page}/${limit}`,
  7. method: 'get',
  8. data: searchObj
  9. })
  10. }
  11. }

2、初始化vue组件

src/views/edu/teacher/list.vue

  1. <template>
  2. <div class="app-container">
  3. 讲师列表
  4. </div>
  5. </template>
  6. <script>
  7. import teacher from '@/api/edu/teacher'
  8. export default {
  9. data() {// 定义数据
  10. return {}
  11. },
  12. created() { // 当页面加载时获取数据
  13. this.fetchData()
  14. },
  15. methods: {
  16. fetchData() { // 调用api层获取数据库中的数据
  17. console.log('加载列表')
  18. }
  19. }
  20. }
  21. </script>

3、定义data

  1. data() {
  2. return {
  3. listLoading: true, // 是否显示loading信息
  4. list: null, // 数据列表
  5. total: 0, // 总记录数
  6. page: 1, // 页码
  7. limit: 10, // 每页记录数
  8. searchObj: {}// 查询条件
  9. }
  10. },

4、定义methods

  1. methods: {
  2. fetchData(page = 1) { // 调用api层获取数据库中的数据
  3. console.log('加载列表')
  4. this.page = page
  5. this.listLoading = true
  6. teacher.getPageList(this.page, this.limit, this.searchObj).then(response => {
  7. // debugger 设置断点调试
  8. if (response.success === true) {
  9. this.list = response.data.rows
  10. this.total = response.data.total
  11. }
  12. this.listLoading = false
  13. })
  14. }
  15. }

5、表格渲染

  1. <!-- 表格 -->
  2. <el-table
  3. v-loading="listLoading"
  4. :data="list"
  5. element-loading-text="数据加载中"
  6. border
  7. fit
  8. highlight-current-row>
  9. <el-table-column
  10. label="序号"
  11. width="70"
  12. align="center">
  13. <template slot-scope="scope">
  14. {{ (page - 1) * limit + scope.$index + 1 }}
  15. </template>
  16. </el-table-column>
  17. <el-table-column prop="name" label="名称" width="80" />
  18. <el-table-column label="头衔" width="80">
  19. <template slot-scope="scope">
  20. {{ scope.row.level===1?'高级讲师':'首席讲师' }}
  21. </template>
  22. </el-table-column>
  23. <el-table-column prop="intro" label="资历" />
  24. <el-table-column prop="gmtCreate" label="添加时间" width="160"/>
  25. <el-table-column prop="sort" label="排序" width="60" />
  26. <el-table-column label="操作" width="200" align="center">
  27. <template slot-scope="scope">
  28. <router-link :to="'/edu/teacher/edit/'+scope.row.id">
  29. <el-button type="primary" size="mini" icon="el-icon-edit">修改</el-button>
  30. </router-link>
  31. <el-button type="danger" size="mini" icon="el-icon-delete" @click="removeDataById(scope.row.id)">删除</el-button>
  32. </template>
  33. </el-table-column>
  34. </el-table>

6、分页组件

  1. <!-- 分页 -->
  2. <el-pagination
  3. :current-page="page"
  4. :page-size="limit"
  5. :total="total"
  6. style="padding: 30px 0; text-align: center;"
  7. layout="total, prev, pager, next, jumper"
  8. @current-change="fetchData"
  9. />

7、顶部查询表单

注意:
element-ui的 date-picker组件默认绑定的时间值是默认世界标准时间,和中国时间差8小时
设置 value-format=”yyyy-MM-dd HH:mm:ss” 改变绑定的值

  1. <!--查询表单-->
  2. <el-form :inline="true" class="demo-form-inline">
  3. <el-form-item>
  4. <el-input v-model="searchObj.name" placeholder="讲师名"/>
  5. </el-form-item>
  6. <el-form-item>
  7. <el-select v-model="searchObj.level" clearable placeholder="讲师头衔">
  8. <el-option :value="1" label="高级讲师"/>
  9. <el-option :value="2" label="首席讲师"/>
  10. </el-select>
  11. </el-form-item>
  12. <el-form-item label="添加时间">
  13. <el-date-picker
  14. v-model="searchObj.begin"
  15. type="datetime"
  16. placeholder="选择开始时间"
  17. value-format="yyyy-MM-dd HH:mm:ss"
  18. default-time="00:00:00"
  19. />
  20. </el-form-item>
  21. <el-form-item>
  22. <el-date-picker
  23. v-model="searchObj.end"
  24. type="datetime"
  25. placeholder="选择截止时间"
  26. value-format="yyyy-MM-dd HH:mm:ss"
  27. default-time="00:00:00"
  28. />
  29. </el-form-item>
  30. <el-button type="primary" icon="el-icon-search" @click="fetchData()">查询</el-button>
  31. <el-button type="default" @click="resetData()">清空</el-button>
  32. </el-form>

清空方法

  1. resetData() {
  2. this.searchObj = {}
  3. this.fetchData()
  4. }

8、测试

二、删除

1、定义api

src/api/edu/teacher.js

  1. removeById(teacherId) {
  2. return request({
  3. url: `${api_name}/${teacherId}`,
  4. method: 'delete'
  5. })
  6. }

2、定义methods

src/views/edu/teacher/list.vue
使用MessageBox 弹框组件

  1. removeDataById(id) {
  2. // debugger
  3. // console.log(memberId)
  4. this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
  5. confirmButtonText: '确定',
  6. cancelButtonText: '取消',
  7. type: 'warning'
  8. }).then(() => {
  9. return teacher.removeById(id)
  10. }).then(() => {
  11. this.fetchData()
  12. this.$message({
  13. type: 'success',
  14. message: '删除成功!'
  15. })
  16. }).catch((response) => { // 失败
  17. if (response === 'cancel') {
  18. this.$message({
  19. type: 'info',
  20. message: '已取消删除'
  21. })
  22. } else {
  23. this.$message({
  24. type: 'error',
  25. message: '删除失败'
  26. })
  27. }
  28. })
  29. }