vue3 + tsx 表格 Action 单独封装组件用法

前言

先上图看右侧列 action 的 UI 效果:

正常来说,如果一个表格的附带 action 操作,我们一般会放在最右侧的列里面实现,这个时候有些UI 框架支持在 SFC 模板里面定义额外的 solt,当然如果不支持,更通用的做法是通过 vue 的 h 函数来实现,纯粹用 js 或 ts 组装组件方式实现,这种方式很灵活,但有一个弊端,当定义的组件很多的时候,比如上图有4 个Button,还得定义按钮样式和点击事件,代码就显的非常乱。

更为优雅的做法是,把这一小块的功能给单独定义成一个 vue 组件,可以用 SFC 也可以用 JSX/TSX 实现,然后事件通过回调完成,代码就显得非常整洁了。

定义 Action 组件

这里用 tsx 实现:

import { defineComponent, PropType, toRefs } from 'vue'
import { NSpace, NTooltip, NButton, NIcon, NPopconfirm } from 'naive-ui'
import {AreaChartOutlined, DeleteOutlined, EditOutlined,
PlayCircleOutlined,PauseCircleOutlined,RedoOutlined,AlignCenterOutlined} from "@vicons/antd";
import {SolrListActionModel} from "@/service/middleware/types";

const props = {
    row: {
        type: SolrListActionModel as PropType<SolrListActionModel>
    }
}

export default defineComponent({
    name: 'SolrTableAction',
    props,
    emits: [
        'start',
        'stop',
        'restart',
        'showLog',
    ],
    setup(props, ctx) {

        const handleStartAction = () => {
            ctx.emit('start')
        }
        const handleStopAction = () => {
            ctx.emit('stop')
        }
        const handleRestartAction = () => {
            ctx.emit('restart')
        }
        const handleShowLogAction = () => {
            ctx.emit('showLog')
        }

        return {
            handleStartAction,
            handleStopAction,
            handleRestartAction,
            handleShowLogAction,
            ...toRefs(props)
        }
    },
    render() {
        return (
            <NSpace>
                {this.row?.start ? <NTooltip trigger={'hover'}>
                    {{
                        default: () => "启动",
                        trigger: () => (
                            <NButton
                                size='small'
                                type='info'
                                tag='div'
                                circle
                                onClick={this.handleStartAction}
                                class='btn-edit'
                            >
                                <NIcon>
                                    <PlayCircleOutlined></PlayCircleOutlined>
                                </NIcon>
                            </NButton>
                        )
                    }}
                </NTooltip>
                    :""
                }
                {
                   this.row?.stop?<NTooltip trigger={'hover'}>
                        {{
                            default: () => "停止",
                            trigger: () => (
                                <NButton
                                    size='small'
                                    type='info'
                                    tag='div'
                                    circle
                                    onClick={this.handleStopAction}
                                    class='btn-edit'
                                >
                                    <NIcon>
                                        <PauseCircleOutlined></PauseCircleOutlined>
                                    </NIcon>
                                </NButton>
                            )
                        }}
                    </NTooltip>:
                       ""
                }
                {
                    this.row?.restart?<NTooltip trigger={'hover'}>
                            {{
                                default: () => "重启",
                                trigger: () => (
                                    <NButton
                                        size='small'
                                        type='info'
                                        tag='div'
                                        circle
                                        onClick={this.handleRestartAction}
                                        class='btn-edit'
                                    >
                                        <NIcon>
                                            <RedoOutlined></RedoOutlined>
                                        </NIcon>
                                    </NButton>
                                )
                            }}
                        </NTooltip>:
                        ""
                }
                {
                    this.row?.showLog?<NTooltip trigger={'hover'}>
                            {{
                                default: () => "日志",
                                trigger: () => (
                                    <NButton
                                        size='small'
                                        type='info'
                                        tag='div'
                                        circle
                                        onClick={this.handleShowLogAction}
                                        class='btn-edit'
                                    >
                                        <NIcon>
                                            <AlignCenterOutlined></AlignCenterOutlined>
                                        </NIcon>
                                    </NButton>
                                )
                            }}
                        </NTooltip>:
                        ""
                }
                {this.$slots.extraAction?.()}
            </NSpace>
        )
    }
})
在 Table 列里面引用

定义好之后,在 table 里面引用就非常简单了,只需要把 props 属性传递一下,然后定义回调处理事件即可:

        const columns = [
            {
                title: '服务名',
                key: 'server_name',
                align: 'center'
            },
            {
                title: '运行状态',
                key: 'process_state',
                align: 'center'
            },
            {
                title: '主机IP',
                key: 'process_ip',
                align: 'center'
            },
            {
                title: '启动信息',
                key: 'process_description',
                align: 'center'
            },
            {
                title: '操作',
                key: 'operation',
              //列宽自适应
                ...COLUMN_WIDTH_CONFIG['operation'](4),
                align: 'center',
                render: (row: SolrList)=>{
                    let model=new SolrListActionModel(row)
                   return  h(SolrTableAction, {
                        row: model,
                        onStart: () => {

                        },
                        onStop:()=>{

                        },
                       onRestart:()=>{

                       },
                       onShowLog:()=>{

                       }
                    })


                }
            }
        ] as TableColumns<any>
列宽自适应工具类

这个是为了该列的宽度,根据实际情况重新分布:

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import { isNumber, sumBy } from 'lodash'
import type {
  TableColumns,
  CommonColumnInfo
} from 'naive-ui/es/data-table/src/interface'

export const COLUMN_WIDTH_CONFIG = {
  selection: {
    width: 50
  },
  index: {
    width: 50
  },
  linkName: {
    width: 200
  },
  linkEllipsis: {
    style: 'max-width: 180px;line-height: 1.5'
  },
  name: {
    width: 200,
    ellipsis: {
      tooltip: true
    }
  },
  state: {
    width: 120
  },
  type: {
    width: 130
  },
  version: {
    width: 80
  },
  time: {
    width: 180
  },
  timeZone: {
    width: 220
  },
  operation: (number: number): CommonColumnInfo => ({
    fixed: 'right',
    width: Math.max(30 * number + 12 * (number - 1) + 24, 100)
  }),
  userName: {
    width: 120,
    ellipsis: {
      tooltip: true
    }
  },
  ruleType: {
    width: 120
  },
  note: {
    width: 180,
    ellipsis: {
      tooltip: true
    }
  },
  dryRun: {
    width: 140
  },
  times: {
    width: 120
  },
  duration: {
    width: 120
  },
  yesOrNo: {
    width: 100,
    ellipsis: {
      tooltip: true
    }
  },
  size: {
    width: 100
  },
  tag: {
    width: 160
  },
  copy: {
    width: 50
  }
}

export const calculateTableWidth = (columns: TableColumns) =>
  sumBy(columns, (column) => (isNumber(column.width) ? column.width : 0))

export const DefaultTableWidth = 1800
总结

通用这种单独定义组件的方式,大大提高了我们代码的可读性,并且后续这个组件可以单独扩展和改变样式而不影响主列表页的迭代,最后我们要注意给 h 组件的传递 props 的方式,直接使用 props name:value 的形式即可。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/793070.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

无头单向非循环链表实现 and leetcode刷题

无头单向非循环链表实现 1. 单链表的模拟实现IList.java接口&#xff1a;MySingleList.java文件&#xff1a; 2. leetcode刷题2.1 获取链表的中间节点2.2 删除链表中所有值为value的元素2.3 单链表的逆置2.4 获取链表倒数第k个节点2.5 给定 x, 把一个链表整理成前半部分小于 x,…

【C++】C++书店管理系统(源码+论文)【独一无二】

&#x1f449;博__主&#x1f448;&#xff1a;米码收割机 &#x1f449;技__能&#x1f448;&#xff1a;C/Python语言 &#x1f449;公众号&#x1f448;&#xff1a;测试开发自动化【获取源码商业合作】 &#x1f449;荣__誉&#x1f448;&#xff1a;阿里云博客专家博主、5…

如何在 Python 中创建一个类似于 MS 计算器的 GUI 计算器

问题背景 假设我们需要创建一个类似于微软计算器的 GUI 计算器。这个计算器应该具有以下功能&#xff1a; 能够显示第一个输入的数字。当按下运算符时&#xff0c;输入框仍显示第一个数字。当按下第二个数字时&#xff0c;第一个数字被替换。 解决方案 为了解决这个问题&am…

mysql高可用解决方案:MHA原理及实现

MHA&#xff1a;Master High Availability。对主节点进行监控&#xff0c;可实现自动故障转移至其它从节点&#xff1b;通过提升某一从节点为新的主节点&#xff0c;基于主从复制实现&#xff0c;还需要客户端配合实现&#xff0c;目前MHA主要支持一主多从的架构&#xff0c;要…

STL(一)

书写形式&#xff1a;string (const string& str, size_t pos, size_t len npos); 举例&#xff1a; int main(){ string url("https://mp.csdn.net/mp_blog/creation/editor?spm1000.2115.3001.4503") string sub1(url,0,5);//从下标为0开始向后5个字符&…

07列的完整性约束

文章目录 设置表字段的主键约束设置表字段的外键约束(FOREIGN KEY,FK)、设置表字段的非空约束(NOT NULL, NK)设置表字段唯一约束(UNIQUE,UK)设置表字段值自动增加(AUTO_INCREMENT)设置表字段的默认值(DEFAULT)修改默认值DEFAULT、自增长和非空NK设置表字段的主键约…

30.ROM-IP核的调用

&#xff08;1&#xff09;ROM IP核简介&#xff1a; ROM是只读存储器&#xff0c;是一种只能读出事先锁存的固态半导体存储器。其特性是一旦存储资料就无法再将之改变或删除&#xff0c;并且资料也不会因为电源关闭而消失。&#xff08;掉电不丢失&#xff09; FPGA使用内部RA…

JavaScript青少年简明教程:为何学习JavaScript及JavaScript简介

JavaScript青少年简明教程&#xff1a;为何学习JavaScript及JavaScript简介 JavaScript最初是为web浏览器&#xff08;前端开发&#xff09;设计的。它可以在所有现代浏览器中运行&#xff0c;包括Chrome, Firefox, Safari, Edge等。 这意味着JavaScript代码可以在任何能运行…

学习测试7-ADB的使用

ADB是什么&#xff1f; ADB&#xff0c;即 Android Debug Bridge&#xff08;安卓调试桥&#xff09; 是一种允许模拟器或已连接的 Android 设备进行通信的命令行工具&#xff0c;它可为各种设备操作提供便利&#xff0c;如安装和调试应用&#xff0c;并提供对 Unix shell&…

数据(图像)增广

一、数据增强 1、增加一个已有数据集&#xff0c;使得有更多的多样性&#xff0c;比如加入不同的背景噪音、改变图片的颜色和形状。 2、增强数据是在线生成的 3、增强类型&#xff1a; &#xff08;1&#xff09;翻转 &#xff08;2&#xff09;切割 &#xff08;3&#xf…

MessageBox与HubSpot:企业沟通与客户管理的双重利器

今天咱们来聊聊两个超实用的工具——MessageBox和HubSpot。它们就像是你的超级助手&#xff0c;让你和客户沟通起来更顺畅&#xff0c;管理起来也更轻松。 先说说MessageBox吧 想象一下&#xff0c;你正在忙着工作&#xff0c;突然客户发来个消息&#xff0c;你嗖的一下就收到…

实验场:在几分钟内使用 Bedrock Anthropic Models 和 Elasticsearch 进行 RAG 实验

作者&#xff1a;来自 Elastic Joe McElroy, Aditya Tripathi 我们最近发布了 Elasticsearch Playground&#xff0c;这是一个新的低代码界面&#xff0c;开发人员可以通过 A/B 测试 LLM、调整提示&#xff08;prompt&#xff09;和分块数据来迭代和构建生产 RAG 应用程序。今天…

github恢复码怎么备份

https://docs.github.com/zh/authentication/securing-your-account-with-two-factor-authentication-2fa/configuring-two-factor-authentication-recovery-methods

电商IP分类及其应用是什么?

在现代电商运营中&#xff0c;IP地址不仅是网络通信的基础&#xff0c;也扮演着关键的角色&#xff0c;支持多种功能和应用场景。本文将介绍几种常见的电商IP分类&#xff0c;以及它们在电商领域中的具体应用。 1. 前台IP与后台IP 电商网站在运营过程中通常需要区分前台IP和后…

数据不可修改 确保数据安全-GS备份存储方案防病毒防勒索

为保障企业关键数据不被病毒或勒索软件侵害&#xff0c;通过Veeam数据不可变功能&#xff0c;存储内数据更安全

【计算机组成原理 | 第三篇】各个硬件的组成部分

前言&#xff1a; 在前面的文章中&#xff0c;我们介绍了计算机架构的基本组成。可以知道计算机的基本架构由“存储器”&#xff0c;“运算器”&#xff0c;“控制器”&#xff0c;“输入设备”&#xff0c;“输出设备”这五部分组成。 在这片文章中&#xff0c;我们来深入的了…

【若依管理系统】注意事项

1.前端字段必填 rules: {sceneName: [{ required: true, message: "场景名称不能为空", trigger: "blur" }],orderNum: [{ required: true, message: "显示排序不能为空", trigger: "blur" }], }, 2.IDEA&#xff0c;默认以debug模式…

dive deeper into tensor:从底层开始学习tensor

inspired by karpathy/micrograd: A tiny scalar-valued autograd engine and a neural net library on top of it with PyTorch-like API (github.com)and Taking PyTorch for Granted | wh (nrehiew.github.io). 这属于karpathy的karpathy/nn-zero-to-hero: Neural Networks…

数据库系统原理练习 | 作业2-第2章关系数据库(附答案)

整理自博主本科《数据库系统原理》专业课完成的课后作业&#xff0c;以便各位学习数据库系统概论的小伙伴们参考、学习。 *文中若存在书写不合理的地方&#xff0c;欢迎各位斧正。 专业课本&#xff1a; 目录 一、选择题 二、填空题 三、简答题 四、关系代数 1.课本p70页&…

插片式远程 I/O模块:Profinet总线耦合器在SIMATIC Manager配置

XD9000是Profinet总线耦合器&#xff0c;单个耦合器最多可扩展32个I/O模块&#xff01;本文将详细介绍如何在SIMATIC Manager中配置插片式远程 I/O模块的Profinet总线耦合器&#xff0c;帮助您更好地应用这一技术。 一、SIMATIC Manager软件组态步骤&#xff1a; 1、创建工程&…