use-table.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 { useAsyncState } from '@vueuse/core'
  18. import { queryTenantListPaging, deleteTenantById } from '@/service/modules/tenants'
  19. import { reactive, h, ref } from 'vue'
  20. import { NButton, NPopconfirm } from 'naive-ui'
  21. import { useI18n } from 'vue-i18n'
  22. import { DeleteOutlined, EditOutlined } from '@vicons/antd'
  23. export function useTable() {
  24. const { t } = useI18n()
  25. const handleEdit= (row: any) => {
  26. variables.showModalRef = true
  27. variables.statusRef = 1
  28. variables.row = row
  29. }
  30. const handleDelete = (row: any) => {
  31. deleteTenantById(row.id).then(() => {
  32. getTableData({
  33. pageSize: variables.pageSize,
  34. pageNo: variables.page,
  35. searchVal: variables.searchVal
  36. })
  37. })
  38. }
  39. const createColumns = () => {
  40. return [
  41. {
  42. title: '编号',
  43. key: 'num',
  44. },
  45. {
  46. title: '操作系统租户',
  47. key: 'tenantCode',
  48. },
  49. {
  50. title: '描述',
  51. key: 'description',
  52. },
  53. {
  54. title: '队列',
  55. key: 'queueName',
  56. },
  57. {
  58. title: '创建时间',
  59. key: 'createTime',
  60. },
  61. {
  62. title: '更新时间',
  63. key: 'updateTime',
  64. },
  65. {
  66. title: '操作',
  67. key: 'actions',
  68. render(row: any) {
  69. return h('div', null, [
  70. h(
  71. NButton,
  72. {
  73. size: 'small',
  74. onClick: () => {
  75. handleEdit(row)
  76. }
  77. },
  78. {
  79. icon: () => h(EditOutlined)
  80. }
  81. ),
  82. h(
  83. NPopconfirm,
  84. {
  85. onPositiveClick: () => { handleDelete(row) }
  86. },
  87. {
  88. trigger: () => h(
  89. NButton,
  90. {
  91. size: 'small',
  92. style: {'margin-left': '5px'},
  93. },
  94. {
  95. icon: () => h(DeleteOutlined),
  96. }
  97. ),
  98. default: () => {return '确定删除吗?'}
  99. }
  100. )
  101. ])
  102. }
  103. }
  104. ]
  105. }
  106. const variables = reactive({
  107. columns: createColumns(),
  108. tableData: [],
  109. page: ref(1),
  110. pageSize: ref(10),
  111. searchVal: ref(null),
  112. totalPage: ref(1),
  113. showModalRef: ref(false),
  114. statusRef: ref(0),
  115. row: {}
  116. })
  117. const getTableData = (params: any) => {
  118. const { state } = useAsyncState(
  119. queryTenantListPaging({ ...params }).then((res: any) => {
  120. variables.tableData = res.totalList.map((item: any, index: number) => {
  121. return {
  122. num: index + 1,
  123. ...item,
  124. }
  125. })
  126. variables.totalPage = res.totalPage
  127. }),
  128. {}
  129. )
  130. return state
  131. }
  132. return {
  133. variables,
  134. getTableData,
  135. }
  136. }