首页 > 其他分享 >ElementUI——elementui2.0表格支持render渲染

ElementUI——elementui2.0表格支持render渲染

时间:2024-03-04 10:58:56浏览次数:18  
标签:status render ElementUI type elementui2.0 label data row

前言

当前项目基于vue2.0+elementui来实现的,所以简单封装一个表格支持render函数来自定义渲染;

内容

custom-column

封装自定义列的组件

var customColumn = {
  functional: true,
  render: (h, data) => {
    const params = {
      row: data.props.row,
      column: data.props.prop,
      render: data.props.render,
    };
    return params.render
      ? params.render(h, params.row)
      : h("span", params.row[params.column]);
  },
};

render

引用时的写法

{
    label: "主体资质状态",
  prop: "status",
  align: "center",
  render: (h, row) => {
    return h(
      "el-tag",
      {
        props: {
          type: TIKTOK_QUA_COLOR[row.status],
        },
      },
      TIKTOK_QUA_STATUS[row.status]
    );
  },
},

ExpandTable

完整的表单组件,同理基于封装的render,这里的组件可以进一步的进行优化;

<template>
  <div class="app-container">
    <!-- 查询表单 -->
    <el-form
      :inline="true"
      :model="formData"
      ref="ruleForm"
      v-if="formItems.length"
    >
      <el-form-item
        v-for="item in formItems"
        :label="item.label"
        :key="item.name"
      >
        <component
          :is="item.type"
          :type="item.range"
          :value-format="item.valueFormat"
          :start-placeholder="item.startPlaceholder"
          :end-placeholder="item.endPlaceholder"
          v-model="formData[item.name]"
          :placeholder="item.placeholder"
          @keyup.enter.native="submitForm"
        >
          <component
            v-for="(els, i) in item.els"
            :key="i"
            :is="els.type"
            :slot="els.slot"
            v-model="formData[els.name]"
            class="input-width"
          >
            <el-option
              v-for="op in els.options"
              :key="op.value"
              :label="op.label"
              :value="op.value"
            >
            </el-option>
          </component>

          <el-option
            v-for="op in item.options"
            :key="op.value"
            :label="op.label"
            :value="op.value"
          >
          </el-option>
        </component>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm">查询</el-button>
        <el-button @click="resetForm">重置</el-button>
        <el-button v-if="tableConfig.export" @click="handleExport">
          导出
        </el-button>
        <a id="downlink" />
      </el-form-item>
    </el-form>

    <!-- 动态表格 -->
    <el-button
      v-if="tableConfig.add.status"
      type="primary"
      class="margin-b15"
      @click="handleOperateEvent(tableConfig.add.value)"
    >
      {{ tableConfig.add.label }}
    </el-button>
    <el-table
      :data="data"
      border
      fit
      highlight-current-row
      style="width: 100%"
      :max-height="tableHeight"
    >
      <el-table-column
        v-for="column in columns"
        :type="column.type"
        :key="column.prop"
        :prop="column.prop"
        :label="column.label"
        :fixed="column.fixed"
        :width="column.width"
        :align="column.align"
      >
        <template scope="scope">
          <!-- 自定义render -->
          <custom-column
            :render="column.render"
            :row="scope.row"
            :prop="column.prop"
          />
        </template>
      </el-table-column>

      <el-table-column
        v-if="tableConfig.op.status"
        label="操作"
        align="center"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-link
            v-for="els in tableConfig.op.items"
            :key="els.value"
            :type="els.type"
            class="margin-r15"
            @click="handleOperateEvent(els.value, scope.row)"
          >
            {{ els.label }}
          </el-link>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <v-page
      style="margin-top: 15px"
      :page="page"
      @onChangePage="handlePageChange"
    />
  </div>
</template>
<script>
var customColumn = {
  functional: true,
  props: {
    row: Object,
    render: Function,
    prop: String,
  },
  render: (h, data) => {
    const params = {
      row: data.props.row,
      column: data.props.prop,
      render: data.props.render,
    };
    return params.render
      ? params.render(h, params.row)
      : h("span", params.row[params.column]);
  },
};

export default {
  props: {
    formItems: {
      type: Array,
      default: () => [],
    },
    data: Array,
    columns: Array,
    page: Object,
    tableConfig: Object,
  },
  components: { customColumn },
  data() {
    return {
      formData: {},
      tableHeight: 600,
    };
  },
  created() {
    if (window) {
      this.tableHeight = window.innerHeight - 320;
      if (this.tableHeight < 300) this.tableHeight = 300;
    }
  },
  methods: {
    handleExport() {
      this.$emit("export");
    },
    submitForm() {
      this.$emit("search", this.formData);
    },
    resetForm() {
      this.formData = {};
      this.$emit("search", this.formData);
    },
    handlePageChange(data) {
      this.$emit("page", data);
    },
    handleOperateEvent(type, data) {
      this.$emit("operate", type, data);
    },
  },
};
</script>

<style lang="scss" scoped>
.margin-r15 {
  margin-right: 15px;
}

.margin-b15 {
  margin-bottom: 15px;
}

.input-width {
  width: 88px !important;
}
</style>

使用

引用组件使用,因为组件直接在main.js中注册了,所以直接使用了

<template>
  <div class="app-container">
    <!-- 表格 -->
    <v-ExpandTable
      :data="tableData"
      :columns="columns"
      :form-items="formItems"
      :page="page"
      :table-config="tableConfig"
      @search="handleSearch"
      @page="handlePageChange"
      @export="handleExport"
      @operate="handleOperateEvent"
    />
  </div>
</template>

<script>
import {
  TIKTOK_QUA_OPTIONS,
  TIKTOK_QUA_COLOR,
  TIKTOK_QUA_STATUS,
} from "@/utils/data";
import { getQualificationList } from "@/api/dy-compete/qualification";
export default {
  data() {
    return {
      searchForm: {},
      formItems: [
        {
          type: "el-input",
          label: "用户名称",
          name: "username",
          placeholder: "请输入用户名称",
        },
        {
          type: "el-input",
          label: "代理商名称",
          name: "agent_name",
          placeholder: "请输入代理商名称",
        },
        {
          type: "el-input",
          label: "一级代理商名称",
          name: "first_agent_name",
          placeholder: "请输入一级代理商名称",
        },
        {
          type: "el-select",
          label: "主体资质状态",
          name: "status",
          placeholder: "请选择主体资质状态",
          options: TIKTOK_QUA_OPTIONS,
        },
        {
          type: "el-select",
          label: "行业资质状态",
          name: "industry_qua_status",
          placeholder: "请选择行业资质状态",
          options: TIKTOK_QUA_OPTIONS,
        },
      ],
      tableConfig: {
        type: "subject",
        op: {
          status: true,
          items: [
            {
              label: "编辑",
              value: "edit",
              type: "primary",
            },
            {
              label: "详情",
              value: "detail",
              type: "success",
            },
          ],
        },
        add: {
          status: true,
          label: "新增主体资质",
          value: "add",
        },
        export: true,
      },
      columns: [
        {
          label: "用户名称",
          prop: "username",
          align: "center",
        },
        {
          label: "代理商",
          prop: "agent_name",
          align: "center",
        },
        {
          label: "一级代理商",
          prop: "first_agent_name",
          align: "center",
        },
        {
          label: "主体资质状态",
          prop: "status",
          align: "center",
          render: (h, row) => {
            return h(
              "el-tag",
              {
                props: {
                  type: TIKTOK_QUA_COLOR[row.status],
                },
              },
              TIKTOK_QUA_STATUS[row.status]
            );
          },
        },
        {
          label: "行业资质状态",
          prop: "industry_qua_status",
          align: "center",
          render: (h, row) => {
            return h(
              "el-tag",
              {
                props: {
                  type: TIKTOK_QUA_COLOR[row.status],
                },
              },
              TIKTOK_QUA_STATUS[row.status]
            );
          },
        },
        {
          label: "拒绝理由",
          prop: "reject_reason",
          align: "center",
        },
      ],
      tableData: [],
      page: {},
    };
  },
  created() {
    this.getQualificationListInfo();
  },
  methods: {
    handleExport() {
      const head = {
        username: "用户名",
        agent_name: "代理商名称",
        first_agent_name: "一级代理商",
        status: "主体资质状态",
        industry_qua_status: "行业资质状态",
        reject_reason: "拒绝理由",
        subject: "主体资质",
        industries: "行业资质",
      };

      this.$downloadFile(
        this.outFile,
        "抖音竞价_主体资质信息",
        head,
        "/douyin/qualification/subject",
        this.searchForm,
        {},
        "SUBJECT_QUALIFICATION"
      );
    },
    handleSearch(data) {
      this.searchForm = data;
      this.getQualificationListInfo(data);
    },
    getQualificationListInfo(params) {
      getQualificationList(params).then((r) => {
        this.tableData = r.data.items;
        this.page = r.data.page;
      });
    },
    handlePageChange(data) {
      this.getQualificationListInfo({ ...data, ...this.searchForm });
    },
    handleOperateEvent(type, data) {
      console.log(type, data);
    },
  },
};
</script>
<style lang="scss" scoped></style>

渲染效果

标签:status,render,ElementUI,type,elementui2.0,label,data,row
From: https://www.cnblogs.com/wangyang0210/p/18051346

相关文章

  • uni-app的renderjs示例 | app端使用window
    原文:https://juejin.cn/post/6974552469917401125<template><viewstyle="padding-top:200rpx;"><!--这里的:change:msg旨在于监听逻辑层的msg变化,有变化就调用renderjs内容--><!--这里要注意,render。receiveMsg中的render是下面renderjs定义的module名称-->......
  • 【PR】3D Gaussian Splatting for Real-Time Radiance Field Rendering
    最近开始接触基于深度学习的渲染,记录下阅读过的论文。欢迎交流。 这篇论文的主要作者来自法国Inria(国家信息与自动化研究所)。发表在ACMTransactionsonGraphics。 本文主要介绍了一种使用辐射场(RadianceFieldmethods)进行新视角合成的方法:Gaussiansplatting(也有描述说这种......
  • react错误:Uncaught Error: Too many re-renders. React limits the number of renders
    react错误:UncaughtError:Toomanyre-renders.Reactlimitsthenumberofrenderstopreventaninfiniteloop. 信铁寒胜:更改页面数据时未放到useEffect方法内,导致页面一直在刷新。  原因1:错误写法:<divclassName='article_item'onClick={toArticleDetail......
  • three.js使用 CSS2DRenderer
    导入 import{CSS2DRenderer,CSS2DObject}from'three/examples/jsm/renderers/CSS2DRenderer';functioncreateLableObj(text,vector){letlaberDiv=document.createElement('div');//创建div容器laberDiv.className='laber_name......
  • 【libGDX】使用ShapeRenderer绘制几何图形
    1ShapeRenderer简介​ShapeRenderer是libGDX中用于绘制基本形状的工具之一。它可以绘制点、线、矩形、多边形、圆形、椭圆形、扇形、立方体、圆锥体等几何图形。这对于在游戏或图形应用程序中绘制简单的形状是很有用的。​ShapeRenderer的主要方法如下:​1)开......
  • nativeUI页面table列表显示,render渲染函数
    {key:'type',title:$t('cmdType'),width:150,align:'center',render(t){switch(t.type){case2:returnh('span',{......
  • 【踩坑】Unity Android(安卓)平台 Render texture 有残留,即使调用 DiscardContents 还
    在编辑器环境下OK,打包成PC客户端也OK,但是打包成apk,在安卓手机上运行的时候就会有残留。 我的代码如下。主要是在LateUpdate()中,修改特定摄像机的cullmask,在捕捉到制定的rendertexture上(即此处的m_levelUnitRT)。 voidLateUpdate(){if(m_levelU......
  • Vulkan学习苦旅06:创建渲染通道(VkRenderPass)
    对于一个复杂的图形应用程序,需要多个过程的配合,以生成图像的各个部分。通常,各个过程间存在着依赖关系,例如某个过程生成的图像(输出)被另一个过程使用(作为此过程的输入)。在Vulkan中,每个过程被称为一个子通道(subpass),所有的子通道构成了一个渲染通道(VkRenderPass).在这篇博客中,我......
  • elementUI表格滚动条样式修改,隐藏表格右边留白
    修改滚动条样式//设置滚动条的宽度.el-table__body-wrapper::-webkit-scrollbar{width:4px;}//设置滚动条的背景色和圆角.el-table__body-wrapper::-webkit-scrollbar-thumb{background-color:#535353;-webkit-box-shadow:inset005pxrgba(0,0,0,0.2......
  • 通过CanvasRenderer.SetColor和Image.color修改UI组件颜色的区别
    1)通过CanvasRenderer.SetColor和Image.color修改UI组件颜色的区别2)OPPO相关机型没法在Unity启用90或120FPS3)手机输入法中的emoji4)UnityApplicationPatching怎么用这是第369篇UWA技术知识分享的推送,精选了UWA社区的热门话题,涵盖了UWA问答、社区帖子等技术知识点,助力大家更全面......