| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- <template>
- <div class="tablePaganations">
- <a-config-provider :locale="locale">
- <a-table
- class="ant-table-striped"
- bordered
- size="small"
- height="1000px"
- :loading="isLoading"
- :data-source="dataSource"
- :columns="columns"
- :scroll="{ y: 350 }"
- :pagination="pagination"
- :row-key="(record) => record.id"
- :row-selection="
- isSelect
- ? {
- selectedRowKeys: state.selectedRowKeys,
- onSelect: selectEvent,
- onSelectAll: selectAllEvent,
- }
- : null
- "
- :row-class-name="
- (_record, index) => (index % 2 === 1 ? 'table-striped' : null)
- "
- >
- <template
- v-for="(item, index) in renderArr"
- #[item]="scope"
- :key="index"
- >
- <slot :name="item" :scope="scope" v-bind="scope || {}" />
- </template>
- </a-table>
- </a-config-provider>
- </div>
- </template>
-
- <script setup>
- import {
- useSlots,
- ref,
- reactive,
- defineProps,
- defineEmits,
- watch,
- onMounted,
- defineExpose,
- } from 'vue';
- import zhCN from 'ant-design-vue/lib/locale-provider/zh_CN';
- const locale = ref(zhCN);
- const props = defineProps({
- dataSource: {
- type: Object,
- required: true,
- },
- columns: {
- type: Object,
- required: true,
- },
- isSelect: {
- type: Boolean,
- },
- isLoading: {
- type: Boolean,
- },
- total: {
- type: Number,
- default: 0,
- },
- });
- // 选择数据
- const state = reactive({
- selectedRows: [],
- selectedRowKeys: [],
- });
- // 分页配置
- const pagination = reactive({
- showQuickJumper: true,
- current: 1,
- pageSize: 20, // 默认每页显示数量
- showSizeChanger: true, // 显示可改变每页数量
- pageSizeOptions: ['20', '50', '100', '200', '300', '500'], // 每页数量选项值
- showTotal: (total, range) =>
- range[0] + '-' + range[1] + '条' + ' 共' + total + '条', // 显示总数
- onShowSizeChange: (current, pageSize) => showSizeChange(current, pageSize),
- onChange: (current, pageSize) => changePage(current, pageSize), //点击页码事件
- total: props.total,
- });
- const emit = defineEmits(['pageParams', 'selectColumn']);
- // 改变每页数量时更新显示
- const showSizeChange = (current, pageSize) => {
- setTimeout(() => {
- pagination.current = 1;
- emit('pageParams', pagination.current, pageSize);
- });
- pagination.pageSize = pageSize;
- };
- //点击页码事件
- const changePage = (current, size) => {
- pagination.current = current;
- emit('pageParams', pagination.current, size);
- };
- // 选择每一项操作
- const selectEvent = (record, selected, selectedRows) => {
- if (selected) {
- state.selectedRows.push(record);
- state.selectedRowKeys.push(record.id);
- } else {
- let index = state.selectedRowKeys.indexOf(record.id);
- if (index >= 0) {
- state.selectedRows.splice(index, 1);
- state.selectedRowKeys.splice(index, 1);
- }
- }
- emit('selectColumn', state.selectedRows);
- };
- // 全选按钮操作
- const selectAllEvent = (selected, selectedRows, changeRows) => {
- if (selected) {
- state.selectedRows = state.selectedRows.concat(changeRows);
- state.selectedRows.forEach(item => {
- state.selectedRowKeys.push(item.id);
- });
- } else {
- changeRows.forEach(item => {
- state.selectedRows.forEach(row => {
- if (item.id == row.id) {
- state.selectedRows.splice(0, 1);
- }
- });
- let index = state.selectedRowKeys.indexOf(item.id);
- if (index >= 0) {
- state.selectedRows.splice(index, 1);
- state.selectedRowKeys.splice(index, 1);
- }
- });
- }
- emit('selectColumn', state.selectedRows);
- };
- // 清空选择
- const clear = () => {
- state.selectedRowKeys = [];
- state.selectedRows = [];
- emit('selectColumn', state.selectedRows);
- };
- // 回到第一页
- const backFirstPage = () => {
- pagination.current = 1;
- emit('pageParams', pagination.current,pagination.pageSize);
- };
- defineExpose({ clear,backFirstPage });
- // 监听total变化
- watch(
- props,
- newData => {
- pagination.total = newData.total;
- },
- { immediate: true, deep: true },
- );
- // 插槽的实例
- const slots = useSlots();
- const renderArr = Object.keys(slots);
- </script>
- <style scoped>
- .tablePaganations {
- width: 100%;
- margin-top: 20px;
- margin-bottom: 20px;
- }
- .ant-table-striped :deep(.table-striped) td {
- background-color: #fafafa;
- }
- </style>
|