web
2025-03-26 98247bb0b4da9a322c97867262c1a76c53ace520
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<script setup>
/**
 * getList 获取列表
 * headList 列定义
 *          order 是否排序
 *          orderFun: function (a, b){ return Number(a.code) > Number(b.code) ? 1 : -1 }  排序函数
 *          fixed 是否固定列
 *          filters 列过滤
 *          filterFun 过滤函数
 *          slot 自定义列, row为当前列插槽传参, 接收 <template #code="scope"> </template>, code为插槽名
 * select 是否可选择
 *
 */
import {onMounted, ref} from "vue";
 
const props = defineProps({
    searchData: {
        type: Object,
        default: () => {}
    },
    getList: {
        type: Function,
        default: () => {},
        required: true
    },
    headList: {
        type: Array,
        default: [],
        required: true
    },
    select: {
        type: Boolean,
        default: false
    }
})
 
const state = reactive({
    page: 1,
    limit: 10,
    total: 0,
    list: []
})
const loading = ref(false);
 
// 默认第一页
const getData = (data={ page: state.page }) => {
    const pagedata = { limit: state.limit, page: data.page, ...props.searchData }
    loading.value = true;
    props.getList(pagedata).then(res => {
        state.list = res.data.list
        state.total = res.data.total
    }).finally(() => {
        loading.value = false;
    })
}
 
// 分页按钮
const paginationFun = (data) => {
    state.page = data.page
    getData(data)
}
 
onMounted(() =>{
    getData({ page: state.page })
})
 
// 将方法抛出去,外面可以调用
defineExpose({
    state,
    getData
})
</script>
 
<template>
    <div style="width: 100%; height: 100%">
        <el-table :data="state.list"
                  v-loading="loading"
                  ref="multipleTable"
                  style="width: 100%; height: 85%">
            <el-table-column v-if="props.select" type="selection"></el-table-column>
            <el-table-column
                type="index"
                label="序号"
                align="center"
                width="80px"
            ></el-table-column>
            <template v-for="item in props.headList">
                <!-- 排序列 -->
                <el-table-column
                    v-if="item.order"
                    :prop="item.prop"
                    :label="item.label"
                    :sortable="item?.order"
                    :sort-method="item?.orderFun"
                />
                <!-- 正常列 -->
                <el-table-column
                    v-else
                    :prop="item.prop"
                    :label="item.label"
                    :fixed="item?.fixed"
                    :filters="item?.filters"
                    :filter-method="item?.filterFun"
                >
                    <template #default="scope">
                        <!-- 自定义列 -->
                        <template v-if="item?.slot">
                            <slot :name="item.prop" :row="scope.row"></slot>
                        </template>
                    </template>
                </el-table-column>
            </template>
        </el-table>
        <div class="pagination" v-show="state.total > 0">
            <div class="pagination-total">共{{state.total}}条</div>
            <pagination
                layout="prev, pager, next, jumper"
                :total="state.total"
                :page="state.page"
                :limit="state.limit"
                @pagination="paginationFun"
            />
            <!-- 页码右侧自定义插槽,可以加自定义按钮 -->
            <slot name="pagination"></slot>
        </div>
    </div>
</template>
 
<style scoped lang="scss">
.pagination{
    display: flex;
    align-items: center;
    justify-content: flex-end;
    .pagination-total{
        color: #fff;
    }
}
:deep(.el-table--default .el-table__cell){
    padding: 12px 0;
}
:deep(.pagination-container){
    background-color: transparent;
    margin: 0;
    padding: 20px;
}
</style>