indexcopy.vue 8.0 KB

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