indexsss.vue 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. <template>
  2. <div class="h-full overflow-hidden">
  3. <n-card title="课程分类" :bordered="false" class="rounded-16px shadow-sm">
  4. <n-space class="pb-14px" justify="space-between">
  5. <!-- 加入查询组件 -->
  6. <n-space>
  7. <n-button type="primary" @click="addTableData">
  8. <icon-ic-round-plus class="mr-4px text-20px" />
  9. 新增
  10. </n-button>
  11. <n-button type="error" @click="deleteTableData" @update:checked-row-keys="handleCheck">
  12. <icon-ic-round-delete class="mr-4px text-20px" />
  13. 删除
  14. </n-button>
  15. <!-- 添加查询按钮 -->
  16. <n-button type="primary" @click="handleOpenSearch">
  17. <icon-simple-line-icons:magnifier class="mr-4px text-16px" />
  18. 查询
  19. </n-button>
  20. <div class="mr-5px"></div>
  21. <n-input-group>
  22. <n-input :style="{ width: '50%',marginRight:'2%'}" :value="conditionParams.id"
  23. @input="event => conditionParams.id = event" placeholder="请输入ID..." />
  24. <n-input :style="{ width: '50%',marginRight:'2%'}" :value="conditionParams.name"
  25. @input="event => conditionParams.name = event" placeholder="请输入学科名称..." />
  26. <n-input :style="{ width: '50%',marginRight:'2%' }" :value="conditionParams.description"
  27. @input="event => conditionParams.description = event" placeholder="请输入学科描述..." />
  28. <n-input :style="{ width: '50%',marginRight:'2%' }" :value="conditionParams.createUid"
  29. @input="event => conditionParams.createUid = event" placeholder="请输入创建用户ID..." />
  30. <n-input :style="{ width: '50%',marginRight:'2%' }" :value="conditionParams.createTime"
  31. @input="event => conditionParams.createTime = event" placeholder="请输入创建时间..." />
  32. <n-input :style="{ width: '50%',marginRight:'2%' }" :value="conditionParams.modifyTime"
  33. @input="event => conditionParams.modifyTime = event" placeholder="请输入修改时间..." />
  34. <n-select :style="{ width: '50%' }" :value="conditionParams.disabled"
  35. @input="event => conditionParams.disabled = event" placeholder="输入查询状态..." />
  36. <n-button type="primary" ghost @click="searchCondition()">
  37. 搜索
  38. </n-button>
  39. </n-input-group>
  40. </n-space>
  41. <n-space align="center" :size="18">
  42. <n-button size="small" type="primary" @click="searchCondition">
  43. <icon-mdi-refresh class="mr-4px text-16px" :class="{ 'animate-spin': loading }" />
  44. 刷新表格
  45. </n-button>
  46. </n-space>
  47. </n-space>
  48. <n-scrollbar>
  49. <!-- <n-data-table :columns="columns" :data="searchForm" :loading="loading"
  50. :pagination="pagination" :row-key="rowKey" @update:checked-row-keys="handleCheck" /> -->
  51. <n-data-table :columns="columns" :data="tableData" :loading="loading"
  52. :pagination="pagination" :row-key="rowKey" />
  53. <table-action-add v-model:visible="visible" :type="modalType" :edit-data="(editData as SelectByCondition_1Params)" @update:checked-row-keys="handleCheck"/>
  54. <div style="width: 100%; height: 300px;"></div>
  55. </n-scrollbar>
  56. </n-card>
  57. </div>
  58. </template>
  59. <script setup lang="tsx" >
  60. import { ref, Ref, reactive } from 'vue'
  61. import { NButton, NSpace, NTag, NPopconfirm } from 'naive-ui';
  62. import type { DataTableColumns, PaginationProps } from 'naive-ui';
  63. import { useBoolean, useLoading } from '@/hooks';
  64. import type { DataTableRowKey } from 'naive-ui'
  65. import TableActionAdd from './components/table-action-add.vue'
  66. import type { ModalType } from './components/table-action-add.vue';
  67. import { userStatusLabels } from '@/constants';
  68. import { deleteById } from '~/src/service/api/sort';
  69. import { selectByCondition_1 } from '~/src/service/api/sort';
  70. import type { SelectByCondition_1Params } from '~/src/service/api/sort';
  71. type RowData = {
  72. key: number
  73. id: number
  74. name: string
  75. description: string
  76. createTime: string
  77. modifyTime: string
  78. createUid: number
  79. disabled: string
  80. }
  81. const conditionParams: SelectByCondition_1Params = reactive({
  82. id: null,
  83. name: null,
  84. description: null,
  85. createUid:null,
  86. createTime: null,
  87. modifyTime: null,
  88. disabled: null
  89. });
  90. const { loading, startLoading, endLoading } = useLoading(false);
  91. const { bool: visible, setTrue: openModal } = useBoolean();
  92. const tableData = ref<any[]>([]);
  93. const pagination: PaginationProps = ref({
  94. page: 1,
  95. pageSize: 10,
  96. showSizePicker: true,
  97. pageCount: 12,
  98. pageSizes: [10, 20, 30,40,50],
  99. onChange: (page: number) => {
  100. // 处理页码变化
  101. pagination.page = page;
  102. },
  103. onUpdatePageSize: (pageSize: number) => {
  104. // 处理每页显示数量变化
  105. pagination.pageSize = pageSize;
  106. pagination.page = 1;
  107. }
  108. }).value;
  109. // function searchCondition() {
  110. // const pageNum = searchpagedata.value as number;
  111. // const pageSize = searchpagesize.value as number;
  112. // const params: SelectByCondition_1Params = {};
  113. // if (pageNum && pageSize) {
  114. // selectByCondition_1(pageNum, pageSize, params).then(res => {
  115. // searchConData.value = res.data as [];
  116. // pagination.page = searchpagedata.value
  117. // pagination.pageSize = searchpagesize.value
  118. // window.$message?.success('查询成功');
  119. // })
  120. // }
  121. // else if (!pageNum && !pageSize) {
  122. // searchpagedata.value = 0;
  123. // searchpagesize.value = 0;
  124. // window.$message?.warning('请输入页码和条数进行查询');
  125. // }
  126. // }
  127. // function getTableData() {
  128. // startLoading();
  129. // selectAll_1().then(res => {
  130. // setTimeout(() => {
  131. // tableData.value = res.data as [];
  132. // endLoading();
  133. // }, 1000);
  134. // });
  135. // }
  136. function init() {
  137. searchCondition();
  138. }
  139. // 初始化
  140. init();
  141. // function searchConClear() {
  142. // searchConData.value.length = 0
  143. // searchpagedata.value = 0;
  144. // searchpagesize.value = 0;
  145. // getTableData();
  146. // }
  147. const showSearch = ref(false)
  148. function handleOpenSearch() {
  149. // 点击查询时显示搜索框
  150. showSearch.value = !showSearch.value;
  151. }
  152. // 输入数据
  153. // function handleChange(v: string) {
  154. // searchId.value = Number(v)
  155. // }
  156. // 根据id查询数据
  157. // async function handleSearch() {
  158. // if (searchId.value) {
  159. // await selectById_1(searchId.value)
  160. // .then(res => {
  161. // // 更新数据
  162. // searchInput.value = res.data as [];
  163. // const filteredItems = tableData.value.filter(item => item.id === searchInput.value.id)
  164. // searchForm.value = filteredItems;
  165. // window.$message?.success('查询成功');
  166. // })
  167. // }
  168. // else {
  169. // searchForm.value.length = 0
  170. // window.$message?.warning('请输入有效的ID进行查询');
  171. // }
  172. // }
  173. // function searchClear() {
  174. // searchForm.value.length = 0
  175. // getTableData();
  176. // }
  177. const checkedRowKeysRef = ref<DataTableRowKey[]>([])
  178. const s = ref<DataTableRowKey[]>([])
  179. // 根据id进行删除
  180. async function deleteTableData() {
  181. const ids = checkedRowKeysRef.value.filter((item) => !s.value?.includes(item));
  182. s.value = checkedRowKeysRef.value
  183. if (ids.length !== 0) {
  184. console.log(ids);
  185. for (const id of ids) {
  186. // 调用删除接口
  187. await deleteById(id as number)
  188. .then((res) => {
  189. window.$message?.success('删除成功!');
  190. })
  191. }
  192. searchCondition(); // 删除完成后刷新表格数据
  193. } else {
  194. // 没有选择任何行时的操作
  195. window.$message?.error('未选择要删除的行');
  196. }
  197. }
  198. const modalType = ref<ModalType>('add' || 'edit');
  199. function setModalType(type: ModalType) {
  200. modalType.value = type;
  201. }
  202. // 添加
  203. function addTableData() {
  204. openModal();
  205. setModalType('add');
  206. }
  207. const editData = ref<SelectByCondition_1Params | null>(null);
  208. function setEditData(data:SelectByCondition_1Params | null) {
  209. editData.value = data;
  210. }
  211. function handleEditTable(findItem: SelectByCondition_1Params) {
  212. // findItem = tableData.value.find(item => item.id === rowId);
  213. setModalType('edit');
  214. openModal();
  215. if (findItem) {
  216. setEditData(findItem);
  217. }
  218. searchCondition(); // 编辑完成后刷新表格数据
  219. }
  220. function handleDeleteTable(rowId: string) {
  221. // const findItem = tableData.value.find(item => item.id === rowId);
  222. const rowIdNumber = Number(rowId);
  223. deleteById(rowIdNumber)
  224. init()
  225. }
  226. function deleteSubjectsList() {
  227. const difference = checkedRowKeysRef.value.filter(item => !s.value.includes(item));
  228. s.value = checkedRowKeysRef.value
  229. for (let i = 0; i < difference.length; i++) {
  230. deleteById(Number(difference[i]));
  231. }
  232. init();
  233. }
  234. function searchCondition() {
  235. selectByCondition_1(pagination.page, pagination.pageSize, conditionParams).then(r => {
  236. tableData.value = r.data as [];
  237. }).catch(() => {
  238. console.log('jdias')
  239. })
  240. }
  241. const columns: Ref<DataTableColumns<SelectByCondition_1Params>> = ref([
  242. {
  243. type: 'selection',
  244. align: 'center',
  245. },
  246. {
  247. key: 'id',
  248. title: "ID",
  249. align: 'center',
  250. },
  251. {
  252. key: 'name',
  253. title: '学科名称',
  254. align: 'center'
  255. },
  256. {
  257. key: 'description',
  258. title: '学科描述',
  259. align: 'center'
  260. },
  261. {
  262. key: 'createTime',
  263. title: '创建时间',
  264. align: 'center'
  265. },
  266. {
  267. key: 'modifyTime',
  268. title: '修改时间',
  269. align: 'center'
  270. },
  271. {
  272. key: 'createUid',
  273. title: '创建用户ID',
  274. align: 'center'
  275. },
  276. {
  277. key: 'disabled',
  278. title: '状态',
  279. align: 'center',
  280. render: row => {
  281. if (row.disabled) {
  282. const tagTypes: Record<UserManagement.UserStatusKey, NaiveUI.ThemeColor> = {
  283. 'N': 'error',
  284. 'Y': 'success',
  285. };
  286. return <NTag type={tagTypes[row.disabled]}>{userStatusLabels[row.disabled]}</NTag>;
  287. }
  288. }
  289. },
  290. {
  291. key: 'actions',
  292. title: '操作',
  293. align: 'center',
  294. render: row => {
  295. return (
  296. <NSpace justify={'center'}>
  297. <NButton size={'small'} onClick={() => handleEditTable(row)} >
  298. 编辑
  299. </NButton>
  300. <NPopconfirm onPositiveClick={() => handleDeleteTable(row.id.toString())}>
  301. {{
  302. default: () => '确认删除',
  303. trigger: () => <NButton size={'small'}>删除</NButton>
  304. }}
  305. </NPopconfirm>
  306. </NSpace>
  307. );
  308. }
  309. },
  310. ])as Ref<DataTableColumns<SelectByCondition_1Params>>
  311. const rowKey = (row: RowData) => row.id
  312. function handleCheck(rowKeys: DataTableRowKey[]) {
  313. checkedRowKeysRef.value = rowKeys
  314. }
  315. </script>