網(wǎng)站首頁 編程語言 正文
項(xiàng)目技術(shù):Vue-cli3、Element-ui、Ant-Design-Vue、Echarts4等
需求:在項(xiàng)目中所有使用el-table表格的地方,對(duì)【項(xiàng)目名稱列】添加點(diǎn)擊事件及樣式【觸發(fā)某個(gè)彈窗顯示并在該列添加cursor: pointer;
等樣式】。
一、場(chǎng)景一:項(xiàng)目初期
1. 解決一:el-table默認(rèn)插槽
描述:通過el-table默認(rèn)插槽給項(xiàng)目名稱itemName列添加指定事件及樣式。
弊端:所有用到el-table的地方,都需要添加相同處理itemName列的邏輯代碼。
優(yōu)點(diǎn):靈活性,不同表格中添加其他不同邏輯代碼較靈活。
官網(wǎng)描述:
插槽代碼:
<template slot-scope="{ row, column }">
<span
:class="{table__cellCanClick: column.property === 'iteName'}"
@click="column.property === 'iteName' && showModal()"
v-html="row[item.field]"
></span>
<span></span>
</template>
優(yōu)化插槽代碼(自定義指令):
// 全局注冊(cè)指令
let itemNameD = {
inserted: function(el, binding) {
let row = binding.value.row;
let column = binding.value.column;
if (column.property === "itemName") {
el.classList.add("table__cell-can-click");
el.addEventListener("click", () => {
console.log(row);
});
}
}
};
Vue.directive("itemNameD", itemNameD);
<template>
<!-- 省略部分多余代碼 -->
<el-table>
<template slot-scope="{ row, column }">
<span v-itemNameD="{ row, column }">{{ row[item.field] }}</span>
</template>
</el-table>
</template>
2. 解決二:同場(chǎng)景二的解決一
二、場(chǎng)景二:項(xiàng)目后期
描述:整個(gè)項(xiàng)目直接使用el-table組件的地方有近百處,秉承盡可能的減少修改原代碼原則。
解決一:私有化el-table組件
描述:私有化掉el-table組件 然后去改這個(gè)組件 組件內(nèi)部進(jìn)行調(diào)整。最后在項(xiàng)目中覆蓋掉element的el-table組> 件。
缺點(diǎn):當(dāng)更新element-ui的版本后,還需要重寫el-table。
優(yōu)點(diǎn):不用修改項(xiàng)目原來代碼,畢竟使用到的地方近百個(gè)el-table。
對(duì)源碼有抵觸心理的,可以先通過這篇簡(jiǎn)單了解 # element-ui 的組件源碼還能這么看:
-
找到el-table的源碼
node_module/element-ui/packages/table
整個(gè)文件復(fù)制到項(xiàng)目中(不要被eslent檢測(cè)到,盡可能少改動(dòng)源碼) -
修改依賴
因?yàn)轫?xiàng)目中使用
Vue.use(ElementUI);
將element組件全部注冊(cè),將代碼中引入組件element-ui/packages
的代碼注釋掉(如果不注釋掉會(huì)報(bào)錯(cuò),直接引入node_module下element-ui/packages下的單個(gè)組件,可能內(nèi)部代碼在未顯示引入全局Vue的情況下,不能直接使用Vue的transition等組件),默認(rèn)直接使用Vue全局組件即可,以table-body.js
為例: -
添加邏輯代碼
一步步簡(jiǎn)略查看最終定位到
src/table-body.js
:created() { // ... // 自己添加 this.selfCustomField = ['itemName']; }, methods: { // ... rowRender(row, $index, treeRowData) { // ... return ( <td style={ this.getCellStyle($index, cellIndex, row, column) } class={ this.getCellClass($index, cellIndex, row, column) } rowspan={ rowspan } colspan={ colspan } on-click={ ($event) => this.handleCellClick($event, row, column) } /**自己添加**/ on-mouseenter={ ($event) => this.handleCellMouseEnter($event, row) } on-mouseleave={ this.handleCellMouseLeave }> { column.renderCell.call( this._renderProxy, this.$createElement, data, columnsHidden[cellIndex] ) } </td> ); }, getCellClass(rowIndex, columnIndex, row, column) { // ... // 自己添加 if (this.selfCustomField.includes(column.property)) { classes.push('table__cell-can-click'); } classes.push('el-table__cell'); return classes.join(' '); } /**自己添加**/ handleCellClick(event, row, column) { if (this.selfCustomField.includes(column.property)) { console.log(row); } }, }
-
在main.js中覆蓋原el-table組件
import ElementUI from "element-ui"; // 重寫的組件 import ElTable from "./components/ReWriteFrameComponents/el-table"; // ... Vue.use(ElementUI); Vue.use(ElTable);
最終效果:
解決二:識(shí)別并修改Vnode
描述:識(shí)別router-view當(dāng)前route中內(nèi)部所有el-table,并修改Vnode。
目前個(gè)人能力有限,暫且擱置,后面會(huì)進(jìn)行補(bǔ)充。
原文鏈接:https://blog.csdn.net/qq_43461877/article/details/122095393
相關(guān)推薦
- 2023-01-05 Kotlin注解與反射的定義及創(chuàng)建使用詳解_Android
- 2022-08-31 python中ndarray數(shù)組的索引和切片的使用_python
- 2022-11-04 react-router-dom?v6?使用詳細(xì)示例_React
- 2022-08-02 C/C++詳解如何實(shí)現(xiàn)文件備份_C 語言
- 2022-05-10 SpringBoot端口已占用解決:配置端口號(hào)
- 2022-05-15 C++?Clock類模擬實(shí)現(xiàn)鬧鐘運(yùn)行_C 語言
- 2023-08-16 數(shù)據(jù)選擇器 uni-data-checkbox,獲取value值
- 2023-12-09 【設(shè)計(jì)模式-3.1】結(jié)構(gòu)型——外觀模式
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支