溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

如何使用Element實(shí)現(xiàn)表格表頭添加搜索圖標(biāo)和功能

發(fā)布時(shí)間:2022-07-29 11:25:27 來源:億速云 閱讀:405 作者:iii 欄目:開發(fā)技術(shù)

今天小編給大家分享一下如何使用Element實(shí)現(xiàn)表格表頭添加搜索圖標(biāo)和功能的相關(guān)知識點(diǎn),內(nèi)容詳細(xì),邏輯清晰,相信大部分人都還太了解這方面的知識,所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來了解一下吧。

Element 表格表頭添加搜索圖標(biāo)和功能

如何使用Element實(shí)現(xiàn)表格表頭添加搜索圖標(biāo)和功能

如何使用Element實(shí)現(xiàn)表格表頭添加搜索圖標(biāo)和功能

主要實(shí)現(xiàn) table的slot=‘header’

  • headerData是表頭的循環(huán)數(shù)組

  • tableData是表格內(nèi)容的數(shù)組

  • <template slot="header"></template> 自定義表頭的內(nèi)容

  • 注意:在使用<template slot="header"></template>的時(shí)候,只會顯示表頭的自定義內(nèi)容,表格的內(nèi)容還需要使用<template slot-scope="scope"> {{ scope.row }} </template> scope.row會顯示出該列的所有內(nèi)容

  • 如果<template slot='header'></template>不使用slot-scope='scope'會出現(xiàn)不能輸入的問題

  • Vue 2.6+版本的插槽語法使用#header替換<template slot='header' slot-scope='scope'></template>Vue的作用域插槽

<template>
    <el-table :data="tableData" >
        <template v-for="(headerItem, headerIndex) in headerData">
            <!-- 下拉框選擇器 -->
            <el-table-column
                v-if="headerItem.select"
                :label="headerItem.label"
                :prop="headerItem.prop"
                :key="headerIndex"
            >
            	<!-- 表頭的 slot -->
                <template #header>
                    <el-popover placement="bottom" title="請選擇" width="200" trigger="click">
                        <div slot="reference" class="search-header">
                            <span class="search-title">{{ headerItem.label }}</span>
                            <i class="search-icon el-icon-search"></i>
                        </div>
                        <el-select v-model="headerItem.selectValue" placeholder="請選擇">
                            <el-option
                                v-for="item in headerItem.selectOptions"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value"
                            >
                            </el-option>
                        </el-select>
                    </el-popover>
                </template>
                <!-- 表格的 內(nèi)容 slot -->
                <template slot-scope="scope">
                    {{ scope.row[headerItem.prop] }}
                </template>
            </el-table-column>
            <!-- 日期選擇器 -->
            <el-table-column
                v-else-if="headerItem.dateSelect"
                :label="headerItem.label"
                :prop="headerItem.prop"
                :key="headerIndex"
            >
                <template #header>
                    <el-popover placement="bottom" title="請選擇" trigger="click">
                        <div class="search-box" slot="reference">
                            <span class="search-title">{{ headerItem.label }}</span>
                            <i class="el-icon-arrow-down search-icon"></i>
                        </div>
                        <el-date-picker
                            type="daterange"
                            range-separator="至"
                            start-placeholder="開始日期"
                            end-placeholder="結(jié)束日期"
                        >
                        </el-date-picker>
                    </el-popover>
                </template>
                <template slot-scope="scope">
                    {{ scope.row[headerItem.prop] }}
                </template>
            </el-table-column>
            <!-- 輸入框 -->
            <el-table-column
                v-else-if="headerItem.inputSelect"
                :label="headerItem.label"
                :prop="headerItem.prop"
                :key="headerIndex"
            >
                <template #header>
                    <el-popover placement="bottom" title="請選擇" trigger="click">
                        <div slot="reference" class="search-header">
                            <span class="search-title">{{ headerItem.label }}</span>
                            <i class="search-icon el-icon-search"></i>
                        </div>
                        <el-input />
                    </el-popover>
                </template>
                <template slot-scope="scope">
                    {{ scope.row[headerItem.prop] }}
                </template>
            </el-table-column>
            <el-table-column v-else :label="headerItem.label" :prop="headerItem.prop" :key="headerIndex">
            </el-table-column>
        </template>
    </el-table>
</template>

js代碼

export default {
    data() {
        return {
            headerData: [
                {
                    label: '日期',
                    prop: 'date',
                    dateSelect: true,
                },
                {
                    label: '名稱',
                    prop: 'name',
                    inputSelect: true,
                },
                {
                    label: '類型',
                    prop: 'type',
                    select: true,
                    selectValue: null,
                    selectOptions: [
                        {
                            value: 'Vue',
                            label: 'Vue',
                        },
                        {
                            value: 'React',
                            label: 'React',
                        },
                        {
                            value: 'Angular',
                            label: 'Angular',
                        },
                    ],
                },
            ],
            tableData: [
                {
                    date: '2016-05-02',
                    name: '王小虎',
                    type: 'Vue',
                },
                {
                    date: '2016-05-04',
                    name: '王小虎',
                    type: 'React',
                },
                {
                    date: '2016-05-01',
                    name: '王小虎',
                    type: 'Angular',
                },
            ],
        }
    },
}

element ui表格el-tabel給表頭加icon圖標(biāo)

設(shè)置 Scoped slot 來自定義表頭

<el-table :data="mockTableData" >
    <el-table-column prop="status">
        <template slot="header">類型 <i class="icon"></i></template>
    </el-table-column>
</el-table>

以上就是“如何使用Element實(shí)現(xiàn)表格表頭添加搜索圖標(biāo)和功能”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,小編每天都會為大家更新不同的知識,如果還想學(xué)習(xí)更多的知識,請關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI