subjectList.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <template>
  2. <el-dialog v-model="subjectShowDialog" title="科目列表" width="600px">
  3. <div class="subject_list">
  4. <div v-for="tag in subjectList" :key="tag.id" class="list_item" :class="tag.canDelete == 1 ? 'list_item can_delete' : 'list_item'">
  5. <span>{{ tag.courseName }}</span>
  6. <img src="@/assets/icon/delete.svg" v-if="tag.canDelete" @click="delSubject(tag.id)">
  7. </div>
  8. <el-input ref="InputRef" v-if="inputVisible" v-model="inputValue" @keyup.enter="handleInputConfirm" @blur="InputBlur" class="input_item">
  9. <template #append>
  10. <img src="@/assets/icon/confirm.svg" @click="handleInputConfirm">
  11. </template>
  12. </el-input>
  13. <div class="list_item cancel" v-if="inputVisible" @click="cancelAdd">
  14. <img src="@/assets/icon/cancel.svg">
  15. <span>取消</span>
  16. </div>
  17. <div v-else class="list_item add" @click="AddCourse">
  18. <img src="@/assets/icon/add.svg">
  19. <span>添加</span>
  20. </div>
  21. </div>
  22. </el-dialog>
  23. </template>
  24. <script lang="ts">
  25. export default {
  26. name: 'SubjectList'
  27. }
  28. </script>
  29. <script lang="ts" setup>
  30. import { ref, inject, nextTick, onMounted } from 'vue'
  31. import { ElMessage } from 'element-plus'
  32. import { getAllSubject, deleteSubject, addSubject } from '@/api/school'
  33. let subjectShowDialog = inject('subjectShowDialog')
  34. interface subjectList {
  35. id: number
  36. courseName: string
  37. canDelete?: number
  38. }
  39. const subjectList = ref<subjectList[]>([])
  40. const inputValue = ref('')
  41. const inputVisible = ref(false)
  42. onMounted(() => {
  43. GetAllSubject()
  44. })
  45. // 获取所有科目
  46. const GetAllSubject = () => {
  47. getAllSubject({}).then((res: any) => {
  48. if (res.code === 200 && res.data) {
  49. const { data } = res
  50. subjectList.value = data
  51. }
  52. })
  53. }
  54. const InputBlur = () => {
  55. if(!inputValue.value) {
  56. inputVisible.value = false
  57. inputValue.value = ''
  58. }
  59. }
  60. const handleInputConfirm = () => {
  61. if (inputValue.value) {
  62. const isDuplicate = subjectList.value.some(item => item.courseName === inputValue.value.trim())
  63. if (isDuplicate) {
  64. ElMessage.warning('当前科目已存在,请勿重复添加')
  65. return
  66. }
  67. addSubject({ courseName: inputValue.value }).then((res: any) => {
  68. if (res.code === 200) {
  69. ElMessage.success(res.msg)
  70. GetAllSubject()
  71. inputVisible.value = false
  72. inputValue.value = ''
  73. } else {
  74. ElMessage.error(res.msg)
  75. }
  76. })
  77. } else {
  78. inputVisible.value = false
  79. inputValue.value = ''
  80. }
  81. }
  82. const InputRef = ref<HTMLElement | null>(null)
  83. const AddCourse = () => {
  84. inputVisible.value = true;
  85. nextTick(() => {
  86. (InputRef.value as HTMLInputElement).focus()
  87. })
  88. }
  89. const cancelAdd = () => {
  90. inputVisible.value = false
  91. inputValue.value = ''
  92. }
  93. const delSubject = (id: number) => {
  94. deleteSubject({ ids: [id] }).then((res: any) => {
  95. if (res.code === 200) {
  96. ElMessage.success(res.msg)
  97. GetAllSubject()
  98. } else {
  99. ElMessage.error(res.msg)
  100. }
  101. })
  102. }
  103. </script>
  104. <style lang="scss" scoped>
  105. .el-dialog ::deep(.el-dialog__body ) {
  106. min-height: 300px !important;
  107. padding: 50px!important;
  108. }
  109. .subject_list {
  110. height: auto;
  111. display: flex;
  112. justify-content: flex-start;
  113. flex-wrap: wrap;
  114. gap:20px ;
  115. .list_item {
  116. border-radius: 4px 4px 4px 4px;
  117. border: 1px solid #2E64FA;
  118. width: auto;
  119. min-width: 72px;
  120. height: 36px;
  121. line-height: 36px;
  122. padding: 0 10px;
  123. font-weight: 400;
  124. font-size: 14px;
  125. color: #2E64FA;
  126. display: flex;
  127. justify-content: space-between;
  128. align-items: center;
  129. &.can_delete {
  130. background: rgba(46,100,250,0.1);
  131. }
  132. span {
  133. width: 100%;
  134. text-align: center;
  135. // margin-right: 8px;
  136. }
  137. img {
  138. margin-left: 8px;
  139. width: 16px;
  140. height: 16px;
  141. cursor: pointer;
  142. }
  143. img:hover {
  144. opacity: 0.8;
  145. }
  146. &.add,
  147. &.cancel {
  148. background: #fff;
  149. span {
  150. margin-right: 0;
  151. }
  152. img {
  153. margin-right: 5px;
  154. }
  155. cursor: pointer;
  156. }
  157. &.cancel {
  158. border-color: #BBBBBB;
  159. color: #BBBBBB;
  160. }
  161. }
  162. :deep(.input_item.el-input) {
  163. width: 120px;
  164. height: 36px;
  165. border-radius: 4px;
  166. border: 1px solid #2E64FA!important;
  167. .el-input__wrapper {
  168. height: 34px;
  169. box-shadow: none;
  170. padding: 1px 5px;
  171. }
  172. .el-input__inner {
  173. border: none; /* 去掉边框 */
  174. background-color: transparent; /* 去掉背景色 */
  175. box-shadow: none; /* 去掉阴影 */
  176. }
  177. .el-input-group__append {
  178. padding: 0 10px 0 0;
  179. background: #fff;
  180. box-shadow: none;
  181. cursor: pointer;
  182. }
  183. }
  184. }
  185. </style>