index.vue 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <template>
  2. <div>
  3. <div class="search__example--wrap">
  4. <com-search
  5. :data="searchData"
  6. @search-submit="searchSubmit"
  7. @reset-submit="resetSubmit"
  8. />
  9. </div>
  10. <div class="button__example--wrap">
  11. <el-button type="primary" icon="el-icon-circle-plus-outline" @click="open(false)">新增</el-button>
  12. <el-button
  13. type="danger"
  14. icon="el-icon-delete"
  15. @click="dels"
  16. >删除</el-button>
  17. </div>
  18. <com-table
  19. v-loading="loading"
  20. selection
  21. :columns="columns"
  22. :data="tableData"
  23. :pagination="{
  24. currentPage: defalutParams.pageIndex,
  25. total: total,
  26. onSizeChange: handleSizeChange,
  27. onCurrentChange: handleCurrentChange
  28. }"
  29. @selection-change="handleSelectionChange"
  30. >
  31. <template #importance="scope">
  32. <el-tag
  33. :type="scope.row.importance === 3
  34. ? 'success'
  35. : (scope.row.importance === 2
  36. ? 'warning'
  37. : 'danger')"
  38. >{{ scope.row.importance === 3
  39. ? '重要'
  40. : (scope.row.importance === 2
  41. ? '良好'
  42. : '一般') }}
  43. </el-tag>
  44. </template>
  45. <template #action="scope">
  46. <el-button type="primary" size="mini" @click="open(scope.row)">编辑</el-button>
  47. <el-button type="success" size="mini" @click="open(scope.row, 'Detail')">查看</el-button>
  48. <el-button type="danger" size="mini" @click="dels(scope.row)">删除</el-button>
  49. </template>
  50. </com-table>
  51. </div>
  52. </template>
  53. <script lang="ts">
  54. import { defineComponent, onMounted, onUnmounted } from 'vue'
  55. import { useRouter } from 'vue-router'
  56. import vueBus from '@/vue-bus'
  57. import { useExample } from '@/hooks/useExample'
  58. import { Message } from '_c/Message'
  59. import { getExampleListApi, delsExampApi } from './api'
  60. const searchData = [
  61. {
  62. label: '标题',
  63. value: '',
  64. itemType: 'input',
  65. field: 'title',
  66. placeholder: '请输入标题',
  67. clearable: true
  68. }
  69. ]
  70. const columns = [
  71. {
  72. field: 'title',
  73. label: '标题',
  74. showOverflowTooltip: true
  75. },
  76. {
  77. field: 'author',
  78. label: '作者'
  79. },
  80. {
  81. field: 'display_time',
  82. label: '创建时间'
  83. },
  84. {
  85. field: 'importance',
  86. label: '重要性',
  87. slots: {
  88. default: 'importance'
  89. }
  90. },
  91. {
  92. field: 'pageviews',
  93. label: '阅读数'
  94. },
  95. {
  96. field: 'action',
  97. label: '操作',
  98. width: '220px',
  99. slots: {
  100. default: 'action'
  101. }
  102. }
  103. ]
  104. export default defineComponent({
  105. name: 'ExamplePage',
  106. setup() {
  107. const { push } = useRouter()
  108. const {
  109. defalutParams,
  110. tableData,
  111. loading,
  112. total,
  113. currentChange,
  114. sizeChange,
  115. handleSelectionChange,
  116. selectionData,
  117. delData
  118. } = useExample()
  119. // 请求数据
  120. async function getExampleList(data?: any): Promise<void> {
  121. try {
  122. const res = await getExampleListApi({
  123. params: Object.assign(defalutParams, data || {})
  124. })
  125. if (res.code === '0000') {
  126. total.value = res.data.total
  127. tableData.value = res.data.list
  128. }
  129. } finally {
  130. loading.value = false
  131. }
  132. }
  133. // 查询
  134. function searchSubmit(data: any) {
  135. // 该方法重置了一些默认参数
  136. currentChange(1)
  137. getExampleList(data)
  138. }
  139. // 重置
  140. function resetSubmit(data: any) {
  141. // 该方法重置了一些默认参数
  142. currentChange(1)
  143. getExampleList(data)
  144. }
  145. // 展示多少条
  146. function handleSizeChange(val: number) {
  147. // 该方法重置了一些默认参数
  148. sizeChange(val)
  149. getExampleList()
  150. }
  151. // 展示第几页
  152. function handleCurrentChange(val: number) {
  153. // 该方法重置了一些默认参数
  154. currentChange(val)
  155. getExampleList()
  156. }
  157. // 删除多选
  158. function dels(item?: any) {
  159. delData(async() => {
  160. let ids: string[]
  161. if (item.id) {
  162. ids = [item.id]
  163. } else {
  164. ids = selectionData.value.map((v: any) => {
  165. return v.id
  166. })
  167. }
  168. const res = await delsExampApi({
  169. data: { ids }
  170. })
  171. if (res.code === '0000') {
  172. Message.success('删除成功!')
  173. getExampleList()
  174. }
  175. }, { hiddenVerify: item.id })
  176. }
  177. // 打开新页面
  178. function open(row: any, component?: string) {
  179. push(!row
  180. ? `/example-demo/example-add`
  181. : (component
  182. ? `/example-demo/example-detail?id=${row.id}`
  183. : `/example-demo/example-edit?id=${row.id}`))
  184. }
  185. getExampleList()
  186. onMounted(() => {
  187. vueBus.$on('success', (type: string) => {
  188. if (type === 'add') {
  189. currentChange(1)
  190. }
  191. getExampleList()
  192. })
  193. })
  194. onUnmounted(() => {
  195. vueBus.$off('success')
  196. })
  197. return {
  198. open,
  199. searchData, searchSubmit, resetSubmit,
  200. columns,
  201. defalutParams,
  202. loading,
  203. tableData,
  204. total,
  205. handleSizeChange,
  206. handleCurrentChange,
  207. handleSelectionChange,
  208. dels
  209. }
  210. }
  211. })
  212. </script>
  213. <style>
  214. </style>