|
@@ -15,7 +15,7 @@ import { fuzzySearch, generateRandomFileName, throttleFunc } from '@/utils/commo
|
|
|
|
|
|
import { useTable } from '@/hooks/useTable.ts'
|
|
|
import { useRequest } from '@/hooks/useRequest.ts'
|
|
|
-import { computed, onMounted, reactive, ref, toRaw, watch } from 'vue'
|
|
|
+import { computed, type ComputedRef, onMounted, reactive, ref, toRaw, watch } from 'vue'
|
|
|
import { downLoadData } from '@/utils/table/table'
|
|
|
|
|
|
import TableFilterForm from '@/components/table/TableFilterForm/TableFilterForm.vue'
|
|
@@ -36,10 +36,11 @@ const throttleTime = 500
|
|
|
const props = withDefaults(defineProps<PropsParams>(), {
|
|
|
needRowindex: true,
|
|
|
needAverage: false,
|
|
|
- openFilterQuery: false,
|
|
|
+ openFilterQuery: true,
|
|
|
openPageQuery: true,
|
|
|
- openRemoteQuery: true,
|
|
|
- loadingState: false
|
|
|
+ loadingState: false,
|
|
|
+ openRemoteReqData: true,
|
|
|
+ openRemoteQuery: true
|
|
|
})
|
|
|
|
|
|
const emits = defineEmits(['addNewItem', 'upload', 'downLoad'])
|
|
@@ -71,23 +72,27 @@ const reqConfig = reactive<ReqConfig>({
|
|
|
})
|
|
|
|
|
|
// 一些公用方法
|
|
|
-const { getTableData } = useTable(tableData, paginationConfig)
|
|
|
+const { getTableData, setTableData } = useTable(tableData, paginationConfig)
|
|
|
|
|
|
/**
|
|
|
- * 计算表格数据,如果开启了分页查询,那么就返回一个空数组,否则返回当前页码的数据
|
|
|
+ * 本地使用的数据,只有在使用外部数据的情况下使用
|
|
|
*/
|
|
|
-const tableDataNoPaging = computed<any[]>(() => {
|
|
|
- // 开启分页查询则需要去远程查询,不使用此处的动态计算的数据
|
|
|
- if (props.openRemoteQuery) return []
|
|
|
- let curPage = paginationConfig.currentPage
|
|
|
- let limit = paginationConfig.limit
|
|
|
- let begin = curPage * limit - limit
|
|
|
- //这里不减一是因为,slice方法裁切是左闭右开数组
|
|
|
- let end = curPage * limit
|
|
|
- return tableData.slice(begin, end)
|
|
|
-})
|
|
|
+let localTableData: ComputedRef | [] = []
|
|
|
+if (!props.openRemoteReqData && props.dataList) {
|
|
|
+ localTableData = computed<Array<any>>(() => {
|
|
|
+ let curPage = paginationConfig.currentPage
|
|
|
+ let limit = paginationConfig.limit
|
|
|
+ let begin = curPage * limit - limit
|
|
|
+ let end = curPage * limit
|
|
|
+ console.log(tableData)
|
|
|
+ return tableData.slice(begin, end)
|
|
|
+ })
|
|
|
+}
|
|
|
|
|
|
-// 计算行号
|
|
|
+/**
|
|
|
+ * 开启行号功能后,计算行号
|
|
|
+ * @param index 当前行索引
|
|
|
+ */
|
|
|
const computedRowIndex = (index: number) => {
|
|
|
return (paginationConfig.currentPage - 1) * paginationConfig.limit + index + 1
|
|
|
}
|
|
@@ -111,41 +116,88 @@ const handleSizeChange = (val: number) => {
|
|
|
/**
|
|
|
* @description 加载表格数据
|
|
|
*/
|
|
|
-const loadTableData = async (): Promise<boolean> => {
|
|
|
- loading.value = true
|
|
|
+// const loadTableData = async (): Promise<boolean> => {
|
|
|
+// try {
|
|
|
+// // if (!props.openRemoteReqData) {
|
|
|
+// // if (props.dataList) {
|
|
|
+// // tableData.splice(0, tableData.length, ...props.dataList)
|
|
|
+// // if (props.paginationConfig) {
|
|
|
+// // paginationConfig.total = props.paginationConfig.total
|
|
|
+// // }
|
|
|
+// // return true
|
|
|
+// // } else {
|
|
|
+// // console.error('请传入dataList,没有数据源!')
|
|
|
+// // return false
|
|
|
+// // }
|
|
|
+// // }
|
|
|
+//
|
|
|
+// // 如果是直接传入的数据
|
|
|
+// if (props.dataList) {
|
|
|
+// tableData.splice(0, tableData.length, ...props.dataList)
|
|
|
+// if (props.paginationConfig) {
|
|
|
+// paginationConfig.total = props.paginationConfig.total
|
|
|
+// }
|
|
|
+// return true
|
|
|
+// }
|
|
|
+//
|
|
|
+// // 如果需要表格自己请求数据的
|
|
|
+// if (props.requestConfig) {
|
|
|
+// if (props.openRemoteQuery) {
|
|
|
+// reqConfig.otherOptions.offset = (paginationConfig.currentPage - 1) * paginationConfig.limit
|
|
|
+// reqConfig.otherOptions.limit = paginationConfig.limit
|
|
|
+// }
|
|
|
+//
|
|
|
+// await getTableData(reqConfig.url, reqConfig.otherOptions, props.openRemoteQuery)
|
|
|
+// backupTableData.splice(0, backupTableData.length, ...tableData)
|
|
|
+// return true
|
|
|
+// }
|
|
|
+//
|
|
|
+// // 如果 dataList 和 requestConfig 都没有传入,返回 false
|
|
|
+// console.warn('Both dataList and requestConfig are undefined. Returning early.')
|
|
|
+// return false
|
|
|
+// } catch (err) {
|
|
|
+// console.error('Error while loading table data:', err)
|
|
|
+// return false // 确保返回 false 表示失败
|
|
|
+// }
|
|
|
+// }
|
|
|
|
|
|
+const loadTableData = async (): Promise<boolean> => {
|
|
|
try {
|
|
|
- console.log('执行')
|
|
|
- // 如果是直接传入的数据
|
|
|
- if (props.dataList) {
|
|
|
- console.log('执行')
|
|
|
- tableData.splice(0, tableData.length, ...props.dataList)
|
|
|
- if (props.paginationConfig) {
|
|
|
- paginationConfig.total = props.paginationConfig.total
|
|
|
+ // 使用传入数据源
|
|
|
+ // 如果使用前端查询,则需要传入dataList作为数据源
|
|
|
+ if (!props.openRemoteReqData) {
|
|
|
+ if (props.dataList) {
|
|
|
+ const backList = JSON.parse(JSON.stringify(props.dataList))
|
|
|
+ tableData.splice(0, tableData.length, ...backList)
|
|
|
+ backupTableData.splice(0, tableData.length, ...backList)
|
|
|
+ console.log('执行1')
|
|
|
+ if (props.paginationConfig) {
|
|
|
+ paginationConfig.total = backList.length
|
|
|
+ }
|
|
|
+ return true
|
|
|
+ } else {
|
|
|
+ console.error('请传入dataList,没有数据源!')
|
|
|
+ return false
|
|
|
}
|
|
|
- return true
|
|
|
}
|
|
|
|
|
|
// 如果需要表格自己请求数据的
|
|
|
+ // 必须要传入requestConfig
|
|
|
if (props.requestConfig) {
|
|
|
- if (props.openRemoteQuery) {
|
|
|
- reqConfig.otherOptions.offset = (paginationConfig.currentPage - 1) * paginationConfig.limit
|
|
|
- reqConfig.otherOptions.limit = paginationConfig.limit
|
|
|
- }
|
|
|
-
|
|
|
- await getTableData(reqConfig.url, reqConfig.otherOptions, props.openRemoteQuery)
|
|
|
+ // 当使用远程请求数据源的时候,默认使用远程查询
|
|
|
+ reqConfig.otherOptions.offset = (paginationConfig.currentPage - 1) * paginationConfig.limit
|
|
|
+ reqConfig.otherOptions.limit = paginationConfig.limit
|
|
|
+ const [tableData, total] = await getTableData(reqConfig.url, reqConfig.otherOptions)
|
|
|
+ setTableData(tableData, total, props.openPageQuery)
|
|
|
backupTableData.splice(0, backupTableData.length, ...tableData)
|
|
|
return true
|
|
|
+ } else {
|
|
|
+ console.error('缺少请求配置')
|
|
|
+ return false
|
|
|
}
|
|
|
-
|
|
|
- // 如果 dataList 和 requestConfig 都没有传入,返回 false
|
|
|
- console.warn('Both dataList and requestConfig are undefined. Returning early.')
|
|
|
- return false
|
|
|
} catch (err) {
|
|
|
console.error('Error while loading table data:', err)
|
|
|
return false // 确保返回 false 表示失败
|
|
|
- } finally {
|
|
|
- loading.value = false // 确保 loading 状态被正确重置
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -154,8 +206,13 @@ const loadTableData = async (): Promise<boolean> => {
|
|
|
*/
|
|
|
const getData = async () => {
|
|
|
try {
|
|
|
+ loading.value = true
|
|
|
// 等待数据加载完成
|
|
|
- await loadTableData()
|
|
|
+ const loadResult = await loadTableData()
|
|
|
+ if (!loadResult) {
|
|
|
+ ElMessage.error('获取表格数据失败')
|
|
|
+ return false
|
|
|
+ }
|
|
|
// 如果需要平均值字段,则需要在表格头部插入一行计算平均值
|
|
|
if (props.needAverage) {
|
|
|
let rowData: any = {}
|
|
@@ -181,6 +238,8 @@ const getData = async () => {
|
|
|
} catch (err) {
|
|
|
console.log(err)
|
|
|
return false
|
|
|
+ } finally {
|
|
|
+ loading.value = false // 确保 loading 状态被正确重置
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -198,7 +257,7 @@ const resetTableData = () => {
|
|
|
* @description 按条件查询,如果开启了分页查询,那么会直接重新查询数据,否则,会根据现有数据进行查询
|
|
|
*/
|
|
|
const queryTableData = () => {
|
|
|
- if (props.openRemoteQuery && props.requestConfig) {
|
|
|
+ if (props.openRemoteQuery && props.openRemoteReqData && props.requestConfig) {
|
|
|
reqConfig.otherOptions = { ...props.requestConfig.otherOptions, ...queryFormData }
|
|
|
// 需要在查询前清除掉目前的数据,不然会导致之前缓存的数据混入
|
|
|
// 比如第一页已经缓存了,在第二页重新查询,在切回第一页,还是显示查询前的数据,因为缓存没被清除
|
|
@@ -209,6 +268,9 @@ const queryTableData = () => {
|
|
|
// 过滤出来所有符合formData数据的条件
|
|
|
filteredTable = backupTableData.filter((item) => {
|
|
|
let state = true
|
|
|
+ console.log('aa')
|
|
|
+ console.log(queryFormData)
|
|
|
+ console.log('bb')
|
|
|
for (let [k, v] of Object.entries(queryFormData)) {
|
|
|
// 模糊查询,看值是否跟表格中的数据匹配
|
|
|
if (!fuzzySearch(v, item[k])) {
|
|
@@ -218,8 +280,11 @@ const queryTableData = () => {
|
|
|
}
|
|
|
return state
|
|
|
})
|
|
|
+
|
|
|
paginationConfig.total = filteredTable.length
|
|
|
+
|
|
|
tableData.splice(0, tableData.length, ...filteredTable)
|
|
|
+ // console.log(JSON.parse(JSON.stringify(tableData)))
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -232,7 +297,7 @@ const throttleQueryTableData = throttleFunc(queryTableData, throttleTime)
|
|
|
* @param rowData 插入的数据
|
|
|
*/
|
|
|
const insertRow = (start: number, rowData: any) => {
|
|
|
- if (props.openRemoteQuery) {
|
|
|
+ if (props.openRemoteReqData) {
|
|
|
tableData[start].splice(0, 0, rowData)
|
|
|
} else {
|
|
|
tableData.splice(start, 0, rowData)
|
|
@@ -301,7 +366,7 @@ const watchCurPage = watch(
|
|
|
)
|
|
|
|
|
|
// 如果没有开启分页查询,直接关闭这两个监听
|
|
|
-if (!props.openRemoteQuery) {
|
|
|
+if (!props.openPageQuery) {
|
|
|
watchLimit()
|
|
|
watchCurPage()
|
|
|
}
|
|
@@ -368,13 +433,13 @@ const createRowKey = () => {
|
|
|
// 没有pf取消掉
|
|
|
if (!props.requestConfig?.otherOptions.pf) watchPf()
|
|
|
|
|
|
-//如果没有日期就取消掉
|
|
|
+// 如果没有日期就取消掉
|
|
|
if (!props.requestConfig?.otherOptions.startTime && !props.requestConfig?.otherOptions.endTime) {
|
|
|
watchDateChange()
|
|
|
}
|
|
|
|
|
|
-// 没传入datalist则取消该监听
|
|
|
-if (!props.dataList) {
|
|
|
+// 如果是使用远程数据源,则取消监听
|
|
|
+if (props.openRemoteReqData) {
|
|
|
changeDataList()
|
|
|
}
|
|
|
|
|
@@ -403,7 +468,7 @@ const tableSortChange = (data: { column: any; prop: string; order: any }) => {
|
|
|
else if (order === 'descending') order = 'desc'
|
|
|
else order = ''
|
|
|
reqConfig.otherOptions.order = order
|
|
|
- getData()
|
|
|
+ throttleQueryTableData()
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -438,6 +503,42 @@ const outGetTableData = () => {
|
|
|
return toRaw(tableData).flat()
|
|
|
}
|
|
|
|
|
|
+/**
|
|
|
+ * 对传入的props进行检查,对错误配置进行提示
|
|
|
+ */
|
|
|
+const checkPropsConfig = () => {
|
|
|
+ const {
|
|
|
+ openFilterQuery,
|
|
|
+ queryInfo,
|
|
|
+ openPageQuery,
|
|
|
+ paginationConfig,
|
|
|
+ openRemoteReqData,
|
|
|
+ requestConfig,
|
|
|
+ openRemoteQuery,
|
|
|
+ dataList
|
|
|
+ } = props
|
|
|
+ if (openFilterQuery && !queryInfo) {
|
|
|
+ console.error('请输入查询的配置信息')
|
|
|
+ }
|
|
|
+ if (openPageQuery && !paginationConfig) {
|
|
|
+ console.error('请输入分页配置信息')
|
|
|
+ }
|
|
|
+ if (openRemoteReqData || openRemoteQuery) {
|
|
|
+ if (!requestConfig) {
|
|
|
+ console.error('请输入请求配置信息')
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!openRemoteReqData) {
|
|
|
+ if (!dataList) {
|
|
|
+ console.error('请至少确保一个数据源')
|
|
|
+ }
|
|
|
+ if (openRemoteQuery) {
|
|
|
+ console.error('远程查询无效,请开启数据远程请求')
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
// 定义暴露出去的方法
|
|
|
defineExpose({
|
|
|
getData,
|
|
@@ -450,7 +551,7 @@ defineExpose({
|
|
|
onMounted(() => {
|
|
|
initPageConfig()
|
|
|
initReqConfig()
|
|
|
- loading.value = props.loadingState
|
|
|
+ checkPropsConfig()
|
|
|
})
|
|
|
</script>
|
|
|
|
|
@@ -483,11 +584,15 @@ onMounted(() => {
|
|
|
<div class="tableBox">
|
|
|
<!-- 没有分页的时候需要重新计算一下data -->
|
|
|
<el-table
|
|
|
- :data="openRemoteQuery ? tableData[paginationConfig.currentPage] : tableDataNoPaging"
|
|
|
+ :data="
|
|
|
+ openRemoteReqData && openRemoteQuery
|
|
|
+ ? tableData[paginationConfig.currentPage]
|
|
|
+ : localTableData
|
|
|
+ "
|
|
|
style="width: 100%"
|
|
|
class="tableBody"
|
|
|
:cell-style="tableCellStyle"
|
|
|
- v-loading="loading"
|
|
|
+ v-loading="openRemoteReqData ? loading : props.loadingState"
|
|
|
:row-key="createRowKey()"
|
|
|
@sort-change="tableSortChange"
|
|
|
@query="throttleGetData"
|