vue列表简单封装

这篇博客介绍了一种使用Vue.js封装表格组件的方法,该组件支持多表控制,包括数据加载、默认排序、多选操作以及自定义列显示。通过监听`selection-change`和`current-change`事件来处理选择和当前行变化,同时提供了分页功能。文章还展示了具体的模板和脚本代码,包括数据转换和过滤方法的实现。

Vue封装table实现功能多表控制

封装
一、列表

<template>
  <div>
    <!--  selectList定义接收字段  tableList获取数据 defaultSort定义默认排序 handleSelectionChange多选方法-->
    <el-table :data="tableList" :default-sort="defaultSort||''"
              @selection-change="handleSelectionChange"
              @current-change="handleSelectionChange"
              class="table"
              border
              width="100%"
              row-key="id"
              :tree="{id:'id',children:'children'}">
      <!--定义序号-->
      <el-table-column width="68" align="center" label="序号">
        <template slot-scope="scope">
          <div v-if="Object.keys(scope.row).length">
            {{scope.$index + 1 + (params.pageSize * (params.pageNum - 1) || 0) }}
          </div>
        </template>
      </el-table-column>
      <!--多选框-->
      <el-table-column align="center" type="selection" width="55">
      </el-table-column>
      <el-table-column v-for="(item,index) in selectList" v-if="item.show" :key="index" :sortable="item.sorBooleam"
                       :prop="item.key"
                       :label="item.label" :width="item.width" min-width="100" :show-overflow-tooltip="true"
                       :filters="item.filter"
                       :filter-method="item.filters ? filterHandler :null">
        <!--   定义接收数据转换展示     -->
        <template slot-scope="scope">
          {{item.fn ? item.fn(scope) : scope.row[item.key]}}
        </template>

      </el-table-column>
      <slot></slot>
    </el-table>
    <!-- 分页 -->
    <pagination class="pagination" :total="total" :page.sync="params.pageNum" :limit.sync="params.pageSize"
                @pagination="getList"/>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        params: {
          pageNum: 1,
          pageSize: 20
        },
      }
    },
    mounted() {
      this.params = this.queryParams || this.params;
    },
    //注册
    props: ['tableList', 'selectList', "defaultSort", "baseIndex", 'total'],
    methods: {
      handleSelectionChange(select) {
        console.log(select);
        this.$emit("select-change", select);
      },
      getList() {
        this.$emit("queryStable");
      },
      filterHandler(value, row, column) {
        console.log(value, row, column)
        const property = column['property'];
        return row[property] === value;
      }
    }
  }
</script>

<style lang="scss" scoped>
  .table {
    width: 100%;
  }

  /deep/ .el-table--border td:first-child .cell {
    display: flex;
    align-items: center;
    justify-content: center;
  }
</style>

Vue 3 组件封装有多种方式,下面提供两个简单示例。 ### 自定义按钮组件 ```vue <template> <div class="custom-button"> <button @click="handleClick">{{ buttonText }}</button> </div> </template> <script> import { defineComponent } from 'vue'; export default defineComponent({ props: { buttonText: { type: String, default: 'Click Me' } }, emits: ['button-clicked'], setup(props, { emit }) { const handleClick = () => { emit('button-clicked'); }; return { handleClick }; } }); </script> <style scoped> .custom-button { margin: 10px; } </style> ``` 在这个示例中,使用 `defineComponent` 定义了一个自定义按钮组件。通过 `props` 接收父组件传递的 `buttonText`,并通过 `emits` 定义了可触发的事件 `button-clicked`。在 `setup` 函数中处理按钮点击事件并触发自定义事件 [^1]。 ### 自定义模态框组件 ```vue <template> <div ref="myModal" class="custom-modal"></div> <a-modal v-model:visible="visible" centered :getContainer="() => $refs.myModal" :style="{ width: '560px'}" destroyOnClose v-bind="$attrs"> <template v-for="(_val, name) in $slots" #[name]="ops"> <slot :name="name" v-bind="ops || {}"></slot> </template> </a-modal> </template> <script setup> import { ref } from 'vue'; const visible = ref(false); defineExpose({ visible }); </script> <style lang="less" scoped> .custom-modal { /* style */ } </style> ``` 此示例封装了一个自定义模态框组件,使用 `ref` 来控制模态框的显示与隐藏,通过 `defineExpose` 暴露 `visible` 变量供父组件使用。同时,使用 `v-bind="$attrs"` 传递父组件的属性,通过 `v-for` 遍历 `$slots` 实现插槽内容的分发 [^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值