Table.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. <script setup lang="ts">
  2. import type { PropsParams } from '@/types/table'
  3. import { FilterType, FieldSpecialEffectType } from '@/types/table'
  4. import { computed, onMounted, reactive, ref, watch } from 'vue'
  5. import { useTable } from '@/hooks/useTable'
  6. import FilterPopover from './toolsBtn/FilterPopover.vue'
  7. import RegreshBtn from './toolsBtn/RegreshBtn.vue'
  8. import type { FormInstance } from 'element-plus'
  9. // 表格工具图标大小
  10. const toolsIconSize = ref(25)
  11. // 查询表单
  12. const queryFormRef = ref<FormInstance>()
  13. // 传过来的配置
  14. const props = defineProps<PropsParams>()
  15. const emits = defineEmits(['addNewItem'])
  16. // 表格数据
  17. const tableData: Array<any> = reactive([])
  18. // 查询表单的数据
  19. const queryFormData = reactive<any>({})
  20. // 一些公用方法
  21. const { getTableData } = useTable(tableData, props.paginationConfig)
  22. // 所有类型为input的表单控件信息
  23. const inputFieldsList = computed(() => {
  24. return props.queryInfo?.filter((item) => item.type === FilterType.INPUT)
  25. })
  26. // 所有类型为select的表单控件信息
  27. const selectFieldsList = computed(() => {
  28. return props.queryInfo?.filter((item) => {
  29. if (item.default) {
  30. queryFormData[item.name] = item.default
  31. }
  32. return item.type === FilterType.SELECT
  33. })
  34. })
  35. console.log(selectFieldsList)
  36. // 所有类型为date的表单控件信息
  37. const dateFieldsList = computed(() => {
  38. return props.queryInfo?.filter((item) => item.type === FilterType.DATE)
  39. })
  40. // 改变页码
  41. const handleCurrentChange = (val: number) => {
  42. props.paginationConfig.currentPage = val
  43. }
  44. // 改变每页大小
  45. const handleSizeChange = (val: number) => {
  46. props.paginationConfig.limit = val
  47. }
  48. // 获取数据
  49. const getData = () => {
  50. if (props.openPageQuery) {
  51. // 如果开启了分页查询,那么要计算出需要展示的页码位置所对应的偏移量
  52. // 同时要将查询的条数改为对应的用户选择的展示条数
  53. props.requestConfig.other.offset =
  54. (props.paginationConfig.currentPage - 1) * props.paginationConfig.limit
  55. props.requestConfig.other.limit = props.paginationConfig.limit
  56. }
  57. // 查询时要根据是否开启分页查询传入对应参数
  58. getTableData(props.requestConfig.url, props.requestConfig.other, props.openPageQuery)
  59. }
  60. // 清空表格数据
  61. const resetTableData = () => {
  62. tableData.splice(0, tableData.length)
  63. }
  64. // 根据分页大小的切换来更新数据
  65. // 这里将他赋值,用于根据传入的配置来选择是否开启该监听
  66. const changePageLimit = watch(
  67. () => [props.paginationConfig.limit, props.paginationConfig.currentPage],
  68. ([newLimit], [oldLimit]) => {
  69. if (newLimit != oldLimit) {
  70. // 需要给分页按钮加上:current-page.sync="current_page" 配置,不然不生效
  71. // 当改变每页大小时把之前的缓存全部清除,重新开始
  72. props.paginationConfig.currentPage = 1
  73. resetTableData()
  74. getData()
  75. } else if (!tableData[props.paginationConfig.currentPage]) {
  76. // 当对应的数组下标位置没有这页的数据的时候再去请求
  77. getData()
  78. }
  79. },
  80. { deep: true }
  81. )
  82. // 按条件查询
  83. const queryTableData = () => {
  84. props.requestConfig.other = { ...props.requestConfig.other, ...queryFormData }
  85. getData()
  86. }
  87. // 重置查询的条件
  88. const resetQueryForm = (formEl: FormInstance | undefined) => {
  89. if (!formEl) return
  90. formEl?.resetFields()
  91. }
  92. // 没有开启分页查询就关闭掉这个监听
  93. if (!props.openPageQuery) changePageLimit()
  94. // 定义暴露出去的方法
  95. defineExpose({
  96. getData,
  97. resetTableData
  98. })
  99. onMounted(() => {
  100. getData()
  101. })
  102. </script>
  103. <template>
  104. <div class="tableContent">
  105. <div class="filterBox" v-if="openFilterQuery">
  106. <!-- slot -->
  107. <div class="filterHeader">
  108. <span>查询条件</span>
  109. </div>
  110. <div class="filterBody">
  111. <el-form
  112. :inline="true"
  113. ref="queryFormRef"
  114. :model="queryFormData"
  115. class="queryForm"
  116. :label-position="'left'"
  117. >
  118. <!-- 所有的input查询框 -->
  119. <el-form-item :label="item.label" v-for="item in inputFieldsList" class="filterItem">
  120. <el-input
  121. v-model="queryFormData[item.name]"
  122. :placeholder="item.placeholder"
  123. clearable
  124. />
  125. </el-form-item>
  126. <!-- 所有选择框 -->
  127. <el-form-item :label="item.label" v-for="item in selectFieldsList" class="filterItem">
  128. <el-select v-model="queryFormData[item.name]" :placeholder="item.placeholder">
  129. <el-option v-for="val in item.otherOption" :label="val.cnName" :value="val.value" />
  130. </el-select>
  131. </el-form-item>
  132. <!-- 所有日期选择框 -->
  133. <el-form-item :label="item.label" v-for="item in dateFieldsList" class="filterItem">
  134. <el-date-picker
  135. v-model="queryFormData[item.name]"
  136. type="date"
  137. :placeholder="item.placeholder"
  138. clearable
  139. />
  140. </el-form-item>
  141. </el-form>
  142. <!-- 分割线 -->
  143. <!-- <el-divider class="queryPartition" content-position="center" direction="vertical" /> -->
  144. <div class="queryBox">
  145. <el-divider class="queryPartition" content-position="center" direction="vertical" />
  146. <div class="queryBtnBox">
  147. <el-button class="queryBtn" color="#165dff" @click="queryTableData">
  148. <el-icon><Search /></el-icon>查询
  149. </el-button>
  150. <el-button class="refreshBtn" color="#f2f3f5" @click="resetQueryForm(queryFormRef)">
  151. <el-icon><RefreshRight /></el-icon>重置
  152. </el-button>
  153. </div>
  154. </div>
  155. </div>
  156. </div>
  157. <!-- 分割线 -->
  158. <el-divider class="partition" content-position="center" />
  159. <div class="tableTools">
  160. <div class="leftTools">
  161. <el-button v-if="needLeftTools" type="primary" color="#165dff" @click="emits('addNewItem')">
  162. <el-icon><Plus /></el-icon>新增
  163. </el-button>
  164. </div>
  165. <div class="rightTools">
  166. <el-button color="#f0f1f3" size="default" class="rightToolsItem">
  167. <el-icon><Download /></el-icon>下载
  168. </el-button>
  169. <RegreshBtn @refresh-table="getData" :icon-size="toolsIconSize"></RegreshBtn>
  170. <FilterPopover
  171. :table-fields-info="tableFieldsInfo"
  172. :icon-size="toolsIconSize"
  173. ></FilterPopover>
  174. </div>
  175. </div>
  176. <div class="tableBox">
  177. <el-table
  178. :data="openPageQuery ? tableData[paginationConfig.currentPage] : tableData"
  179. style="width: 100%"
  180. class="tableBody"
  181. >
  182. <el-table-column align="center" label="#" type="index" :index="1" />
  183. <template v-for="item in tableFieldsInfo">
  184. <el-table-column
  185. :prop="item.name"
  186. :label="item.cnName"
  187. width="auto"
  188. align="center"
  189. show-overflow-tooltip
  190. v-if="item.isShow"
  191. >
  192. <template v-slot="scope">
  193. <!-- tag类 -->
  194. <el-tag
  195. v-if="item.specialEffect?.type === FieldSpecialEffectType.TAG"
  196. :type="scope.row[item.name] ? 'danger' : 'success'"
  197. >
  198. {{
  199. scope.row[item.name]
  200. ? item.specialEffect.othnerInfo[0]
  201. : item.specialEffect.othnerInfo[1]
  202. }}
  203. </el-tag>
  204. <!-- 头像类 -->
  205. <el-image
  206. v-else-if="item.specialEffect?.type === FieldSpecialEffectType.IMG"
  207. :preview-teleported="true"
  208. :src="scope.row[item.name]"
  209. :preview-src-list="[scope.row[item.name]]"
  210. style="width: 35px; height: 35px"
  211. :fit="'fill'"
  212. :hide-on-click-modal="true"
  213. >
  214. <template #error>
  215. <!-- -->
  216. <img style="width: 35px; height: 35px" src="../assets/default/defaultHead.png" />
  217. </template>
  218. </el-image>
  219. <!-- 文字类 -->
  220. <el-text
  221. v-else-if="item.specialEffect?.type === FieldSpecialEffectType.TEXT"
  222. :type="scope.row[item.name] ? 'danger' : 'success'"
  223. >
  224. {{ scope.row[item.name] }}
  225. </el-text>
  226. <!-- 翻译类 -->
  227. <el-text v-else-if="item.specialEffect?.type === FieldSpecialEffectType.TRANSLATE">
  228. {{ item.specialEffect.othnerInfo[scope.row[item.name]] }}
  229. </el-text>
  230. <el-text v-else>
  231. <!-- 其他列按默认方式显示 -->
  232. {{ scope.row[item.name] }}
  233. </el-text>
  234. </template>
  235. </el-table-column>
  236. </template>
  237. <slot name="tableOperation"></slot>
  238. </el-table>
  239. <div class="userTablePaginationBox">
  240. <el-pagination
  241. class="userTablePagination"
  242. background
  243. :page-size="paginationConfig.limit"
  244. :page-sizes="paginationConfig.pagesizeList"
  245. table-layout="fixed"
  246. layout="prev, pager, next ,jumper ,sizes,total,"
  247. :total="paginationConfig.total"
  248. :current-page.sync="paginationConfig.currentPage"
  249. @current-change="handleCurrentChange"
  250. @size-change="handleSizeChange"
  251. />
  252. </div>
  253. </div>
  254. </div>
  255. </template>
  256. <style scoped>
  257. .tableContent {
  258. margin: 0 auto;
  259. width: 98%;
  260. /* height: 100%; */
  261. }
  262. .filterBox,
  263. .tableBox {
  264. width: 100%;
  265. }
  266. .filterBox {
  267. margin-top: 1%;
  268. min-height: 18%;
  269. display: flex;
  270. flex-direction: column;
  271. }
  272. .filterHeader,
  273. .filterBody {
  274. width: 98%;
  275. margin: 0 auto;
  276. }
  277. .filterHeader {
  278. display: flex;
  279. align-items: center;
  280. color: black;
  281. font-size: 20px;
  282. font-weight: bold;
  283. /* background-color: lightblue; */
  284. /* margin-bottom: 1%; */
  285. }
  286. .filterBody {
  287. display: flex;
  288. }
  289. .queryBox {
  290. width: 10%;
  291. display: flex;
  292. justify-content: space-around;
  293. }
  294. .queryBtnBox {
  295. width: 100%;
  296. display: flex;
  297. flex-direction: column;
  298. align-items: center;
  299. justify-content: center;
  300. }
  301. .refreshBtn {
  302. margin-top: 10%;
  303. margin-bottom: 10%;
  304. }
  305. .queryPartition {
  306. height: 90%;
  307. }
  308. .queryForm {
  309. width: 90%;
  310. display: flex;
  311. flex-wrap: wrap;
  312. /* justify-content: space-between; */
  313. }
  314. .filterItem {
  315. width: 30%;
  316. display: flex;
  317. align-items: center;
  318. }
  319. .partition {
  320. width: 98%;
  321. margin: 0 auto;
  322. }
  323. .tableTools {
  324. width: 98%;
  325. margin: 0 auto;
  326. display: flex;
  327. justify-content: space-between;
  328. margin-top: 1%;
  329. }
  330. .leftTools,
  331. .rightTools {
  332. width: 10%;
  333. display: flex;
  334. align-items: center;
  335. justify-content: space-between;
  336. }
  337. .rightTools {
  338. width: 10%;
  339. }
  340. .tableBox {
  341. width: 98%;
  342. /* height: 98%; */
  343. margin: 0 auto;
  344. /* margin-top: 0.5%;
  345. margin-bottom: 2%; */
  346. }
  347. .userTablePaginationBox {
  348. width: 98%;
  349. margin: 0.5% auto;
  350. display: flex;
  351. justify-content: center;
  352. }
  353. </style>