ChooseCourseVideoComponent.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <template>
  2. <div class="app-container">
  3. <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
  4. <el-form-item label="课程" prop="courseId">
  5. <el-select filterable v-model="queryParams.courseId" placeholder="请选择课程" clearable size="small" style="width: 100%" :value-key="'dictValue'">
  6. <el-option
  7. v-for="dict in courseList"
  8. :key="dict.dictValue"
  9. :label="dict.dictLabel"
  10. :value="parseInt(dict.dictValue)"
  11. />
  12. </el-select>
  13. </el-form-item>
  14. <el-form-item>
  15. <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
  16. <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
  17. </el-form-item>
  18. </el-form>
  19. <el-table border v-loading="loading" :data="list" @selection-change="handleSelectionChange" ref="videoTable" :row-key="'videoId'" :reserve-selection="true">
  20. <el-table-column type="selection" width="55" align="center" />
  21. <el-table-column label="课程名称" align="center" prop="courseName" />
  22. <el-table-column label="小节名称" align="center" prop="courseVideoName" />
  23. <el-table-column label="视频名称" align="center" prop="videoName" />
  24. <el-table-column label="时长" align="center" prop="duration"/>
  25. </el-table>
  26. <pagination
  27. v-show="total>0"
  28. :total="total"
  29. :page.sync="queryParams.pageNum"
  30. :limit.sync="queryParams.pageSize"
  31. @pagination="getList"
  32. />
  33. <div class="footer-button">
  34. <el-button @click="cancel">取消</el-button>
  35. <el-button type="primary" @click="submit">确定</el-button>
  36. </div>
  37. </div>
  38. </template>
  39. <script>
  40. import { courseList } from '@/api/course/courseRedPacketLog'
  41. import { getChooseCourseVideoList } from '@/api/course/userCourseVideo'
  42. export default {
  43. name: "ChooseCourseVideoComponent",
  44. props: {
  45. videoIds: {
  46. type: Array,
  47. default: () => []
  48. }
  49. },
  50. activated() {
  51. // keep-alive 激活时恢复选中
  52. this.$nextTick(() => {
  53. this.restoreSelection()
  54. })
  55. },
  56. watch: {
  57. // videoIds 变化时重新恢复选中
  58. videoIds: {
  59. immediate: false,
  60. handler() {
  61. this.$nextTick(() => this.restoreSelection())
  62. }
  63. }
  64. },
  65. data() {
  66. return {
  67. loading: false,
  68. showSearch: true,
  69. queryParams: {
  70. pageNum: 1,
  71. pageSize: 10,
  72. courseId: null
  73. },
  74. courseList: [],
  75. total: 0,
  76. list: [],
  77. selected: []
  78. }
  79. },
  80. created() {
  81. courseList().then(response => {
  82. this.courseList = response.list;
  83. });
  84. this.handleQuery()
  85. },
  86. methods: {
  87. handleQuery() {
  88. this.queryParams.pageNum = 1
  89. this.getList()
  90. },
  91. resetQuery() {
  92. this.queryParams = {
  93. pageNum: 1,
  94. pageSize: 10,
  95. courseId: null
  96. }
  97. this.handleQuery()
  98. },
  99. getList() {
  100. this.loading = true
  101. getChooseCourseVideoList(this.queryParams).then(response => {
  102. this.list = response.data.list
  103. this.total = response.data.total
  104. this.loading = false
  105. // 列表加载后,根据传入的 videoIds 自动选中
  106. this.$nextTick(() => this.restoreSelection())
  107. })
  108. },
  109. handleSelectionChange(selection) {
  110. this.selected = selection
  111. },
  112. cancel() {
  113. this.$emit('closeChoose')
  114. },
  115. submit() {
  116. this.$emit('selectConfirm', this.selected)
  117. },
  118. // 恢复勾选状态
  119. restoreSelection() {
  120. const tableRef = this.$refs.videoTable
  121. if (!tableRef || !Array.isArray(this.list) || !Array.isArray(this.videoIds)) return
  122. tableRef.clearSelection()
  123. if (this.videoIds.length === 0) return
  124. const idsSet = new Set(this.videoIds)
  125. this.list.forEach(row => {
  126. if (row && idsSet.has(row.videoId)) {
  127. tableRef.toggleRowSelection(row, true)
  128. }
  129. })
  130. }
  131. }
  132. }
  133. </script>
  134. <style scoped>
  135. .footer-button {
  136. display: flex;
  137. justify-content: flex-end;
  138. margin-top: 40px;
  139. }
  140. </style>