use-table.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. import { h, ref, reactive } from 'vue'
  18. import { useI18n } from 'vue-i18n'
  19. import { useRouter } from 'vue-router'
  20. import type { Router } from 'vue-router'
  21. import type { TableColumns } from 'naive-ui/es/data-table/src/interface'
  22. import { NSpace, NTooltip, NButton, NPopconfirm } from 'naive-ui'
  23. import { EditOutlined, DeleteOutlined } from '@vicons/antd'
  24. import { useAsyncState } from '@vueuse/core'
  25. import {
  26. queryUdfFuncListPaging,
  27. deleteUdfFunc
  28. } from '@/service/modules/resources'
  29. import { IUdfFunctionParam } from './types'
  30. export function useTable() {
  31. const { t } = useI18n()
  32. const router: Router = useRouter()
  33. const variables = reactive({
  34. columns: [],
  35. row: {},
  36. tableData: [],
  37. id: ref(Number(router.currentRoute.value.params.id) || -1),
  38. page: ref(1),
  39. pageSize: ref(10),
  40. searchVal: ref(),
  41. totalPage: ref(1),
  42. showRef: ref(false)
  43. })
  44. const createColumns = (variables: any) => {
  45. variables.columns = [
  46. {
  47. title: t('resource.function.id'),
  48. key: 'id',
  49. width: 50,
  50. render: (_row, index) => index + 1
  51. },
  52. {
  53. title: t('resource.function.udf_function_name'),
  54. key: 'funcName'
  55. },
  56. {
  57. title: t('resource.function.class_name'),
  58. key: 'className'
  59. },
  60. {
  61. title: t('resource.function.type'),
  62. key: 'type'
  63. },
  64. {
  65. title: t('resource.function.description'),
  66. key: 'description'
  67. },
  68. {
  69. title: t('resource.function.jar_package'),
  70. key: 'resourceName'
  71. },
  72. {
  73. title: t('resource.function.update_time'),
  74. key: 'updateTime'
  75. },
  76. {
  77. title: t('resource.function.operation'),
  78. key: 'operation',
  79. render: (row) => {
  80. return h(NSpace, null, {
  81. default: () => [
  82. h(
  83. NTooltip,
  84. {},
  85. {
  86. trigger: () =>
  87. h(
  88. NButton,
  89. {
  90. circle: true,
  91. type: 'info',
  92. size: 'tiny',
  93. onClick: () => {
  94. handleEdit(row)
  95. }
  96. },
  97. {
  98. icon: () => h(EditOutlined)
  99. }
  100. ),
  101. default: () => t('resource.function.edit')
  102. }
  103. ),
  104. h(
  105. NPopconfirm,
  106. {
  107. onPositiveClick: () => {
  108. handleDelete(row.id)
  109. }
  110. },
  111. {
  112. trigger: () =>
  113. h(
  114. NTooltip,
  115. {},
  116. {
  117. trigger: () =>
  118. h(
  119. NButton,
  120. {
  121. circle: true,
  122. type: 'error',
  123. size: 'tiny'
  124. },
  125. {
  126. icon: () => h(DeleteOutlined)
  127. }
  128. ),
  129. default: () => t('resource.function.delete')
  130. }
  131. ),
  132. default: () => t('resource.function.delete_confirm')
  133. }
  134. )
  135. ]
  136. })
  137. }
  138. }
  139. ] as TableColumns<any>
  140. }
  141. const getTableData = (params: IUdfFunctionParam) => {
  142. const { state } = useAsyncState(
  143. queryUdfFuncListPaging({ ...params }).then((res: any) => {
  144. variables.totalPage = res.totalPage
  145. variables.tableData = res.totalList.map((item: any) => {
  146. return { ...item }
  147. })
  148. }),
  149. { total: 0, table: [] }
  150. )
  151. return state
  152. }
  153. const handleEdit = (row: any) => {
  154. variables.showRef = true
  155. variables.row = row
  156. }
  157. const handleDelete = (id: number) => {
  158. /* after deleting data from the current page, you need to jump forward when the page is empty. */
  159. if (variables.tableData.length === 1 && variables.page > 1) {
  160. variables.page -= 1
  161. }
  162. deleteUdfFunc(id).then(() =>
  163. getTableData({
  164. id: variables.id,
  165. pageSize: variables.pageSize,
  166. pageNo: variables.page,
  167. searchVal: variables.searchVal
  168. })
  169. )
  170. }
  171. return {
  172. variables,
  173. createColumns,
  174. getTableData
  175. }
  176. }