当前位置:   article > 正文

Vue 组件封装之 List 列表_vue list

vue list

一、List 列表

组件说明:
实现 List 列表布局排版。

效果展示:

在这里插入图片描述

实现的功能:

  1. 在一个 List 中实现左中右三栏布局;
  2. 左边为文本 label 标签,纯文字展示。可通过 require 字段来判断是否必填,必填项则前面会有红色的 *。
  3. 中间为输入值 input 标签。
  4. 右边为图标 img 标签,可通过 icon 属性来判断是否展现图标。并且暴露出该图标的点击事件。

二、使用案例

<template>
     <div>
     <el-list
          :list="textList"        
          :result="item"
           width="80px"
        />
      </div>
</template>
<script>
import address from '../assets/address.png';
import right from '../assets/right.png';
export default {
  data(){
     return {
     textList:[
          {label:"地址",field:"address",icon:address,require:true,onRightClick:this.choseAddress},
          {label:"学历",field:"education",icon:right,require:true,onRightClick:this.choseEducation},
          {label:"电话",field:"telephone",require:true},
          {label:"人口",field:"population",disabled:true,require:true,suffixUnit:"万"},
          {label:"收入",field:"income",require:true,prefixUnit:"$"},
          {label:"网址",field:"website"},
        ],
        item:{
          address:"",
          education:"",
          telephone:"",
          population:"1000",
          income:"200",
          website:"",
        },
    }
  }
  methods: {
      choseAddress(){      
          this.item.address="上海市浦东新区";
      },
      choseEducation(){
       this.item.education="本科";
      },
  }
}
</script>	
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43

三、API 使用指南

属性说明类型默认值
list页面展示的静态内容集合Array[]
label左边展示文本String
field中间输入值字段String
icon右边的图标String
onRightClick点击右边图标时触发事件,为了良好的用户体验,事件绑在在行Function
require是否为必输字段Boolean
disabled值是 true 或者 disabled 则为反显值,否则为输入值Booleanfalse
prefixUnit输入值前缀,比如输入值表示收入1000。加个前缀 “¥”,则输入值为“¥1000”String
suffixUnit输入值后缀,比如输入值表示人口1000。加个后缀 “万”,则输入值为“1000万”String
result对应的字段值集合Object{}
width固定左边文本展示的宽度String

四、源代码

List.vue
文件路径:share/list/List.vue

<template>
  <div>
  <div v-for="(item,index) in list" @click="onRightClick(item)" class="cm-flex cm-ai-fs cm-jc-sb cm-p-02 cm-border-bottom-eee">
    <div class="cm-flex cm-ai-fs cm-flex-1 cm-mr-02">
      <div class="cm-fs-030 cm-mr-04" :style="getWidth()">
        <span class="cm-c-red cm-pt-01" v-if="item.require">*</span>{{item.label}}
      </div>
      <span class="cm-mr-02 cm-c-999 cm-flex-1" v-if="item.icon||item.disabled">
        <span v-if="item.prefixUnit">{{item.prefixUnit}}</span>
        {{result[item.field]}}
        <span v-if="item.suffixUnit">{{item.suffixUnit}}</span></span>
      <input type="text" v-model="result[item.field]" class="item-input" v-if="!item.icon&&!item.disabled">
    </div>
    <img :src="item.icon" alt="" class="cm-img-03" v-if="item.icon">
  </div>
  </div>
</template>
<script>
  export default {
    name: "ElList",
    data(){
      return{
      }
    },
    //获取子组件传过来的激活tab
    props:{
      list:{
        type: Array,
        default: function () {
          return [];
        }
      },
      result:{
        type:Object,
        default:{}
      },
      width:{
        type:String,
        default:""
      }
    },
    created(){

    },
    methods:{
      getWidth(){
        if(this.width){
            return "width:"+this.width
        }
      },
      onRightClick(item){
          if(item.onRightClick){
            item.onRightClick();
          }
      }
    }
  }
</script>

<style scoped>
  .item-input{
    height: 30px;
    font-size: 0.3rem;
    flex: 1;
    border: none;
    outline: none;
    background: #fff;
  }
</style>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69

index.js
文件路径:share/list/index.js

import List from './List.vue';

/* istanbul ignore next */
List.install = function(Vue) {
  Vue.component(List.name, List);
};

export default List;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

注:里面用到一些公共样式,公共样式内容比较多,就不贴出来了。有需要的留言。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/231534
推荐阅读
相关标签
  

闽ICP备14008679号