最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

el-table組件實(shí)現(xiàn)表頭搜索功能

 更新時(shí)間:2024年11月23日 12:03:51   作者:weixin_45723246  
文章介紹了如何使用`el-table`組件和`render-header`屬性自定義表頭,并添加搜索功能,通過(guò)`Popover`組件和`Clickoutside`指令實(shí)現(xiàn)點(diǎn)擊搜索區(qū)域外隱藏搜索框,解決了多個(gè)表頭搜索框共存時(shí)的沖突問(wèn)題,感興趣的朋友一起看看吧

一,展示效果

請(qǐng)?zhí)砑訄D片描述

二,功能介紹

利用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-uiClickoutside 指令。

三,實(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)文章希望大家以后多多支持腳本之家!

一,展示效果

請(qǐng)?zhí)砑訄D片描述

二,功能介紹

利用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-uiClickoutside 指令。

三,實(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?頁(yè)面刷新、重置、更新頁(yè)面所有數(shù)據(jù)的示例代碼

    vue?頁(yè)面刷新、重置、更新頁(yè)面所有數(shù)據(jù)的示例代碼

    Vue.js提供了多種方式來(lái)實(shí)現(xiàn)頁(yè)面刷新、重置和更新頁(yè)面所有數(shù)據(jù)的功能,下面通過(guò)示例代碼演示vue?頁(yè)面刷新、重置、更新頁(yè)面所有數(shù)據(jù),感興趣的朋友跟隨小編一起看看吧
    2024-01-01
  • Vue表單組件進(jìn)階之如何打造屬于你的自定義v-model

    Vue表單組件進(jìn)階之如何打造屬于你的自定義v-model

    在Vue中自定義組件是一個(gè)強(qiáng)大的功能,它允許你將可重用的代碼封裝成獨(dú)立的模塊,從而提高開(kāi)發(fā)效率和代碼的可維護(hù)性,這篇文章主要介紹了Vue表單組件進(jìn)階之如何打造屬于你的自定義v-model的相關(guān)資料,需要的朋友可以參考下
    2025-11-11
  • vue的注意規(guī)范之v-if 與 v-for 一起使用教程

    vue的注意規(guī)范之v-if 與 v-for 一起使用教程

    這篇文章主要介紹了vue的注意規(guī)范之v-if 與 v-for 一起使用方法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-08-08
  • 基于前端VUE+ElementUI實(shí)現(xiàn)table行上移或下移功能(支持跨頁(yè)移動(dòng))

    基于前端VUE+ElementUI實(shí)現(xiàn)table行上移或下移功能(支持跨頁(yè)移動(dòng))

    有時(shí)候需要前端實(shí)現(xiàn)上移和下移功能,下面這篇文章主要給大家介紹了關(guān)于如何基于前端VUE+ElementUI實(shí)現(xiàn)table行上移或下移(支持跨頁(yè)移動(dòng))的相關(guān)資料,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2024-07-07
  • Vue前端開(kāi)發(fā)之實(shí)現(xiàn)交錯(cuò)過(guò)渡動(dòng)畫(huà)效果

    Vue前端開(kāi)發(fā)之實(shí)現(xiàn)交錯(cuò)過(guò)渡動(dòng)畫(huà)效果

    這篇文章主要為大家詳細(xì)介紹了如何通過(guò)Vue實(shí)現(xiàn)交錯(cuò)過(guò)渡動(dòng)畫(huà)效果,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2024-11-11
  • Vue實(shí)現(xiàn)pdf在線(xiàn)預(yù)覽功能的示例代碼

    Vue實(shí)現(xiàn)pdf在線(xiàn)預(yù)覽功能的示例代碼

    這篇文章主要為大家詳細(xì)介紹了如何使用Vue實(shí)現(xiàn)pdf在線(xiàn)預(yù)覽功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2025-03-03
  • Vue-cli 如何將px轉(zhuǎn)化為rem適配移動(dòng)端

    Vue-cli 如何將px轉(zhuǎn)化為rem適配移動(dòng)端

    這篇文章主要介紹了Vue-cli 如何將px轉(zhuǎn)化為rem適配移動(dòng)端,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2024-07-07
  • vue2.0使用swiper組件實(shí)現(xiàn)輪播的示例代碼

    vue2.0使用swiper組件實(shí)現(xiàn)輪播的示例代碼

    下面小編就為大家分享一篇vue2.0使用swiper組件實(shí)現(xiàn)輪播的示例代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-03-03
  • vue路由懶加載工作原理

    vue路由懶加載工作原理

    Vue路由懶加載是一種優(yōu)化技術(shù),旨在減少應(yīng)用程序的初始加載時(shí)間并提高性能,這篇文章給大家介紹vue路由懶加載的相關(guān)知識(shí),感興趣的朋友跟隨小編一起看看吧
    2024-05-05
  • Element中Slider滑塊的具體使用

    Element中Slider滑塊的具體使用

    這篇文章主要介紹了Element中Slider滑塊的具體使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07

最新評(píng)論

永泰县| 凌源市| 威信县| 朝阳县| 长丰县| 克东县| 灌南县| 乐山市| 上蔡县| 和田县| 浦北县| 竹山县| 双鸭山市| 洞头县| 新巴尔虎右旗| 伊通| 赤峰市| 青铜峡市| 石首市| 盈江县| 珠海市| 南投县| 林州市| 仙游县| 高密市| 丰台区| 阳泉市| 南通市| 霞浦县| 滨州市| 疏勒县| 彭水| 德清县| 盘锦市| 阿拉善盟| 望谟县| 盈江县| 大渡口区| 清涧县| 汉中市| 丹棱县|