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: () => (<NButtonsize='small'type='info'tag='div'circleonClick={this.handleStartAction}class='btn-edit'><NIcon><PlayCircleOutlined></PlayCircleOutlined></NIcon></NButton>)}}</NTooltip>:""}{this.row?.stop?<NTooltip trigger={'hover'}>{{default: () => "停止",trigger: () => (<NButtonsize='small'type='info'tag='div'circleonClick={this.handleStopAction}class='btn-edit'><NIcon><PauseCircleOutlined></PauseCircleOutlined></NIcon></NButton>)}}</NTooltip>:""}{this.row?.restart?<NTooltip trigger={'hover'}>{{default: () => "重啟",trigger: () => (<NButtonsize='small'type='info'tag='div'circleonClick={this.handleRestartAction}class='btn-edit'><NIcon><RedoOutlined></RedoOutlined></NIcon></NButton>)}}</NTooltip>:""}{this.row?.showLog?<NTooltip trigger={'hover'}>{{default: () => "日志",trigger: () => (<NButtonsize='small'type='info'tag='div'circleonClick={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.pswp.cn/diannao/44754.shtml
繁體地址,請注明出處:http://hk.pswp.cn/diannao/44754.shtml
英文地址,請注明出處:http://en.pswp.cn/diannao/44754.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

【網絡】SCTP協議概念

SCTP協議 SCTP&#xff08;Stream Control Transmission Protocol&#xff09;是一種傳輸層協議&#xff0c;設計用于提供可靠的數據傳輸服務&#xff0c;同時具備一些類似UDP&#xff08;User Datagram Protocol&#xff09;的屬性。以下是SCTP協議的基本概念、優缺點以及與T…

Linux進行vi編譯代碼出現“E45: ‘readonly‘ option is set (add ! to override)”(完美解決)。

用vi修改文件&#xff0c;保存文件時&#xff0c;提示沒有修改該文件的權限“E45: ‘readonly’ option is set (add ! to override)”的解決方法。 E45: ‘readonly’ option is set (add ! to override) 如果您遇到了“當前用戶沒有權限對文件作修改”的錯誤 1. 檢查文件…

2024.7.11最新版IDM破解,操作簡單

前言 IDM的強勁對手&#xff0c;100%免費&#xff0c;如果破解IDM失敗&#xff0c;推薦使用FDM&#xff0c;下載地址&#xff1a;Free Download Manager 破解步驟 打開PowerShell&#xff0c;非CMD 在左下角開始菜單右鍵點擊后選擇PowerShell&#xff0c;注意不是打開CMD。…

園林類專刊《花卉》簡介及投稿郵箱

園林類專刊《花卉》簡介及投稿郵箱 《花卉》雜志是經國家新聞出版總署批準&#xff0c;廣東省農業科學院主管&#xff0c;廣東省農業科學院環境園藝研究所主辦&#xff0c;面向國內外公開發行林業系統專業期刊&#xff0c;是全國從事林業、園林、生態、環保、旅游、自然資源、…

CentOS7安裝部署git和gitlab

安裝Git 在Linux系統中是需要編譯源碼的&#xff0c;首先下載所需要的依賴&#xff1a; yum install -y curl-devel expat-devel gettext-devel openssl-devel zlib-devel gcc perl-ExtUtils-MakeMaker方法一 下載&#xff1a; wget https://mirrors.edge.kernel.org/pub/s…

【文檔+源碼+調試講解】冷凍倉儲管理系統

摘 要 隨著互聯網時代的到來&#xff0c;同時計算機網絡技術高速發展&#xff0c;網絡管理運用也變得越來越廣泛。因此&#xff0c;建立一個B/S結構的冷凍倉儲管理系統&#xff0c;會使冷凍倉儲管理系統工作系統化、規范化&#xff0c;也會提高冷凍倉儲管理系統平臺形象&#x…

現在國內的ddos攻擊趨勢怎么樣?想了解現在ddos的情況該去哪看?

目前&#xff0c;國內的DDoS攻擊趨勢顯示出以下幾個特征&#xff1a; 攻擊頻次顯著增加&#xff1a;根據《快快網絡2024年DDoS攻擊趨勢白皮書》&#xff0c;2023年DDoS攻擊活動有顯著攀升&#xff0c;總攻擊次數達到1246.61萬次&#xff0c;比前一年增長了18.1%。 攻擊強度和規…

微軟子公司Xandr遭隱私訴訟,或面臨巨額罰款

近日&#xff0c;歐洲隱私權倡導組織noyb對微軟子公司Xandr提起了訴訟&#xff0c;指控其透明度不足&#xff0c;侵犯了歐盟公民的數據訪問權。據指控&#xff0c;Xandr的行為涉嫌違反《通用數據保護條例》&#xff08;GFPR&#xff09;&#xff0c;因其處理信息并創建用于微目…

Shader每日一練(2)護盾

Shader "Custom/Shield" {Properties{_Size("Size", Range(0 , 10)) 1 // 控制噪聲紋理縮放大小的參數_colorPow("colorPow", Float) 1 // 控制顏色強度的指數_colorMul("colorMul", Float) 1 // 控制顏色乘法因子_mainColor("…

多旋翼+VR眼鏡:10寸FPV穿越機技術詳解

FPV&#xff08;First Person View&#xff09;穿越機&#xff0c;是指通過第一人稱視角來駕駛的無人機&#xff0c;特別強調速度和靈活性&#xff0c;常常用于競賽、航拍和探索等領域。結合多旋翼設計和VR眼鏡&#xff0c;FPV穿越機為用戶提供了身臨其境的飛行體驗。 多旋翼技…

數據庫操作和ORM(對象關系映射)框架.creat insert .save區別

1. .create 用途&#xff1a;.create 方法通常用于創建一個新的實體&#xff08;Entity&#xff09;實例&#xff0c;并為其屬性設置初始值。這個方法不會立即將實體保存到數據庫中&#xff0c;而是返回一個配置好的實體實例&#xff0c;該實例可以被進一步修改或用于后續操作…

企業網站被攻擊的常見方式是什么,該如何去做防護

隨著互聯網的普及和人們對網絡使用的增加&#xff0c;網站安全問題變得越來越突出。無論是個人還是企業&#xff0c;都需要了解并采取措施來保護自己的網站和用戶數據的安全。本文介紹常見的網站安全攻擊方式、潛在危害及其預防措施&#xff0c;幫助全面了解網站安全的各個方面…

Vue的常見指令

目錄 1.v-bind 2. class綁定 3.style綁定 4.v-if/v-show 1.v-bind v-bind指令用于綁定屬性 可以簡寫成 “ &#xff1a;” 它的作用就是我們可以動態的定義屬性的值&#xff0c;比如常見的<img src "1.jpg"> 我們如果想要修改圖片就需要獲取到DOM對象&am…

新興市場游戲產業爆發 傳音以技術搶抓機遇

隨著年輕人口的增加以及互聯網的普及&#xff0c;非洲、中東等新興市場正迎來游戲產業的大爆發&#xff0c;吸引著全球游戲企業玩家在此開疆辟土。中國出海企業代表傳音以新興市場需求為中心&#xff0c;秉持本地化創新理念不斷加強游戲等關鍵領域技術攻關憑借移動終端設備為全…

藍卓創始人褚健:工業互聯網平臺技術賦能中小企業數字化轉型的實施路徑

工業4.0是由工業軟件驅動的工業革命&#xff0c;與傳統厚重的工業軟件不同&#xff0c;supOS就好比嵌入工廠的“安卓系統”。如果把一個工廠當作一臺手機&#xff0c;因為有安卓或蘋果開放的操作系統&#xff0c;吸引了全世界聰明的人開發了大量APP供人們使用&#xff0c;手機才…

Backend - visual studio 安裝配置運行

目錄 一、安裝 &#xff08;一&#xff09;visual studio的內存需求很大&#xff01; &#xff08;二&#xff09;自定義工具和SDK的安裝位置 1. 菜單欄搜索regedit&#xff0c;進入注冊表編輯器 2. 修改SharedInstallationPath項的路徑 3. 重啟電腦 4. 重新打開visual studio …

物聯網系統中市電電量計量方案(二)

上文我們主要介紹了電量計量中最重要的組成部分——電量計量芯片&#xff08;如果沒有閱讀該文章的&#xff0c;可以點擊這里&#xff09;。本文會再為大家介紹電量計量的另外一個組成部分——電流互感器。 電流互感器的定義 電流互感器是一種可將一次側大電流轉換為二次側小電…

智慧校園科研管理:論文管理提升學術影響力

在智慧校園科研管理平臺中&#xff0c;論文信息管理模塊扮演著連接學術創新與管理效率的橋梁角色&#xff0c;它精心設計了一系列功能&#xff0c;旨在促進學術成果的高效記錄、跟蹤、分享與評估&#xff0c;為科研工作者、管理人員及全體師生構建了一個協同合作的學術生態環境…

printJS實現打印圖片和pdf

下載依賴 npm install print-js --save 引入 import printJS from print-js <el-dialog title"提示" :visible.sync"dialogVisible" width"30%" :before-close"handleClose"><span slot"footer" class"dia…

L1和L2正則化的區別是什么?

L1和L2正則化的區別是什么&#xff1f; L1和L2正則化都是機器學習中用于防止過擬合的技術&#xff0c;它們通過向模型的損失函數添加一個懲罰項來鼓勵模型參數的稀疏性或平滑性。 L1 正則化&#xff08;也稱為 Lasso 正則化&#xff09;&#xff1a; 它對模型的權重施加一個…