当前位置: 首页 > wzjs >正文

外贸做网站公司网络策划与营销

外贸做网站公司,网络策划与营销,美国做3d+h动画的网站,农产品网站建设策划书范文文章目录 前言CustomTable如何使用tableColumn 属性h函数创建原生元素创建组件动态生成 前言 elementui中的table组件,表格中想要自由地渲染内容,是一种比较麻烦的事情,比如你表格中想要某一列插入一个button按钮,是不是要用插槽…

文章目录

    • 前言
    • CustomTable
    • 如何使用
    • tableColumn 属性
    • h函数
      • 创建原生元素
      • 创建组件
      • 动态生成

前言

elementui中的table组件,表格中想要自由地渲染内容,是一种比较麻烦的事情,比如你表格中想要某一列插入一个button按钮,是不是要用插槽,那下一次某一列插入一个图片,又得新开一种插槽或者类别。

那么,有没有什么方法,能够通过配置,直接配置一个组件的方式,让表格的那一列直接渲染对应的组件。elementui table中并不提供这样的配置。所以需要开发人员自己封装。

CustomTable

在element UI 中Table组件的基础上,封装一个可以自定义渲染的table

<template><!-- 表格 --><el-table:data="tableData"v-loading="loading":empty-text="'暂无数据'"v-bind="$attrs"><!-- :border="true" --><template v-for="column in tableColumn" :key="column?.label"><el-table-column v-if="column.cols" v-bind="{ ...column }"><!-- 列名合并,存在cols的情况下 --><template v-for="col in column.cols" :key="column?.label + col?.label"><el-table-column v-bind="{ ...col }"><template #default="scope"><!-- 自定义render --><span v-if="col?.render" :class="col?.class" :style="col?.style ? col.style : {}">{{ col?.render(scope.row[col.prop], scope.row) }}</span><!-- 自定义render component 将当前列的数据和这一整行的数据都传给component函数 --><div v-else-if="col?.component" v-html="renderVNode(col.component(scope.row[col.prop], scope.row))"> </div><spanv-else:style="col?.style ? col.style : {}":class="col?.class":title="scope.row[col.prop]":data-message="scope.row[col.prop]">{{ scope.row[col.prop] }}</span></template></el-table-column></template></el-table-column><el-table-column v-else v-bind="{ ...column }"><template #default="scope"><!-- 自定义render字符串 当前列的数据和这一整行的数据都传给render函数--><span v-if="column?.render" :style="column?.style ? column.style : {}" :class="column?.class">{{ column?.render(scope.row[column.prop], scope.row) }}</span><!-- 自定义render component 将当前列的数据和这一整行的数据都传给component函数 --><div v-else-if="column?.component" v-html="renderVNode(column.component(scope.row[column.prop], scope.row))"></div><spanv-else:style="column?.style ? column.style : {}":class="column?.class":title="scope.row[column.prop]":data-message="scope.row[column.prop]">{{ scope.row[column.prop] }}</span></template></el-table-column></template></el-table>
</template><script lang="ts" setup name="customTable">const props = defineProps({tableData: {type: Array,default: () => [],},tableColumn: {type: Array<any>,default: () => [],},loading: {type: Boolean,default: false,},});const generateRandomString = (length) => {const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';const charactersLength = characters.length;let result = '';for (let i = 0; i < length; i++) {const randomIndex = Math.floor(Math.random() * charactersLength);result += characters.charAt(randomIndex);}return result;};
// 通过renderVNode 接收一个vnode节点来渲染component
const renderVNode = (vnode: VNode) => {const tempDiv = document.createElement('div');const parentDiv = document.createElement('div');const id = generateRandomString(10);parentDiv.appendChild(tempDiv);tempDiv.id = id;// 利用createApp和render将vnode挂载成一个新的appconst Comp = createApp({render: () => vnode,});// 将这个新的vue app 挂载到对应的dom上nextTick(() => {// 但要放置到nextTick里面,以防止dom还没放置到table的对应位置,就先挂在了Comp.mount('#' + id);});// 返回一个html dom ,利用v-html挂载到table中对应的位置return parentDiv.innerHTML;};
</script >

如何使用

<template><custom-table maxHeight="100vh" :tableColumn="tableColumn" :tableData="tableData" />
</template><script lang="ts" setup name="test">
import componentTest from '../componentTest.vue';
import customTable from '../components/customTable.vue';
const tableColumn = ref([{label: '名称',prop: 'name',align: 'center',minWidth: 130,fixed: true,component: (value, row) => {return h('div', { onClick: () => handleClick(row?.name) }, [h('span', { style: { fontSize: '14px', display: 'inline-block' } }, value),h('p', { class: 'dealer-scale' }, row?.scale),]);},},{label: '订单',prop: 'order',component: (value, row) => {return h(componentTest, {title: value})},style: { color: '#C00017' },},{ label: '本年', prop: 'open', style: { color: '#C00017' } },{ label: '费效比', prop: 'efficiency', style: { color: '#038810' },render: (value,rows) => {console.log(value, rows)return value + '%'}},{ label: '处罚', prop: 'punish', style: { color: '#0000aa' } },]);const tableData = ref([// name order open efficiency punish scale{name: 'xxxx有限公司',order: '23455 万',open: '234 万瓶',efficiency: '1.3 %',punish: '3 次',scale: '10亿以上',},{name: '软件有限公司',order: '23456 万',open: '234 万瓶',efficiency: '1.3 %',punish: '3 次',scale: '5千万~1亿',},]);
</script>

tableColumn 属性

属性名类型解释
stylestring能够单独配置某一列的样式
classstring能够单独配置某一列的class名称
renderfunction接受两个参数 (value, row) value是当前行当前列的数据,row是当前行的数据 返回的值会渲染在table对应的列中
componentfunction接受两个参数 (value, row) value是当前行当前列的数据,row是当前行的数据,返回一个vnode,通过vue3自带的h函数实现
fixedboolean是否让当且列固定
剩下的属性查看element ui 中table 的Column配置 https://element.eleme.io/#/zh-CN/component/table#table-column-attributes

h函数

function h(type: string | Component, // 元素/组件类型props?: object | null,    // 属性/Propschildren?: VNodeChildren  // 子节点(字符串、数组、插槽等)
): VNode

创建原生元素

import { h } from 'vue'// 等价于 <div class="box" id="app">Hello</div>
h('div', { class: 'box', id: 'app' }, 'Hello')// 带事件
h('button', { onClick: () => console.log('Clicked!') }, 'Click me')

创建组件

import MyComponent from './MyComponent.vue'// 传递 props 和插槽
h(MyComponent, { title: 'Demo',onClick: () => {} // 监听自定义事件
}, {default: () => h('span', '默认插槽'),footer: () => h('div', 'Footer 内容')
})

动态生成

const items = ['A', 'B', 'C']
h('ul', items.map(item => h('li', item))
)
http://www.dtcms.com/wzjs/385996.html

相关文章:

  • 网站建设的颜色值域名是什么
  • 做网站第三方登录怎么弄seo技术员
  • 网站制作 ?B?T杭州网站推广优化公司
  • 便宜虚拟主机做网站备份短信营销平台
  • 家教网站制作网站手机版排名seo
  • 福州专业网站建设公司推广文案范文100字
  • 物联网平台是干什么的用的简述优化搜索引擎的方法
  • wordpress插件论坛铜川网站seo
  • 网站有什么模块长沙百度公司
  • 数字营销技术应用网站电商平台运营
  • 一个空间可以做多个网站吗新闻头条今日最新消息
  • wordpress 页面开发教程南昌seo服务
  • 建设文库网站网站如何才能被百度收录
  • 农产品网站的品牌建设友情链接的概念
  • 苏州网站开发公司济南兴田德润厉害吗企业管理培训课程网课
  • wordpress缓存无法清除网站seo推广seo教程
  • 网络运维的工作内容北京百度推广优化
  • 网站流量好难做徐州seo排名公司
  • 四川电子有限公司 - 手机网站360营销
  • 郑州网站设计价格福州seo优化
  • 网站标题的选择新东方在线教育平台官网
  • 销售推广方案seo网站推广有哪些
  • 下做图软件在哪个网站下载怎么做网站优化排名
  • 旅游门户网站模板下载竞价托管优化公司
  • 东昌网站建设公司西安百度推广代理商
  • seo关键词排名优化推荐电脑系统优化软件排行榜
  • 中山百度网站推广网站建设运营
  • 上海专业做网站公司电话seo优化关键词分类
  • 建建设网站的seo薪酬如何
  • 如何做网站搜索引擎优化郑州seo排名公司