vue3.2二次封装antd vue 中的Table组件(基础封装)

主要是针对vue3.2中的<script setup>语法

这次这个项目使用的是一个 vue3.2+vite+antd vue

因为这个项目中多处使用到表格组件,所以进行了一个基础的封装,主要是通过antd vue 中表格的slots配置项,通过配合插槽来进行封装自定义表格;

子组件的代码

<template>
  <!-- 表格组件 -->
  <a-table
    :dataSource="dataSource" //通过父组件传递过来的表格数据
    :columns="columns"  // 表头配置通过这来渲染表格(重要)
    :pagination="false" // 是否需要分页,ant表格自带的,里选了fasle(不重要)
    :row-selection="isSelect ? rowSelection1 : null" //是否需要表格前面的选择按钮
  >
    <template v-slot:[item]="scope" v-for="item in renderArr">
       // 插槽后面-slot:[item] 如果是动态渲染插槽需要使用这种方式
       //renderArr 是父组件传递过来的用了插槽的名字,等下会在父组件中讲清楚,通过这个数组来遍历生成插槽
      <slot :name="item" :scope="scope"></slot> //再通过这个插槽传递数据给父组件,做一些编辑提交的操作等等
    </template>
  </a-table>
</template>

<script lang="ts" setup>
// 把setup放在 script 标签里面,相当于 setup()语法糖, 具体可以看vue的官网
import { useSlots } from 'vue' 
//如果要知道使用插槽的实例需要引入 useSlots

defineProps({
  dataSource: Object, 
  columns: Object,
  isSelect: Boolean
})
// defineProps是3.2中新的语法,不需要从 vue里面引入出来

const slots = useSlots()
// 插槽的实例


const renderArr = Object.keys(slots)
// 渲染的数据

const emits = defineEmits(['batch'])

// 是否多选
const rowSelection1 = {
  onChange: (selectedRowKeys: any, selectedRows: any) => {
    console.log(selectedRowKeys, selectedRows)
    emits('batch', selectedRowKeys)
  },
  getCheckboxProps: (record: any) => ({
    disabled: record.name === 'Disabled User', // Column configuration not to be checked
    name: record.name
  })
}
</script>

里面最主要的就是通过useSlots来知道父组件在使用表格组件的时候使用了几个插槽,通过返回的这个对象来通过Object.Keys 来遍历渲染生成新的插槽.

父组件的使用

<template>
 <MyTable :columns="columns" :data-source="dataSource">
        <!-- 通过columns 里面对象来遍历生成 可编辑的组件, 不能编辑序号是因为是因为没有传过去slots , 所以及时columns里面包含序号,但是由于表格组件渲染插槽没有他,所以不会序号不可编辑,通过给操作自定义一个属性,来避免渲染生成操作-->
            
        <template v-slot:[item.dataIndex]="{ scope }" v-for="item in columns">
           // 通过v-for生成 因为每个选项都需要变成input框所以用遍历,但是操作一列有自己的方式
所以不需要,于是我就在操作一列自己加了一个isSlot,表示他不一样
          <div :key="item.dataIndex" v-if="!item.isSlot">
            <span v-if="!scope.record.isEdit">{{ scope.text }} </span>
            <a-input
              size="small"
              v-else
              v-model:value="scope.record[item.dataIndex]"
            ></a-input>
          </div>
            // 操作不需要上面的方式 这是操作自己的方式
          <div v-else>
            <a-button type="text" @click="alertDet(scope.record)"
              >详情</a-button
            >
            <a-button
              :type="scope.record.isEdit ? 'primary' : 'text'"
              @click="editPoint(scope.record)"
              >{{ scope.record.isEdit ? '完成' : '编辑' }}</a-button
            >
            <a-popconfirm
              placement="top"
              ok-text="确认"
              cancel-text="取消"
              @confirm="del_point(scope)"
            >
              <template #title>
                <p>确定删除该扫描节点?</p>
              </template>
              <a-button type="text">删除</a-button>
            </a-popconfirm>
          </div>
        </template>
      </MyTable>
</template>
<script setup>

const dataSource = ref([])
const columns = [
  {
    title: '序号',
    dataIndex: 'numbers',
    key: 'numbers',
    width: '6%'
  },
  {
    title: '资源名称',
    dataIndex: 'ResourceName',
    slots: { customRender: 'ResourceName' }, //slots这个是重点,通过这个相当于告诉表格组件我有一个具名插槽要用,名字就是customRender后面的名字, 父组件中的useSlots插槽的实例就有这个ResourceName,下面也一样
    width: '12%'
  },
  {
    title: '资源名称IP',
    dataIndex: 'IP',
    slots: { customRender: 'IP' },
    width: '12%'
  },
  {
    title: '数据库类型',
    dataIndex: 'DatabaseType',
    slots: { customRender: 'DatabaseType' },
    width: '12%'
  },
  {
    title: '数据库名',
    dataIndex: 'Dbname',
    slots: { customRender: 'Dbname' },
    width: '12%'
  },
  {
    title: '用户名',
    dataIndex: 'Username',
    slots: { customRender: 'Username' },
    width: '12%'
  },
  {
    title: '端口',
    dataIndex: 'Port',
    slots: { customRender: 'Port' },
    width: '12%'
  },
  {
    title: '操作',
    isSlot: true,
    dataIndex: 'operation',
    slots: { customRender: 'operation' }
  }
]
</script>

这次主要的一个功能是编辑之后变成input框 修改了之后变成完成发送请求重新渲染表格

 

  • 7
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
根据引用内容,Vue3.2封装一个组件库的过程包括以下步骤: 1. 组件的项目初建: 1-1. 创建项目; 1-2. 在`packages/button`目录下创建`index.js`文件; 1-3. 在`packages/button`目录下创建`index.vue`文件; 1-4. 在`packages`目录下创建`index.js`文件; 1-5. 在`App.vue`使用组件; 1-6. 配置`package.json`; 1-7. 配置`main.js`和`vite.config.ts`。 2. 组件库之button组件: 2-1. 在`packages/button`目录下创建`index.vue`文件; 2-2. 在需要使用button组件的地方使用该组件。 3. 组件库之input组件: 3-1. 在`packages/input`目录下创建`index.js`文件; 3-2. 在需要使用input组件的地方使用该组件。 4. 发布npm组件库: - 切换npm镜像为淘宝镜像:`npm config set registry https://registry.npm.taobao.org/`; - 登录npm账号:`npm login`; - 执行打包命令:`npm run build`; - 发布组件库:`npm publish`。 在Vue项目的`main.js`文件,可以引入组件库并使用: ```javascript import "./assets/main.css"; import TUI from "xzl-vue-ui"; const app = createApp(App); app.use(TUI); app.mount("#app"); ``` 通过以上步骤,你可以完成Vue3.2封装组件库的过程,并可以在项目使用该组件库。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [vue3.2从0-1封装一个组件库 ( 组件项目的创建 - 发布npm - 使用组件库 )](https://blog.csdn.net/weixin_43845137/article/details/127256751)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [vue3.2二次封装antd vue Table组件(基础封装)](https://blog.csdn.net/m0_57004866/article/details/120419733)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值