elementUI自定義組件--單(多)選

主要是記錄,以備不時之需。
效果圖:


項目中截圖

不多說,直接上代碼。

<template>
    <ul>
        <li class="tempItem" v-for="(item,index) in tempList" :key="index"
            @click="tempItemClick(item,index)"
            :class="{'temp-active':checkArr.includes(item[options.id])}"
            :style="{'width':width}" >

            {{item[options.label]}}
            <div class="triangle">
                <span class="check"></span>
            </div>
        </li>
    </ul>
</template>

<script>
    export default {
        name: 'tempList',
        props: {
            //必傳,選項列表
            tempList: {
                type: Array,
                required: true,
                default () {
                    return []
                }
            },
            //默認寬度200px,支持百分比
            width: {
                type: String,
                default () {
                    return '200px'
                }
            },
            //是否支持多選,默認false
            multiple: {
                type: Boolean,
                default () {
                    return false
                }
            },
            //選中項列表
            checkArr: {
                type: Array,
                default () {
                    return []
                }
            },
            //列表項鍵值對(默認顯示值-label,后臺存值-id)
            options: {
                type: Object,
                default () {
                    return {
                        label: 'label',
                        id: 'id'
                    }
                }
            }
        },
        methods: {
            tempItemClick(item){
                let id = item[this.options.id];
                this.checkArr.includes(id) ? 
                this.checkArr.splice(this.checkArr.indexOf(id),1) :
                this.multiple ? this.checkArr.push(id) : this.$set(this.checkArr,0,(id));
            }
        }
    }
</script>

<style lang="scss" scoped>
    .temp-active{
        color: #2476c2 !important;
        border-color: #2476c2 !important;
        /* 三角形 */
        .triangle {
            position: absolute;
            right: 0;
            top: 0;
            width: 0;
            height: 0;
            border: 11px solid #2476c2;
            border-left: 11px solid transparent;
            border-bottom: 11px solid transparent;
        }
        /* 對號 */
        .check {
            position: relative;
            display: inline-block;
            width: 25px;
            height: 25px;
            border-radius: 25px;
        }
        .check::after {
            content: "";
            position: absolute;
            left: -16px;
            top: -8px;
            width: 40%;
            height: 22%;
            border: 2px solid #fff;
            border-radius: 1px;
            border-top: none;
            border-right: none;
            background: transparent;
            transform: rotate(-45deg);
        }
    }
    .tempItem{
        position: relative;
        display: inline-block;
        // width: 200px;
        height: 30px;
        line-height: 30px;
        font-size: 16px;
        color: #DADADA;
        text-indent: 1em;
        white-space: nowrap;
        text-overflow: ellipsis;
        overflow: hidden;
        word-break: break-all;
        border: 1px solid #DADADA;
        cursor: pointer;
        margin: 0 10px 10px 0;
    }
    
</style>

使用示例:

<TemplateSelect
   :options="{label:'name',id:'pkId'}"
   :tempList="formList"
   multiple
   width="20%"
   :checkArr="formData.formId"
 />
...
...
import TemplateSelect from "@/components/TemplateSelect";

checkArr最好傳入,可以直接在父組件拿值不需要子組件往上傳(vue子組件直接修改父組件屬性是會報錯的,數(shù)組不會,而且也沒發(fā)現(xiàn)什么副作用,所以安啦)。
OK!dawanshougong!

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容