el-table組件實(shí)現(xiàn)表頭搜索功能
一,展示效果
二,功能介紹
利用el-table
組件提供的render-header
屬性自定義表頭渲染內(nèi)容,包含表頭標(biāo)題和一個(gè)搜索圖標(biāo),圖標(biāo)是一個(gè)Popover
組件彈出框,點(diǎn)擊圖標(biāo)出現(xiàn)下面輸入框和搜索按鈕,點(diǎn)擊搜索區(qū)域以外的位置,搜索區(qū)域消失,這個(gè)使用的是element-ui
的 Clickoutside
指令。
三,實(shí)現(xiàn)代碼
主頁(yè)面 template
部分:
<!-- template部分--> <el-table :data="list" v-loading="listLoading" ref="table"> <el-table-column v-for="(item, index) in tableHead[activeOption]" :key="index + item.prop + item.label" :prop="item.prop" :label="item.label" :min-width="item.width ? item.width : item.label.length * 12 + 50" :show-overflow-tooltip="true" :align="item.align || 'center'" class-name="flexColumn" :render-header="(h, row) => NumberRenderHeader(h, row, item)" :fixed="item.fixed" > <template slot-scope="{ row }"> <span> {{ row[item.prop] || "" }} </span> </template> </el-table-column>
主頁(yè)面 methods
部分,其中SearchCom
是自定義搜索組件。
// 表頭渲染函數(shù) NumberRenderHeader(createElement, { column, $index }, item) { let self = this; if (!item.isHeadSearch) { return item.label; } return createElement("div", [ createElement("div", { domProps: { innerHTML: column.label, }, }), createElement(SearchCom, { props: { defaultValue: "", // 默認(rèn)值 selectIndex: item.popIndex || $index - 3, }, on: { selectChange: (val) => self.selectFruitChange(val, item), }, }), ]); },
render-header
屬性:
關(guān)于
createElement
函數(shù):介紹鏈接
自定義組件部分
<template> <el-popover placement="bottom" width="200" trigger="manual" v-model="visible" @show="showPopover" popper-class="charge-item-header-popover aaa" > <!-- 彈出框內(nèi)容 --> <div class="popover_box"> <el-input placeholder="請(qǐng)輸入" v-model="selectValue" @keyup.enter.native="confirm" ref="sInput" style="padding: 10px 5px" > </el-input> <el-button @click="confirm">搜索</el-button> </div> <!-- 觸發(fā)元素 --> <div slot="reference" style="margin-left: 5px" @click.stop="popClick" v-clickoutside="closeOver" > <i class="el-icon-search"></i> </div> </el-popover> </template> <script> // import Clickoutside from "element-ui/src/utils/clickoutside"; // 使用elementui的 Clickoutside 指令 import Clickoutside from "./clickoutside"; // 使用elementui的 Clickoutside 指令 export default { data() { return { value: "", // 輸入框中的值 visible: false, // 組件顯示隱藏控制 selectValue: "", // 當(dāng)前選中值 popperElm: "", }; }, props: { defaultValue: { type: String, default: "", }, selectIndex: { type: Number, default: 0, }, }, mounted() { // 解決點(diǎn)擊輸入框組件關(guān)閉問(wèn)題 this.popperElm = document.getElementsByClassName( "charge-item-header-popover" )[this.selectIndex - 1]; }, methods: { // 點(diǎn)擊當(dāng)前組件之外關(guān)閉 handleOutsideClick(e) { setTimeout(() => { this.visible = false; }, 16); }, // 展示當(dāng)前組件時(shí) 鼠標(biāo)光標(biāo)定位到輸入框 showPopover() { this.$nextTick(() => { this.$refs.sInput.focus(); }); }, // 關(guān)閉當(dāng)前組件 closeOver() { this.visible = false; }, popClick(e) { this.visible = !this.visible; }, // 輸入文字匹配對(duì)象的li項(xiàng) confirm() { this.$emit("selectChange", this.selectValue); }, }, directives: { Clickoutside, // 引用elementui Clickoutside指令 }, }; </script> <style scoped> .el-input { border-bottom: 1px solid #ccc; } .el-input--prefix /deep/ .el-input__prefix { left: 15px; } .popover_box { display: flex; align-items: center; padding: 0 5px; } ::v-deep .el-input { border-bottom: none; } </style> <style> .charge-item-header-popover { padding: 0; } .charge-item-header-popover .el-button { height: 80%; } </style>
四,遇到的問(wèn)題
點(diǎn)擊表格的某個(gè)搜索圖標(biāo),點(diǎn)擊輸入框,搜索區(qū)域消失,控制是否點(diǎn)擊目標(biāo)區(qū)域以外的元素是通過(guò)Clickoutside
指令實(shí)現(xiàn)的,下面是Clickoutside
指令的關(guān)鍵代碼:
function createDocumentHandler(el, binding, vnode) { return function (mouseup = {}, mousedown = {}) { if ( !vnode || !vnode.context || !mouseup.target || !mousedown.target || el.contains(mouseup.target) || el.contains(mousedown.target) || el === mouseup.target || (vnode.context.popperElm && (vnode.context.popperElm.contains(mouseup.target) || vnode.context.popperElm.contains(mousedown.target))) ) return; if ( binding.expression && el[ctx].methodName && vnode.context[el[ctx].methodName] ) { vnode.context[el[ctx].methodName](); } else { el[ctx].bindingFn && el[ctx].bindingFn(); } }; }
其中vnode
代表使用自定義指令的元素,vnode.context.popperElm
則代表使用自定義指令所在的vue
文件中data
屬性中的數(shù)據(jù),若這個(gè)值綁定的元素包含鼠標(biāo)點(diǎn)擊的元素(即搜索圖標(biāo))則Popver
彈出框不會(huì)消失,否則消失,其中popperElm
綁定的元素如下:
mounted() { // 解決點(diǎn)擊輸入框組件關(guān)閉問(wèn)題 this.popperElm = document.getElementsByClassName( "charge-item-header-popover" )[0]; },
以上說(shuō)明通過(guò)上面方法獲取的彈出框元素并不包含搜索圖標(biāo)(兩個(gè)元素不具有父子關(guān)系),但是從控制臺(tái)檢索元素看,兩個(gè)元素又確實(shí)是包含關(guān)系,后來(lái)想到原因如下
一個(gè)表格內(nèi)包含多個(gè)表頭搜索字段,而第二個(gè)搜索框肯定是不包含第一個(gè)搜索框圖標(biāo)的
五,解決
在獲取彈出框元素時(shí)傳給搜索框組件一個(gè)索引說(shuō)明是當(dāng)前頁(yè)面中的第幾個(gè)彈出框
父組件頁(yè)面
createElement(SearchCom, { props: { defaultValue: "", // 默認(rèn)值 selectIndex: item.popIndex || 1, //selectIndex代表當(dāng)前頁(yè)面的第幾個(gè)popper彈出框 }, on: { selectChange: (val) => self.selectFruitChange(val, item), }, }),
自定義彈出框組件
mounted() { // 解決點(diǎn)擊輸入框組件關(guān)閉問(wèn)題 this.popperElm = document.getElementsByClassName( "charge-item-header-popover" )[this.selectIndex - 1]; },
到此這篇關(guān)于el-table組件實(shí)現(xiàn)表頭搜索的文章就介紹到這了,更多相關(guān)el-table表頭搜索內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
一,展示效果
二,功能介紹
利用el-table
組件提供的render-header
屬性自定義表頭渲染內(nèi)容,包含表頭標(biāo)題和一個(gè)搜索圖標(biāo),圖標(biāo)是一個(gè)Popover
組件彈出框,點(diǎn)擊圖標(biāo)出現(xiàn)下面輸入框和搜索按鈕,點(diǎn)擊搜索區(qū)域以外的位置,搜索區(qū)域消失,這個(gè)使用的是element-ui
的 Clickoutside
指令。
三,實(shí)現(xiàn)代碼
主頁(yè)面 template
部分:
<!-- template部分--> <el-table :data="list" v-loading="listLoading" ref="table"> <el-table-column v-for="(item, index) in tableHead[activeOption]" :key="index + item.prop + item.label" :prop="item.prop" :label="item.label" :min-width="item.width ? item.width : item.label.length * 12 + 50" :show-overflow-tooltip="true" :align="item.align || 'center'" class-name="flexColumn" :render-header="(h, row) => NumberRenderHeader(h, row, item)" :fixed="item.fixed" > <template slot-scope="{ row }"> <span> {{ row[item.prop] || "" }} </span> </template> </el-table-column>
主頁(yè)面 methods
部分,其中SearchCom
是自定義搜索組件。
// 表頭渲染函數(shù) NumberRenderHeader(createElement, { column, $index }, item) { let self = this; if (!item.isHeadSearch) { return item.label; } return createElement("div", [ createElement("div", { domProps: { innerHTML: column.label, }, }), createElement(SearchCom, { props: { defaultValue: "", // 默認(rèn)值 selectIndex: item.popIndex || $index - 3, }, on: { selectChange: (val) => self.selectFruitChange(val, item), }, }), ]); },
render-header
屬性:
關(guān)于
createElement
函數(shù):介紹鏈接
自定義組件部分
<template> <el-popover placement="bottom" width="200" trigger="manual" v-model="visible" @show="showPopover" popper-class="charge-item-header-popover aaa" > <!-- 彈出框內(nèi)容 --> <div class="popover_box"> <el-input placeholder="請(qǐng)輸入" v-model="selectValue" @keyup.enter.native="confirm" ref="sInput" style="padding: 10px 5px" > </el-input> <el-button @click="confirm">搜索</el-button> </div> <!-- 觸發(fā)元素 --> <div slot="reference" style="margin-left: 5px" @click.stop="popClick" v-clickoutside="closeOver" > <i class="el-icon-search"></i> </div> </el-popover> </template> <script> // import Clickoutside from "element-ui/src/utils/clickoutside"; // 使用elementui的 Clickoutside 指令 import Clickoutside from "./clickoutside"; // 使用elementui的 Clickoutside 指令 export default { data() { return { value: "", // 輸入框中的值 visible: false, // 組件顯示隱藏控制 selectValue: "", // 當(dāng)前選中值 popperElm: "", }; }, props: { defaultValue: { type: String, default: "", }, selectIndex: { type: Number, default: 0, }, }, mounted() { // 解決點(diǎn)擊輸入框組件關(guān)閉問(wèn)題 this.popperElm = document.getElementsByClassName( "charge-item-header-popover" )[this.selectIndex - 1]; }, methods: { // 點(diǎn)擊當(dāng)前組件之外關(guān)閉 handleOutsideClick(e) { setTimeout(() => { this.visible = false; }, 16); }, // 展示當(dāng)前組件時(shí) 鼠標(biāo)光標(biāo)定位到輸入框 showPopover() { this.$nextTick(() => { this.$refs.sInput.focus(); }); }, // 關(guān)閉當(dāng)前組件 closeOver() { this.visible = false; }, popClick(e) { this.visible = !this.visible; }, // 輸入文字匹配對(duì)象的li項(xiàng) confirm() { this.$emit("selectChange", this.selectValue); }, }, directives: { Clickoutside, // 引用elementui Clickoutside指令 }, }; </script> <style scoped> .el-input { border-bottom: 1px solid #ccc; } .el-input--prefix /deep/ .el-input__prefix { left: 15px; } .popover_box { display: flex; align-items: center; padding: 0 5px; } ::v-deep .el-input { border-bottom: none; } </style> <style> .charge-item-header-popover { padding: 0; } .charge-item-header-popover .el-button { height: 80%; } </style>
四,遇到的問(wèn)題
點(diǎn)擊表格的某個(gè)搜索圖標(biāo),點(diǎn)擊輸入框,搜索區(qū)域消失,控制是否點(diǎn)擊目標(biāo)區(qū)域以外的元素是通過(guò)Clickoutside
指令實(shí)現(xiàn)的,下面是Clickoutside
指令的關(guān)鍵代碼:
function createDocumentHandler(el, binding, vnode) { return function (mouseup = {}, mousedown = {}) { if ( !vnode || !vnode.context || !mouseup.target || !mousedown.target || el.contains(mouseup.target) || el.contains(mousedown.target) || el === mouseup.target || (vnode.context.popperElm && (vnode.context.popperElm.contains(mouseup.target) || vnode.context.popperElm.contains(mousedown.target))) ) return; if ( binding.expression && el[ctx].methodName && vnode.context[el[ctx].methodName] ) { vnode.context[el[ctx].methodName](); } else { el[ctx].bindingFn && el[ctx].bindingFn(); } }; }
其中vnode
代表使用自定義指令的元素,vnode.context.popperElm
則代表使用自定義指令所在的vue
文件中data
屬性中的數(shù)據(jù),若這個(gè)值綁定的元素包含鼠標(biāo)點(diǎn)擊的元素(即搜索圖標(biāo))則Popver
彈出框不會(huì)消失,否則消失,其中popperElm
綁定的元素如下:
mounted() { // 解決點(diǎn)擊輸入框組件關(guān)閉問(wèn)題 this.popperElm = document.getElementsByClassName( "charge-item-header-popover" )[0]; },
以上說(shuō)明通過(guò)上面方法獲取的彈出框元素并不包含搜索圖標(biāo)(兩個(gè)元素不具有父子關(guān)系),但是從控制臺(tái)檢索元素看,兩個(gè)元素又確實(shí)是包含關(guān)系,后來(lái)想到原因如下
一個(gè)表格內(nèi)包含多個(gè)表頭搜索字段,而第二個(gè)搜索框肯定是不包含第一個(gè)搜索框圖標(biāo)的
五,解決
在獲取彈出框元素時(shí)傳給搜索框組件一個(gè)索引說(shuō)明是當(dāng)前頁(yè)面中的第幾個(gè)彈出框
父組件頁(yè)面
createElement(SearchCom, { props: { defaultValue: "", // 默認(rèn)值 selectIndex: item.popIndex || 1, //selectIndex代表當(dāng)前頁(yè)面的第幾個(gè)popper彈出框 }, on: { selectChange: (val) => self.selectFruitChange(val, item), }, }),
自定義彈出框組件
mounted() { // 解決點(diǎn)擊輸入框組件關(guān)閉問(wèn)題 this.popperElm = document.getElementsByClassName( "charge-item-header-popover" )[this.selectIndex - 1]; },
到此這篇關(guān)于el-table組件實(shí)現(xiàn)表頭搜索的文章就介紹到這了,更多相關(guān)el-table表頭搜索內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue-cli3自動(dòng)消除console.log()的調(diào)試信息方式
這篇文章主要介紹了vue-cli3自動(dòng)消除console.log()的調(diào)試信息方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-10-10解決Vue-cli3沒(méi)有vue.config.js文件夾及配置vue項(xiàng)目域名的問(wèn)題
這篇文章主要介紹了解決Vue-cli3沒(méi)有vue.config.js文件夾及配置vue項(xiàng)目域名的問(wèn)題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12使用vue-cli打包過(guò)程中的步驟以及問(wèn)題的解決
這篇文章主要介紹了使用vue-cli打包過(guò)程中的步驟以及問(wèn)題的解決,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-05-05vue實(shí)現(xiàn)拖動(dòng)左側(cè)導(dǎo)航欄變大變小
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)拖動(dòng)左側(cè)導(dǎo)航欄變大變小,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03vue?draggable組件實(shí)現(xiàn)拖拽及點(diǎn)擊無(wú)效問(wèn)題的解決
這篇文章主要介紹了vue?draggable組件實(shí)現(xiàn)拖拽及點(diǎn)擊無(wú)效問(wèn)題的解決,只需要在設(shè)置handle屬性就可以了,.defaultTypeTag 是要拖拽的塊的類(lèi)名,要注意的是需要做點(diǎn)擊事件的項(xiàng)不能包含在這個(gè)類(lèi)名里面,不然會(huì)無(wú)法觸發(fā)點(diǎn)擊事件,詳細(xì)解決辦法跟隨小編一起學(xué)習(xí)吧2022-05-05