封装表格和标签组件

App.vue

复制代码
<template>
  <div class="table-case">
    <MyTable :data="goods">
    <template #TableHead>
      <tr>
          <th>编号</th>
          <th>名称</th>
          <th>图片</th>
          <th width="100px">标签</th>
        </tr>
    </template>
    <template #TableBody="{item,index}">
      <tr>
          <td>{{ index+1 }}</td>
          <td>{{ item.name }}</td>
          <td>
            <img :src="item.picture" />
          </td>
          <td>
            <MyTagVue :tag.sync="item.tag">
              <template>
                {{ item.tag }}
              </template>
            </MyTagVue>
          </td>
        </tr>
    </template>
    </MyTable>
  </div>
</template>

<script>

import MyTable from './components/MyTable.vue';
import MyTagVue from './components/MyTag.vue';
export default {
  name: 'TableCase',
  components: {
    MyTable,MyTagVue
  },
  data() {
    return {
      goods: [
        {
          id: 101,
          picture:
            'https://yanxuan-item.nosdn.127.net/f8c37ffa41ab1eb84bff499e1f6acfc7.jpg',
          name: '梨皮朱泥三绝清代小品壶经典款紫砂壶',
          tag: '茶具',
        },
        {
          id: 102,
          picture:
            'https://yanxuan-item.nosdn.127.net/221317c85274a188174352474b859d7b.jpg',
          name: '全防水HABU旋钮牛皮户外徒步鞋山宁泰抗菌',
          tag: '男鞋',
        },
        {
          id: 103,
          picture:
            'https://yanxuan-item.nosdn.127.net/cd4b840751ef4f7505c85004f0bebcb5.png',
          name: '毛茸茸小熊出没,儿童羊羔绒背心73-90cm',
          tag: '儿童服饰',
        },
        {
          id: 104,
          picture:
            'https://yanxuan-item.nosdn.127.net/56eb25a38d7a630e76a608a9360eec6b.jpg',
          name: '基础百搭,儿童套头针织毛衣1-9岁',
          tag: '儿童服饰',
        },
      ],
      chageTag:''
    }
  },
}
</script>

<style lang="less" scoped>
.table-case {
  width: 1000px;
  margin: 50px auto;
  img {
    width: 100px;
    height: 100px;
    object-fit: contain;
    vertical-align: middle;
  }

  .my-table {
    width: 100%;
    border-spacing: 0;
    img {
      width: 100px;
      height: 100px;
      object-fit: contain;
      vertical-align: middle;
    }
    th {
      background: #f5f5f5;
      border-bottom: 2px solid #069;
    }
    td {
      border-bottom: 1px dashed #ccc;
    }
    td,
    th {
      text-align: center;
      padding: 10px;
      transition: all 0.5s;
      &.red {
        color: red;
      }
    }
    .none {
      height: 100px;
      line-height: 100px;
      color: #999;
    }
  }

}
</style>
View Code
复制代码

Mytable.vue

复制代码
<template>
  <div class="">
    <table class="my-table">
      <thead>
        <slot name="TableHead"></slot>
      </thead>
      <tbody v-for="(item,index) in data" :key="item.id">
        <slot name="TableBody" :item="item" :index="index"></slot>
      </tbody>
    </table>
  </div>
</template>

<script>
export default {
    props:{
        data:Array
    },
    components:{
   
    }

}
</script>

<style lang="less" scoped>
.table-case {
  width: 1000px;
  margin: 50px auto;
  img {
    width: 100px;
    height: 100px;
    object-fit: contain;
    vertical-align: middle;
  }

  .my-table {
    width: 100%;
    border-spacing: 0;
    img {
      width: 100px;
      height: 100px;
      object-fit: contain;
      vertical-align: middle;
    }
    th {
      background: #f5f5f5;
      border-bottom: 2px solid #069;
    }
    td {
      border-bottom: 1px dashed #ccc;
    }
    td,
    th {
      text-align: center;
      padding: 10px;
      transition: all 0.5s;
      &.red {
        color: red;
      }
    }
    .none {
      height: 100px;
      line-height: 100px;
      color: #999;
    }
  }

}
</style>
View Code
复制代码

MyTag.vue

复制代码
<template>
  <div class="">
    
    <div class="my-tag">
    <input 
    v-if="isEdit"
    v-focus
    @blur="handleChage"
    class="input"
    type="text"
    :placeholder="Tag"
    />
    <div v-else class="text"
    @dblclick="isEdit=true"
    >
    <slot></slot>
    </div>
</div>
  </div>
</template>

<script>
export default {
    data(){
        return{
            isEdit:false,
            newTag:''
        }
    },
    props:{
        Tag:String
    },
    methods:{
        handleChage(e){
            if(e.target.value.trim()=='')return alert('标签内容不能为空')
            this.$emit("update:tag",e.target.value)
            this.isEdit=false
        }
    }
}
</script>

<style lang="less" scoped>
  .my-tag {
    cursor: pointer;
    .input {
      appearance: none;
      outline: none;
      border: 1px solid #ccc;
      width: 100px;
      height: 40px;
      box-sizing: border-box;
      padding: 10px;
      color: #666;
      &::placeholder {
        color: #666;
      }
    }
  }
</style>
View Code
复制代码

在app.vue里面可以实现表头,表内容的定制

posted @   超爱彬宝同学  阅读(3)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示