antd vue table控件的使用(三)

今天就讲讲Ant Design Vue下的控件----table表格(分页、编辑和删除功能)

结合项目中的需求,看看如何配置,让table即可以展示列表,又可以直接编辑数据。需求:

(1)展示即将检查的数据列表,数据量较大,要分页;

(2)当数据有错误时,点击编辑;

(3)数据改完后刷新,保存,恢复列表展示功能;

(4)出现多余的数据时,可删除;

模板中代码如下:

<template>
        <a-table
          class="ant-table-striped"
          style="width: 100%; height: 100%"
          bordered="true"
          size="small"
          :columns="cols"
          :dataSource="dataTable"
          :pagination="pagination"
          :scroll="{ x: srcollWidth, y: tableScrollY }"
          :loading="loading"
          :row-class-name="(_record, index) => (index % 2 === 1 ? 'table-striped' : null)"
          @change="pageIndexChange">
          <template #bodyCell="{ column, text, record }">
            <template v-if="!['operation', 'well_id', 'table_id', 'order_no'].includes(column.dataIndex)">
              <div>
                <a-input v-if="editableData[record.key]" v-model:value="editableData[record.key][column.dataIndex]" style="margin: -5px 0" />
                <template v-else>
                  {{ text }}
                </template>
              </div>
            </template>
            <template v-else-if="column.dataIndex === 'operation'">
              <div class="editable-row-operations">
                <span v-if="editableData[record.key]">
                  <a-typography-link @click="saveRecord(record)">保存</a-typography-link>
                  <a-popconfirm title="是否取消修改?" @confirm="cancelRecord(record.key)">
                    <a>取消</a>
                  </a-popconfirm>
                </span>
                <span v-else>
                  <a @click="editRecord(record)">修改</a> |
                  <a-popconfirm title="是否确定删除该记录?删除后无法恢复,请谨慎操作。" @confirm="deleteRecord(record)">
                    <a>删除</a>
                  </a-popconfirm>
                </span>
              </div>
            </template>
          </template>
        </a-table>
      </template>

先声明与table有关的变量

//表格设置
  const tableScrollY = 'calc(100vh - 280px)';
  const srcollWidth = '100%';
  const loading = ref(false);
  const cols: Ref<Array<any>> = ref([
    {
      title: 'XXXX列',
      dataIndex: 'XX',
      key: 'XXX',
      width: 100,
      checked: true,
    },
    {
        title: '操作',
        fixed: true,
        dataIndex: 'operation',
        width: '110px',
      }
  ]);
const dataTable: Ref<Array<any>> = ref(new Array<any>());

1.分页展示

(1)声明与分页有关的变量

//总页数
  const totalRows = ref(1);
//当前页码
  const p_curPage = ref(1);
//默认每页显示条数
  const defaultPageSize = ref(100);

(2)通过计算属性控制分页

const pagination = computed(() => ({
    total: totalRows.value,
    current: p_curPage.value,
    pageSize: defaultPageSize.value,
    showTotal: (total, range) => {
      if (range && total && range != null) {
        return `共${total}条数据,当前显示${range[0]}~${range[1]}条`;
      }
    },
  }));

(3)分页事件change(change:用于分页、排序、筛选变化时触发)

//分页
  async function pageIndexChange(e) {
    loading = true;
    p_curPage.value = e.current;
    defaultPageSize.value = e.pageSize;
    await queryTableData(activeItem.value, '');
  }
  //根据表名查询表格数据
  //tableName:表名
  async function queryTableData(tableName: string, dtoName: string) {
    dataTable.value = [];
    let wellName = '',
      wellid = '';

    let strWhere = {
      p_well_name: wellName,
      p_well_id: wellid,
      p_table_name: tableName,
      p_curPage: p_curPage.value,//往api传入当前页码
      p_pageSize: defaultPageSize.value,//每页条数
      p_totalRecords: 1,//总记录数
      p_totalPages: 1,//总页数
    };
    let queryProc = {
      whereStr: JSON.stringify(strWhere),
      packageCode: 'getdatacheck',
    };
    let res = await getCommonTableData(queryProc);//调用api
    dataTable.value = res;
    if (res.length > 0) {
      totalRows.value = res[0].total;//总记录数
    }
    loading = false;
  }

注意:dataTable中的列必须含有key这个列。如图:

分页功能实现效果如下图:

2. 实现编辑功能

(1)声明与编辑功能有关的变量

const editableData = reactive({});

(2)与编辑功能有关的事件

//编辑事件
function editRecord(record) {
    let keyId = record.key;
    editableData[keyId] = cloneDeep(record);//深拷贝
  }

3.实现保存功能

//保存事件
function saveRecord(record) {
    let keyId = record.key;
    Object.assign(dataTable.value.filter((item) => keyId === item.key)[0], editableData[keyId]);
    let updateParam = {
      tableName: activeItem.value,
      whereStr: JSON.stringify(editableData[keyId]),
    };
    updateCommonData(updateParam).then((res) => {//api
      if (res && res.length > 0) {
        message.warning(res);
      } else {
        message.info('保存成功');
      }
    });

    delete editableData[keyId];
  }
//取消事件
function cancelRecord(keyId) {
    delete editableData[keyId];//对象移除
  }

Object.assign()方法

//assign静态方法:将一个或者多个源对象中所有可枚举的自有属性复制到目标对象,并返回修改后的目标对象

const target = { a: 1, b: 2 };
const source = { b: 4, c: 5 };
const returnedTarget = Object.assign(target, source);

console.log(target);
// Expected output: Object { a: 1, b: 4, c: 5 }

console.log(returnedTarget === target);
// Expected output: true

4. 实现删除功能

function deleteRecord(record) {
    let keyId = record.key;

    let param = {
      tableName: activeItem.value,
      whereStr: JSON.stringify(record),
    };
    if (record.table_id && record.order_no) {
      deleteCommonData(param).then((res) => {//api
        if (res && res.length > 0) {
          message.warning(res);
        } else {
          message.info('删除成功');
          queryTableData(activeItem.value, '');
        }
      });
    } else {
      message.info('删除条件table_id,order_no不能为空');
    }
  }

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Ant Design 是一个非常流行的 UI 框架,它提供了很多实用的组件,包括 Table 表格组件。在 Vue 项目中使用 Ant Design 的 Table 组件时,可以考虑封装一个通用的 Table 组件,方便在不同的页面中复用。 下面是一个简单的 Ant Design Vue Table 封装: ```vue <template> <a-table :columns="columns" :dataSource="dataSource"> <a-pagination :total="total" :current="current" :pageSize="pageSize" @change="handlePageChange" /> </a-table> </template> <script> import { Table, Pagination } from 'ant-design-vue'; export default { name: 'AntdTable', components: { Table, Pagination }, props: { columns: { type: Array, required: true, }, dataSource: { type: Array, required: true, }, total: { type: Number, required: true, }, current: { type: Number, required: true, }, pageSize: { type: Number, required: true, }, }, methods: { handlePageChange(pageNumber) { this.$emit('page-change', pageNumber); }, }, }; </script> ``` 在这个组件中,我们使用了 Ant Design Vue 提供的 Table 和 Pagination 组件。组件接受了五个 props:columns、dataSource、total、current 和 pageSize,分别对应 Table 组件和 Pagination 组件的属性。在 handlePageChange 方法中,我们触发了一个自定义事件 page-change,这个事件可以在父组件中监听并处理分页请求。 使用这个组件时,只需要传入相应的 props 即可: ```vue <template> <div> <AntdTable :columns="columns" :dataSource="dataSource" :total="total" :current="current" :pageSize="pageSize" @page-change="handlePageChange" /> </div> </template> <script> import AntdTable from '@/components/AntdTable'; export default { components: { AntdTable }, data() { return { columns: [...], dataSource: [...], total: 100, current: 1, pageSize: 10, }; }, methods: { handlePageChange(pageNumber) { console.log('Page changed:', pageNumber); }, }, }; </script> ``` 这样,我们就完成了一个简单的 Ant Design Vue Table 封装。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值