el-table組件實現(xiàn)表頭搜索功能
一,展示效果

二,功能介紹
利用el-table組件提供的render-header屬性自定義表頭渲染內(nèi)容,包含表頭標題和一個搜索圖標,圖標是一個Popover組件彈出框,點擊圖標出現(xiàn)下面輸入框和搜索按鈕,點擊搜索區(qū)域以外的位置,搜索區(qū)域消失,這個使用的是element-ui的 Clickoutside 指令。
三,實現(xiàn)代碼
主頁面 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>主頁面 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: "", // 默認值
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="請輸入"
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: "", // 當前選中值
popperElm: "",
};
},
props: {
defaultValue: {
type: String,
default: "",
},
selectIndex: {
type: Number,
default: 0,
},
},
mounted() {
// 解決點擊輸入框組件關(guān)閉問題
this.popperElm = document.getElementsByClassName(
"charge-item-header-popover"
)[this.selectIndex - 1];
},
methods: {
// 點擊當前組件之外關(guān)閉
handleOutsideClick(e) {
setTimeout(() => {
this.visible = false;
}, 16);
},
// 展示當前組件時 鼠標光標定位到輸入框
showPopover() {
this.$nextTick(() => {
this.$refs.sInput.focus();
});
},
// 關(guān)閉當前組件
closeOver() {
this.visible = false;
},
popClick(e) {
this.visible = !this.visible;
},
// 輸入文字匹配對象的li項
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>四,遇到的問題
點擊表格的某個搜索圖標,點擊輸入框,搜索區(qū)域消失,控制是否點擊目標區(qū)域以外的元素是通過Clickoutside 指令實現(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ù),若這個值綁定的元素包含鼠標點擊的元素(即搜索圖標)則Popver彈出框不會消失,否則消失,其中popperElm綁定的元素如下:
mounted() {
// 解決點擊輸入框組件關(guān)閉問題
this.popperElm = document.getElementsByClassName(
"charge-item-header-popover"
)[0];
},以上說明通過上面方法獲取的彈出框元素并不包含搜索圖標(兩個元素不具有父子關(guān)系),但是從控制臺檢索元素看,兩個元素又確實是包含關(guān)系,后來想到原因如下
一個表格內(nèi)包含多個表頭搜索字段,而第二個搜索框肯定是不包含第一個搜索框圖標的
五,解決
在獲取彈出框元素時傳給搜索框組件一個索引說明是當前頁面中的第幾個彈出框
父組件頁面
createElement(SearchCom, {
props: {
defaultValue: "", // 默認值
selectIndex: item.popIndex || 1, //selectIndex代表當前頁面的第幾個popper彈出框
},
on: {
selectChange: (val) => self.selectFruitChange(val, item),
},
}),自定義彈出框組件
mounted() {
// 解決點擊輸入框組件關(guān)閉問題
this.popperElm = document.getElementsByClassName(
"charge-item-header-popover"
)[this.selectIndex - 1];
},到此這篇關(guān)于el-table組件實現(xiàn)表頭搜索的文章就介紹到這了,更多相關(guān)el-table表頭搜索內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
一,展示效果

二,功能介紹
利用el-table組件提供的render-header屬性自定義表頭渲染內(nèi)容,包含表頭標題和一個搜索圖標,圖標是一個Popover組件彈出框,點擊圖標出現(xiàn)下面輸入框和搜索按鈕,點擊搜索區(qū)域以外的位置,搜索區(qū)域消失,這個使用的是element-ui的 Clickoutside 指令。
三,實現(xiàn)代碼
主頁面 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>主頁面 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: "", // 默認值
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="請輸入"
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: "", // 當前選中值
popperElm: "",
};
},
props: {
defaultValue: {
type: String,
default: "",
},
selectIndex: {
type: Number,
default: 0,
},
},
mounted() {
// 解決點擊輸入框組件關(guān)閉問題
this.popperElm = document.getElementsByClassName(
"charge-item-header-popover"
)[this.selectIndex - 1];
},
methods: {
// 點擊當前組件之外關(guān)閉
handleOutsideClick(e) {
setTimeout(() => {
this.visible = false;
}, 16);
},
// 展示當前組件時 鼠標光標定位到輸入框
showPopover() {
this.$nextTick(() => {
this.$refs.sInput.focus();
});
},
// 關(guān)閉當前組件
closeOver() {
this.visible = false;
},
popClick(e) {
this.visible = !this.visible;
},
// 輸入文字匹配對象的li項
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>四,遇到的問題
點擊表格的某個搜索圖標,點擊輸入框,搜索區(qū)域消失,控制是否點擊目標區(qū)域以外的元素是通過Clickoutside 指令實現(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ù),若這個值綁定的元素包含鼠標點擊的元素(即搜索圖標)則Popver彈出框不會消失,否則消失,其中popperElm綁定的元素如下:
mounted() {
// 解決點擊輸入框組件關(guān)閉問題
this.popperElm = document.getElementsByClassName(
"charge-item-header-popover"
)[0];
},以上說明通過上面方法獲取的彈出框元素并不包含搜索圖標(兩個元素不具有父子關(guān)系),但是從控制臺檢索元素看,兩個元素又確實是包含關(guān)系,后來想到原因如下
一個表格內(nèi)包含多個表頭搜索字段,而第二個搜索框肯定是不包含第一個搜索框圖標的
五,解決
在獲取彈出框元素時傳給搜索框組件一個索引說明是當前頁面中的第幾個彈出框
父組件頁面
createElement(SearchCom, {
props: {
defaultValue: "", // 默認值
selectIndex: item.popIndex || 1, //selectIndex代表當前頁面的第幾個popper彈出框
},
on: {
selectChange: (val) => self.selectFruitChange(val, item),
},
}),自定義彈出框組件
mounted() {
// 解決點擊輸入框組件關(guān)閉問題
this.popperElm = document.getElementsByClassName(
"charge-item-header-popover"
)[this.selectIndex - 1];
},到此這篇關(guān)于el-table組件實現(xiàn)表頭搜索的文章就介紹到這了,更多相關(guān)el-table表頭搜索內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue-cli3自動消除console.log()的調(diào)試信息方式
這篇文章主要介紹了vue-cli3自動消除console.log()的調(diào)試信息方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-10-10
解決Vue-cli3沒有vue.config.js文件夾及配置vue項目域名的問題
這篇文章主要介紹了解決Vue-cli3沒有vue.config.js文件夾及配置vue項目域名的問題,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-12-12
vue?draggable組件實現(xiàn)拖拽及點擊無效問題的解決
這篇文章主要介紹了vue?draggable組件實現(xiàn)拖拽及點擊無效問題的解決,只需要在設(shè)置handle屬性就可以了,.defaultTypeTag 是要拖拽的塊的類名,要注意的是需要做點擊事件的項不能包含在這個類名里面,不然會無法觸發(fā)點擊事件,詳細解決辦法跟隨小編一起學習吧2022-05-05

