index.vue 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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="danger" size="mini" @click="dels(scope.row)">删除</el-button>
  48. </template>
  49. </com-table>
  50. <com-dialog v-model="dialogVisible" :title="title">
  51. <ifno-write :info="info" @close="close" @success="success" />
  52. </com-dialog>
  53. </div>
  54. </template>
  55. <script lang="ts">
  56. import { defineComponent, ref } from 'vue'
  57. import IfnoWrite from './components/IfnoWrite.vue'
  58. import { useExample } from '@/hooks/useExample'
  59. import { Message } from '_c/Message'
  60. import { getExampleListApi, delsExampApi } from './api'
  61. const searchData = [
  62. {
  63. label: '标题',
  64. value: '',
  65. itemType: 'input',
  66. field: 'title',
  67. placeholder: '请输入标题',
  68. clearable: true
  69. }
  70. ]
  71. const columns = [
  72. {
  73. key: 'title',
  74. label: '标题',
  75. showOverflowTooltip: true
  76. },
  77. {
  78. key: 'author',
  79. label: '作者'
  80. },
  81. {
  82. key: 'display_time',
  83. label: '创建时间'
  84. },
  85. {
  86. key: 'importance',
  87. label: '重要性',
  88. slots: {
  89. default: 'importance'
  90. }
  91. },
  92. {
  93. key: 'pageviews',
  94. label: '阅读数'
  95. },
  96. {
  97. key: 'action',
  98. label: '操作',
  99. width: '150px',
  100. slots: {
  101. default: 'action'
  102. }
  103. }
  104. ]
  105. export default defineComponent({
  106. // name: 'Example',
  107. components: {
  108. IfnoWrite
  109. },
  110. setup() {
  111. const info = ref<any>(null)
  112. const {
  113. defalutParams,
  114. tableData,
  115. loading,
  116. total,
  117. dialogVisible,
  118. title,
  119. currentChange,
  120. sizeChange,
  121. handleSelectionChange,
  122. selectionData,
  123. delData
  124. } = useExample()
  125. // 请求数据
  126. async function getExampleList(data?: any): Promise<void> {
  127. try {
  128. const res = await getExampleListApi({
  129. params: Object.assign(defalutParams, data || {})
  130. })
  131. if (res.code === '0000') {
  132. total.value = res.data.total
  133. tableData.value = res.data.list
  134. }
  135. } finally {
  136. loading.value = false
  137. }
  138. }
  139. // 查询
  140. function searchSubmit(data: any) {
  141. // 该方法重置了一些默认参数
  142. currentChange(1)
  143. getExampleList(data)
  144. }
  145. // 重置
  146. function resetSubmit(data: any) {
  147. // 该方法重置了一些默认参数
  148. currentChange(1)
  149. getExampleList(data)
  150. }
  151. // 展示多少条
  152. function handleSizeChange(val: number) {
  153. // 该方法重置了一些默认参数
  154. sizeChange(val)
  155. getExampleList()
  156. }
  157. // 展示第几页
  158. function handleCurrentChange(val: number) {
  159. // 该方法重置了一些默认参数
  160. currentChange(val)
  161. getExampleList()
  162. }
  163. // 删除多选
  164. function dels(item?: any) {
  165. delData(async() => {
  166. let ids: string[]
  167. if (item.id) {
  168. ids = [item.id]
  169. } else {
  170. ids = selectionData.value.map((v: any) => {
  171. return v.id
  172. })
  173. }
  174. const res = await delsExampApi({
  175. data: { ids }
  176. })
  177. if (res.code === '0000') {
  178. Message.success('删除成功!')
  179. getExampleList()
  180. }
  181. }, { hiddenVerify: item.id })
  182. }
  183. // 打开弹窗
  184. function open(row: any) {
  185. title.value = row ? '编辑' : '新增'
  186. info.value = row || null
  187. dialogVisible.value = true
  188. }
  189. // 弹窗关闭
  190. function close() {
  191. dialogVisible.value = false
  192. }
  193. // 成功之后的回调
  194. function success(type: string) {
  195. if (type === 'add') {
  196. currentChange(1)
  197. }
  198. close()
  199. getExampleList()
  200. }
  201. getExampleList()
  202. return {
  203. info, open,
  204. searchData, searchSubmit, resetSubmit,
  205. columns,
  206. defalutParams,
  207. loading,
  208. tableData,
  209. total,
  210. title,
  211. dialogVisible,
  212. handleSizeChange,
  213. handleCurrentChange,
  214. handleSelectionChange,
  215. dels,
  216. close, success
  217. }
  218. }
  219. })
  220. </script>
  221. <style>
  222. </style>