无木
2021-09-15 84c7d516df05cc158389d42c7626a16559186610
src/components/Table/src/BasicTable.vue
@@ -4,14 +4,13 @@
      submitOnReset
      v-bind="getFormProps"
      v-if="getBindValues.useSearchForm"
      :submitButtonOptions="{ loading: getLoading }"
      :tableAction="tableAction"
      @register="registerForm"
      @submit="handleSearchInfoChange"
      @advanced-change="redoHeight"
    >
      <template #[replaceFormSlotKey(item)]="data" v-for="item in getFormSlotKeys">
        <slot :name="item" v-bind="data"></slot>
        <slot :name="item" v-bind="data || {}"></slot>
      </template>
    </BasicForm>
@@ -23,7 +22,7 @@
      @change="handleTableChange"
    >
      <template #[item]="data" v-for="item in Object.keys($slots)" :key="item">
        <slot :name="item" v-bind="data"></slot>
        <slot :name="item" v-bind="data || {}"></slot>
      </template>
      <template #[`header-${column.dataIndex}`] v-for="column in columns" :key="column.dataIndex">
@@ -33,13 +32,20 @@
  </div>
</template>
<script lang="ts">
  import type { BasicTableProps, TableActionType, SizeType } from './types/table';
  import type {
    BasicTableProps,
    TableActionType,
    SizeType,
    ColumnChangeParam,
  } from './types/table';
  import { defineComponent, ref, computed, unref, toRaw } from 'vue';
  import { defineComponent, ref, computed, unref, toRaw, inject, watchEffect } from 'vue';
  import { Table } from 'ant-design-vue';
  import { BasicForm, useForm } from '/@/components/Form/index';
  import { PageWrapperFixedHeightKey } from '/@/components/Page';
  import expandIcon from './components/ExpandIcon';
  import HeaderCell from './components/HeaderCell.vue';
  import { InnerHandlers } from './types/table';
  import { usePagination } from './hooks/usePagination';
  import { useColumns } from './hooks/useColumns';
@@ -54,11 +60,12 @@
  import { createTableContext } from './hooks/useTableContext';
  import { useTableFooter } from './hooks/useTableFooter';
  import { useTableForm } from './hooks/useTableForm';
  import { useExpose } from '/@/hooks/core/useExpose';
  import { useDesign } from '/@/hooks/web/useDesign';
  import { omit } from 'lodash-es';
  import { basicProps } from './props';
  import { isFunction } from '/@/utils/is';
  import { warn } from '/@/utils/log';
  export default defineComponent({
    components: {
@@ -82,12 +89,14 @@
      'edit-row-end',
      'edit-change',
      'expanded-rows-change',
      'change',
      'columns-change',
    ],
    setup(props, { attrs, emit, slots }) {
      const tableElRef = ref<ComponentRef>(null);
    setup(props, { attrs, emit, slots, expose }) {
      const tableElRef = ref(null);
      const tableData = ref<Recordable[]>([]);
      const wrapRef = ref<Nullable<HTMLDivElement>>(null);
      const wrapRef = ref(null);
      const innerPropsRef = ref<Partial<BasicTableProps>>();
      const { prefixCls } = useDesign('basic-table');
@@ -95,6 +104,15 @@
      const getProps = computed(() => {
        return { ...props, ...unref(innerPropsRef) } as BasicTableProps;
      });
      const isFixedHeightPage = inject(PageWrapperFixedHeightKey, false);
      watchEffect(() => {
        unref(isFixedHeightPage) &&
          props.canResize &&
          warn(
            "'canResize' of BasicTable may not work in PageWrapper with 'fixedHeight' (especially in hot updates)",
          );
      });
      const { getLoading, setLoading } = useLoading(getProps);
@@ -117,10 +135,15 @@
      } = useRowSelection(getProps, tableData, emit);
      const {
        handleTableChange,
        handleTableChange: onTableChange,
        getDataSourceRef,
        getDataSource,
        getRawDataSource,
        setTableData,
        updateTableDataRecord,
        deleteTableDataRecord,
        insertTableDataRecord,
        findTableDataRecord,
        fetch,
        getRowKey,
        reload,
@@ -136,8 +159,16 @@
          getFieldsValue: formActions.getFieldsValue,
          clearSelectedRowKeys,
        },
        emit
        emit,
      );
      function handleTableChange(...args) {
        onTableChange.call(undefined, ...args);
        emit('change', ...args);
        // 解决通过useTable注册onChange时不起作用的问题
        const { onChange } = unref(getProps);
        onChange && isFunction(onChange) && onChange.call(undefined, ...args);
      }
      const {
        getViewColumns,
@@ -153,7 +184,7 @@
        tableElRef,
        getColumnsRef,
        getRowSelectionRef,
        getDataSourceRef
        getDataSourceRef,
      );
      const { customRow } = useCustomRow(getProps, {
@@ -168,28 +199,34 @@
      const { getExpandOption, expandAll, collapseAll } = useTableExpand(getProps, tableData, emit);
      const { getHeaderProps } = useTableHeader(getProps, slots);
      const handlers: InnerHandlers = {
        onColumnsChange: (data: ColumnChangeParam[]) => {
          emit('columns-change', data);
          // support useTable
          unref(getProps).onColumnsChange?.(data);
        },
      };
      const { getHeaderProps } = useTableHeader(getProps, slots, handlers);
      const { getFooterProps } = useTableFooter(
        getProps,
        getScrollRef,
        tableElRef,
        getDataSourceRef
        getDataSourceRef,
      );
      const {
        getFormProps,
        replaceFormSlotKey,
        getFormSlotKeys,
        handleSearchInfoChange,
      } = useTableForm(getProps, slots, fetch);
      const { getFormProps, replaceFormSlotKey, getFormSlotKeys, handleSearchInfoChange } =
        useTableForm(getProps, slots, fetch, getLoading);
      const getBindValues = computed(() => {
        const dataSource = unref(getDataSourceRef);
        let propsData: Recordable = {
          size: 'middle',
          // ...(dataSource.length === 0 ? { getPopupContainer: () => document.body } : {}),
          ...attrs,
          customRow,
          expandIcon: expandIcon(),
          expandIcon: slots.expandIcon ? null : expandIcon(),
          ...unref(getProps),
          ...unref(getHeaderProps),
          scroll: unref(getScrollRef),
@@ -199,7 +236,7 @@
          rowKey: unref(getRowKey),
          columns: toRaw(unref(getViewColumns)),
          pagination: toRaw(unref(getPaginationInfo)),
          dataSource: toRaw(unref(getDataSourceRef)),
          dataSource,
          footer: unref(getFooterProps),
          ...unref(getExpandOption),
        };
@@ -207,8 +244,7 @@
          propsData = omit(propsData, 'scroll');
        }
        propsData = omit(propsData, 'class');
        propsData = omit(propsData, ['class', 'onChange']);
        return propsData;
      });
@@ -244,11 +280,16 @@
        deleteSelectRowByKey,
        setPagination,
        setTableData,
        updateTableDataRecord,
        deleteTableDataRecord,
        insertTableDataRecord,
        findTableDataRecord,
        redoHeight,
        setSelectedRowKeys,
        setColumns,
        setLoading,
        getDataSource,
        getRawDataSource,
        setProps,
        getRowSelection,
        getPaginationRef: getPagination,
@@ -267,7 +308,7 @@
      };
      createTableContext({ ...tableAction, wrapRef, getBindValues });
      useExpose<TableActionType>(tableAction);
      expose(tableAction);
      emit('register', tableAction, formActions);
@@ -283,7 +324,7 @@
        wrapRef,
        tableAction,
        redoHeight,
        getFormProps,
        getFormProps: getFormProps as any,
        replaceFormSlotKey,
        getFormSlotKeys,
        getWrapperClass,
@@ -297,27 +338,30 @@
  @prefix-cls: ~'@{namespace}-basic-table';
  [data-theme='dark'] {
    .ant-table-tbody > tr:hover.ant-table-row-selected > td,
    .ant-table-tbody > tr.ant-table-row-selected td {
      background-color: #262626;
    }
  }
  .@{prefix-cls} {
    max-width: 100%;
    &-row__striped {
      td {
        background-color: @app-content-background;
      }
    }
    &-form-container {
      padding: 16px;
      .ant-form {
        padding: 12px 10px 6px 10px;
        margin-bottom: 16px;
        background: #fff;
        border-radius: 4px;
      }
    }
    &-row__striped {
      td {
        background: #fafafa;
      }
    }
    &--inset {
      .ant-table-wrapper {
        padding: 0;
        background-color: @component-background;
        border-radius: 2px;
      }
    }
@@ -327,10 +371,11 @@
    .ant-table-wrapper {
      padding: 6px;
      background: #fff;
      background-color: @component-background;
      border-radius: 2px;
      .ant-table-title {
        min-height: 40px;
        padding: 0 0 8px 0 !important;
      }
@@ -339,7 +384,6 @@
      }
    }
    //
    .ant-table {
      width: 100%;
      overflow-x: hidden;
@@ -352,9 +396,9 @@
        align-items: center;
      }
      .ant-table-tbody > tr.ant-table-row-selected td {
        background: fade(@primary-color, 8%) !important;
      }
      //.ant-table-tbody > tr.ant-table-row-selected td {
      //background-color: fade(@primary-color, 8%) !important;
      //}
    }
    .ant-pagination {
@@ -374,12 +418,18 @@
      .ant-table-body {
        overflow-x: hidden !important;
        overflow-y: scroll !important;
        //  overflow-y: scroll !important;
      }
      td {
        padding: 12px 8px;
      }
    }
    &--inset {
      .ant-table-wrapper {
        padding: 0;
      }
    }
  }
</style>