index.vue 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. <template>
  2. <div class="table-container">
  3. <vab-query-form>
  4. <vab-query-form-left-panel>
  5. <el-button icon="el-icon-plus" type="primary" @click="handleAdd">
  6. 添加
  7. </el-button>
  8. <el-button icon="el-icon-delete" type="danger" @click="handleDelete">
  9. 删除
  10. </el-button>
  11. <el-button type="primary" @click="testMessage">baseMessage</el-button>
  12. <el-button type="primary" @click="testALert">baseAlert</el-button>
  13. <el-button type="primary" @click="testConfirm">baseConfirm</el-button>
  14. <el-button type="primary" @click="testNotify">baseNotify</el-button>
  15. </vab-query-form-left-panel>
  16. <vab-query-form-right-panel>
  17. <el-form
  18. ref="form"
  19. :model="queryForm"
  20. :inline="true"
  21. @submit.native.prevent
  22. >
  23. <el-form-item>
  24. <el-input v-model="queryForm.title" placeholder="标题" />
  25. </el-form-item>
  26. <el-form-item>
  27. <el-button
  28. icon="el-icon-search"
  29. type="primary"
  30. native-type="submit"
  31. @click="handleQuery"
  32. >
  33. 查询
  34. </el-button>
  35. </el-form-item>
  36. </el-form>
  37. </vab-query-form-right-panel>
  38. </vab-query-form>
  39. <el-table
  40. ref="tableSort"
  41. v-loading="listLoading"
  42. :data="list"
  43. :element-loading-text="elementLoadingText"
  44. :height="height"
  45. @selection-change="setSelectRows"
  46. @sort-change="tableSortChange"
  47. >
  48. <el-table-column
  49. show-overflow-tooltip
  50. type="selection"
  51. width="55"
  52. ></el-table-column>
  53. <el-table-column show-overflow-tooltip label="序号" width="95">
  54. <template #default="scope">
  55. {{ scope.$index + 1 }}
  56. </template>
  57. </el-table-column>
  58. <el-table-column
  59. show-overflow-tooltip
  60. prop="title"
  61. label="标题"
  62. ></el-table-column>
  63. <el-table-column
  64. show-overflow-tooltip
  65. label="作者"
  66. prop="author"
  67. ></el-table-column>
  68. <el-table-column show-overflow-tooltip label="头像">
  69. <template #default="{ row }">
  70. <el-image
  71. v-if="imgShow"
  72. :preview-src-list="imageList"
  73. :src="row.img"
  74. ></el-image>
  75. </template>
  76. </el-table-column>
  77. <el-table-column
  78. show-overflow-tooltip
  79. label="点击量"
  80. prop="pageViews"
  81. sortable
  82. ></el-table-column>
  83. <el-table-column show-overflow-tooltip label="状态">
  84. <template #default="{ row }">
  85. <el-tooltip
  86. :content="row.status"
  87. class="item"
  88. effect="dark"
  89. placement="top-start"
  90. >
  91. <el-tag :type="row.status | statusFilter">
  92. {{ row.status }}
  93. </el-tag>
  94. </el-tooltip>
  95. </template>
  96. </el-table-column>
  97. <el-table-column
  98. show-overflow-tooltip
  99. label="时间"
  100. prop="datetime"
  101. width="200"
  102. ></el-table-column>
  103. <el-table-column show-overflow-tooltip label="操作" width="180px">
  104. <template #default="{ row }">
  105. <el-button type="text" @click="handleEdit(row)">编辑</el-button>
  106. <el-button type="text" @click="handleDelete(row)">删除</el-button>
  107. </template>
  108. </el-table-column>
  109. </el-table>
  110. <el-pagination
  111. :background="background"
  112. :current-page="queryForm.pageNo"
  113. :layout="layout"
  114. :page-size="queryForm.pageSize"
  115. :total="total"
  116. @current-change="handleCurrentChange"
  117. @size-change="handleSizeChange"
  118. ></el-pagination>
  119. <table-edit ref="edit"></table-edit>
  120. </div>
  121. </template>
  122. <script>
  123. import { getList, doDelete } from '@/api/table'
  124. import TableEdit from './components/TableEdit'
  125. export default {
  126. name: 'ComprehensiveTable',
  127. components: {
  128. TableEdit,
  129. },
  130. filters: {
  131. statusFilter(status) {
  132. const statusMap = {
  133. published: 'success',
  134. draft: 'gray',
  135. deleted: 'danger',
  136. }
  137. return statusMap[status]
  138. },
  139. },
  140. data() {
  141. return {
  142. imgShow: true,
  143. list: [],
  144. imageList: [],
  145. listLoading: true,
  146. layout: 'total, sizes, prev, pager, next, jumper',
  147. total: 0,
  148. background: true,
  149. selectRows: '',
  150. elementLoadingText: '正在加载...',
  151. queryForm: {
  152. pageNo: 1,
  153. pageSize: 20,
  154. title: '',
  155. },
  156. }
  157. },
  158. computed: {
  159. height() {
  160. alert(this.$baseTableHeight())
  161. return this.$baseTableHeight()
  162. },
  163. },
  164. created() {
  165. this.fetchData()
  166. },
  167. beforeDestroy() {},
  168. mounted() {},
  169. methods: {
  170. tableSortChange() {
  171. const imageList = []
  172. this.$refs.tableSort.tableData.forEach((item, index) => {
  173. imageList.push(item.img)
  174. })
  175. this.imageList = imageList
  176. },
  177. setSelectRows(val) {
  178. this.selectRows = val
  179. },
  180. handleAdd() {
  181. this.$refs['edit'].showEdit()
  182. },
  183. handleEdit(row) {
  184. this.$refs['edit'].showEdit(row)
  185. },
  186. handleDelete(row) {
  187. if (row.id) {
  188. this.$baseConfirm('你确定要删除当前项吗', null, async () => {
  189. const { msg } = await doDelete({ ids: row.id })
  190. this.$baseMessage(msg, 'success')
  191. this.fetchData()
  192. })
  193. } else {
  194. if (this.selectRows.length > 0) {
  195. const ids = this.selectRows.map((item) => item.id).join()
  196. this.$baseConfirm('你确定要删除选中项吗', null, async () => {
  197. const { msg } = await doDelete({ ids: ids })
  198. this.$baseMessage(msg, 'success')
  199. this.fetchData()
  200. })
  201. } else {
  202. this.$baseMessage('未选中任何行', 'error')
  203. return false
  204. }
  205. }
  206. },
  207. handleSizeChange(val) {
  208. this.queryForm.pageSize = val
  209. this.fetchData()
  210. },
  211. handleCurrentChange(val) {
  212. this.queryForm.pageNo = val
  213. this.fetchData()
  214. },
  215. handleQuery() {
  216. this.queryForm.pageNo = 1
  217. this.fetchData()
  218. },
  219. async fetchData() {
  220. this.listLoading = true
  221. const { data, totalCount } = await getList(this.queryForm)
  222. this.list = data
  223. const imageList = []
  224. data.forEach((item, index) => {
  225. imageList.push(item.img)
  226. })
  227. this.imageList = imageList
  228. this.total = totalCount
  229. setTimeout(() => {
  230. this.listLoading = false
  231. }, 500)
  232. },
  233. testMessage() {
  234. this.$baseMessage('test1', 'success')
  235. },
  236. testALert() {
  237. this.$baseAlert('11')
  238. this.$baseAlert('11', '自定义标题', () => {
  239. /* 可以写回调; */
  240. })
  241. this.$baseAlert('11', null, () => {
  242. /* 可以写回调; */
  243. })
  244. },
  245. testConfirm() {
  246. this.$baseConfirm(
  247. '你确定要执行该操作?',
  248. null,
  249. () => {
  250. /* 可以写回调; */
  251. },
  252. () => {
  253. /* 可以写回调; */
  254. }
  255. )
  256. },
  257. testNotify() {
  258. this.$baseNotify('测试消息提示', 'test', 'success', 'bottom-right')
  259. },
  260. },
  261. }
  262. </script>