vue+element-ui的table组件二次封装( 新增TTable 字典过滤功能/单选框新增取消选中功能(再次点击取消选中))

1. 简介

HTML一行代码,可以实现表格编辑/分页/表格内/外按钮操作/行内文字变色/动态字典展示/filters格式化数据/排序/显示隐藏表格内操作按钮等

点击查看文档实例demo

2022-08-24 TTable组件单选框新增取消选中功能(再次点击取消选中)

2022-08-19 新增TTable 字典过滤功能

在这里插入图片描述

代码示例:

     <t-table
          :table="table"
          :columns="columns"
          @size-change="handlesSizeChange"
          @page-change="handlesCurrentChange"
        />

2、events 其他事件按照el-table直接使用(如sort-change排序事件)

事件名说明返回值
page-change当前页码当前选中的页码
add新增按钮-
save保存按钮编辑后的所有数据
handleEvent单个输入触发事件configEdit 中的 event 值和对应输入的 value 值

Methods 方法

事件名说明参数
clearSelection用于多选表格,清空用户的选择-
save保存方法(返回编辑后的所有数据)-

3、配置参数(Table Attributes)

参数说明类型是否必须
table表格数据对象Object
—data展示数据Array
—toolbar表格外操作栏选中表格某行,可以将其数据传出Array
—operator表格内操作栏数据Array
-------show表格内操作栏根据状态显示Object
-------noshow表格内操作栏根据多种状态不显示Array
-------resCode表格内操作栏按钮权限资源(路由 path)String
—operatorConfig表格内操作栏样式Object
—changeColor整行文字颜色样式控制Object
—firstColumn表格首列(序号 index,复选框 selection,单选 radio)排列object
—total数据总条数Number
—pageSize页数量Number
—currentPage是否需要显示切换页条数Number
columns表头信息Array
----sort排序 (设置:sort:true)Boolean
----headerRequired是否显示表头必填’*’Boolean
----renderHeader列标题 Label 区域渲染使用的 Function(h, { column, $index }) 可以用 jsx 方式Function
----bindel-table-column AttributesObject
----noShowTip是否换行 (设置:noShowTip:true)Boolean
----filtersfilters 转义(后台返回数字转中文,时间戳转时间)Object
----customRender配置 renderObject
--------comps配置 customRenderArray
----------- comp组件标签String
----------- text组件标签文案String
----------- bind标签属性Object
----------- event标签事件,参数为点击 当前行数据Function
----------- child标签里面标签Array
----slotName插槽显示此列数据(其值是具名作用域插槽)String
----------param具名插槽获取此行数据必须用解构接收{param}Object
----canEdit是否开启单元格编辑功能Boolean
----configEdit表格编辑配置(开启编辑功能有效)Object
----------labelplaceholder 显示String
----------editComponent组件名称可直接指定全局注册的组件,也可引入’element/abtd’如:‘a-input/el-input’String
----------bind第三方 UI 的 Attributes,如 el-input 中的 clearable 清空功能Object
----------event触发 handleEvent 事件的标志String
----------type下拉或者复选框显示(select-arr/select-obj/checkbox)String
----------list下拉选择数据源名称String
----------arrLabeltype:select-arr 时对应显示的中文字段String
----------arrKeytype:select-arr 时对应显示的数字字段String
listTypeInfo下拉选择数据源Object
footer底部操作区(默认隐藏,使用插槽展示“保存”按钮)slot
isShowFooterBtn是否显示保存按钮Boolean
isEditCell是否开启编辑模式 开启 add 事件激活Boolean
isShowTips开启单元格编辑时鼠标移入是否显示“单击可编辑”tipsBoolean
columnSetting是否显示设置(隐藏/显示列)Boolean
name组件唯一标记,类似 Key 值,用于缓存组件数据String
title表格左上标题String /slot
isShowPagination是否显示分页(默认显示分页)Boolean
isTableColumnHidden是否开启合计行隐藏复选框/单选框Boolean
isMergedCell是否开启合并单元格Boolean
mergeCol第几列合并进行行合并(默认第一列)Number
isCopy是否允许双击单元格复制Boolean

4、 部分具体代码——代码较多,可以结合文档案例使用

 <template v-for="(item,index) in renderColumns">
        <el-table-column
          :key="index+'i'"
          :type="item.type"
          :label="item.label"
          :prop="item.prop"
          :min-width="item['min-width'] || item.minWidth || item.width"
          :sortable="item.sort"
          :align="item.align || 'center'"
          :show-overflow-tooltip="item.noShowTip"
          v-bind="$attrs"
          v-on="$listeners"
        >
          <template slot-scope="scope">
          <!--非编辑模式-->
            <template v-if="!isEditCell">
             <!-- render渲染 -->
              <template v-if="item.customRender">
                <OptComponent
                  v-for="(comp, i) in item.customRender.comps"
                  :key="scope.$index + i.toString()"
                  v-bind="comp"
                  :scope="scope"
                />
              </template>
              <!--自定义作用域插槽-->
              <template v-if="item.slotName">
                <slot :name="item.slotName" :param="scope"></slot>
              </template>
               <!-- 单个单元格编辑 -->
              <template v-if="item.canEdit">
                <single-edit-cell
                  :canEdit="item.canEdit"
                  :configEdit="item.configEdit"
                  v-model="scope.row[scope.column.property]"
                  v-on="$listeners"
                  v-bind="$attrs"
                  ref="editCell"
                >
                  <template v-for="(index, name) in $slots" :slot="name">
                    <slot :name="name" />
                  </template>
                </single-edit-cell>
              </template>
               <!--常见的filters(金额/百分比)-->
              <div
                v-if="!item.filters&&!item.slotName&&!item.customRender"
                :style="{color:txtChangeColor(scope)}"
              >{{scope.row[item.prop]}}</div>
              <div v-if="item.filters" :style="{color:txtChangeColor(scope)}">
              <!--常见的字典过滤-->
                <span
                  v-if="item.filters.param"
                >{{scope.row[item.prop] |constantKey2Value(item.filters.param)}}</span>
                <span
                  v-if="!item.filters.param&&item.filters.method===''"
                >{{scope.row[item.prop] |currencyFilter}}</span>
                <span
                  v-if="!item.filters.param&&item.filters.method==='%'"
                >{{scope.row[item.prop] |percentFilter}}</span>
              </div>
            </template>
            <!--开启编辑模式-->
            <template v-else>
              <edit-cell
                :configEdit="item.configEdit"
                v-model="scope.row[scope.column.property]"
                v-bind="$attrs"
                v-on="$listeners"
                ref="editCell"
              >
                <template v-for="(index, name) in $slots" :slot="name">
                  <slot :name="name" />
                </template>
              </edit-cell>
            </template>
          </template>
        </el-table-column>
      </template>

5、源码地址

gitHub组件地址

gitee码云组件地址


相关文章

基于ElementUi&Antd再次封装基础组件文档


版权声明:本文为cwin8951原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。