欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

一,展示效果

請?zhí)砑訄D片描述

二,功能介紹

利用el-table組件提供的render-header屬性自定義表頭渲染內(nèi)容,包含表頭標題和一個搜索圖標,圖標是一個Popover組件彈出框,點擊圖標出現(xiàn)下面輸入框和搜索按鈕,點擊搜索區(qū)域以外的位置,搜索區(qū)域消失,這個使用的是element-uiClickoutside 指令。

三,實現(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: "", // 當(dāng)前選中值
      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: {
    // 點擊當(dāng)前組件之外關(guān)閉
    handleOutsideClick(e) {
      setTimeout(() => {
        this.visible = false;
      }, 16);
    },
    // 展示當(dāng)前組件時 鼠標光標定位到輸入框
    showPopover() {
      this.$nextTick(() => {
        this.$refs.sInput.focus();
      });
    },
    // 關(guān)閉當(dāng)前組件
    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)包含多個表頭搜索字段,而第二個搜索框肯定是不包含第一個搜索框圖標的

五,解決

在獲取彈出框元素時傳給搜索框組件一個索引說明是當(dāng)前頁面中的第幾個彈出框

父組件頁面

     createElement(SearchCom, {
         props: {
           defaultValue: "", // 默認值
           selectIndex: item.popIndex || 1, //selectIndex代表當(dāng)前頁面的第幾個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)文章希望大家以后多多支持腳本之家!

一,展示效果

請?zhí)砑訄D片描述

二,功能介紹

利用el-table組件提供的render-header屬性自定義表頭渲染內(nèi)容,包含表頭標題和一個搜索圖標,圖標是一個Popover組件彈出框,點擊圖標出現(xiàn)下面輸入框和搜索按鈕,點擊搜索區(qū)域以外的位置,搜索區(qū)域消失,這個使用的是element-uiClickoutside 指令。

三,實現(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: "", // 當(dāng)前選中值
      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: {
    // 點擊當(dāng)前組件之外關(guān)閉
    handleOutsideClick(e) {
      setTimeout(() => {
        this.visible = false;
      }, 16);
    },
    // 展示當(dāng)前組件時 鼠標光標定位到輸入框
    showPopover() {
      this.$nextTick(() => {
        this.$refs.sInput.focus();
      });
    },
    // 關(guān)閉當(dāng)前組件
    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)包含多個表頭搜索字段,而第二個搜索框肯定是不包含第一個搜索框圖標的

五,解決

在獲取彈出框元素時傳給搜索框組件一個索引說明是當(dāng)前頁面中的第幾個彈出框

父組件頁面

     createElement(SearchCom, {
         props: {
           defaultValue: "", // 默認值
           selectIndex: item.popIndex || 1, //selectIndex代表當(dāng)前頁面的第幾個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下載文件以及文件重命名方式

    vue下載文件以及文件重命名方式

    這篇文章主要介紹了vue下載文件以及文件重命名方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-10-10
  • vue-cli3自動消除console.log()的調(diào)試信息方式

    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項目域名的問題

    這篇文章主要介紹了解決Vue-cli3沒有vue.config.js文件夾及配置vue項目域名的問題,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-12-12
  • 基于Vue3實現(xiàn)高性能拖拽指令

    基于Vue3實現(xiàn)高性能拖拽指令

    在現(xiàn)代前端開發(fā)中,拖拽功能是增強用戶體驗的重要手段之一,本文將詳細介紹如何在Vue3中封裝一個拖拽指令并通過實戰(zhàn)例子演示其實現(xiàn)過程,希望對大家有所幫助
    2024-11-11
  • 使用vue-cli打包過程中的步驟以及問題的解決

    使用vue-cli打包過程中的步驟以及問題的解決

    這篇文章主要介紹了使用vue-cli打包過程中的步驟以及問題的解決,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-05-05
  • vue實現(xiàn)拖動左側(cè)導(dǎo)航欄變大變小

    vue實現(xiàn)拖動左側(cè)導(dǎo)航欄變大變小

    這篇文章主要為大家詳細介紹了vue實現(xiàn)拖動左側(cè)導(dǎo)航欄變大變小,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • 深入淺析vue-cli@3.0 使用及配置說明

    深入淺析vue-cli@3.0 使用及配置說明

    這篇文章主要介紹了vue-cli@3.0 使用及配置說明,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-05-05
  • Vue之文件加載執(zhí)行全流程

    Vue之文件加載執(zhí)行全流程

    這篇文章主要介紹了Vue之文件加載執(zhí)行全流程,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-04-04
  • vue?draggable組件實現(xiàn)拖拽及點擊無效問題的解決

    vue?draggable組件實現(xiàn)拖拽及點擊無效問題的解決

    這篇文章主要介紹了vue?draggable組件實現(xiàn)拖拽及點擊無效問題的解決,只需要在設(shè)置handle屬性就可以了,.defaultTypeTag 是要拖拽的塊的類名,要注意的是需要做點擊事件的項不能包含在這個類名里面,不然會無法觸發(fā)點擊事件,詳細解決辦法跟隨小編一起學(xué)習(xí)吧
    2022-05-05
  • vue實現(xiàn)分頁加載效果

    vue實現(xiàn)分頁加載效果

    這篇文章主要為大家詳細介紹了vue實現(xiàn)分頁加載效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-12-12

最新評論