vue树组件循环表格

最近做项目需要实现循环表格这个需求,其中实用到了循环组件,特此记录一下,这是需要实现的功能,如下图:
在这里插入图片描述

vue中实现组件循环

父组件

<template>
	<div>
		<ul>
			<li v-for="(item,index) in asideList" ::key = "index" >
				<p class="main-title">
					<span class="title">{{item.title}}</span>
					<span class="iconfont icon-arrow-right-1"></span>
				</p>
				<ul v-if="item.children" >
					<AsideItem :asideChildren="item.children" ></AsideItem>
				</ul>
			</li>
		</ul>
	</div>
</template>

循环组件


<template>
	<div>
		<li v-for="(item,index) in asideChildren" v-bind:key="index" >
			<p class="secondary-title" @click.prevent="secondaryDropDown" >
				<span>{{item.title}}</span>
			</p>
			<ul v-if="item.children">
				<AsideItem :asideChildren="item.children"></AsideItem>
			</ul>			
		</li>
	</div>
</template>
 
<script>
	export default{
		name: 'AsideItem',
		props: {
			asideChildren: {
				type: Array
			}
		},
	}
</script>

表格实现

父组件:

<template>
  <div>
    <div class="title"><span>信息统计:已提交1{{  }}条,未提交:1{{  }}条,置回1{{ }}条,统计时间:1{{  }}</span></div>
    <div class="table_box border_righttopleft">
      <!-- thead -->
      <div class="head_box">
        <div class="head_title head_1 border_bottomright content">类别权重</div>
        <div class="head_title head_2 border_bottomright">序号</div>
        <div class="head_title head_3 border_bottomright">指标权重</div>
        <div class="head_title head_4 border_bottomright">评分内容</div>
        <div class="head_title head_6 border_bottomright">提示内容</div>
        <div class="head_title head_7 border_bottomright">信息录入</div>
      </div>
      <div class="tableBody" v-for="(item,index) in dataInfo" :key="index">
        <div class="clow">
          <!-- 类别权重 -->
          <div class="clow1" :style="{ height:item.total*40 + 'px' }">{{ item.name }}</div>
          <!-- 序号 -->
          <div class="clow2">
            <div v-for="(item2,index2) in item.children" :style="{ height: item2.lengths*40 + 'px' }">
              <div class="clow2Bottom" :style="{height:item2.length*40+'px'}">{{ index2+1 }}</div>
            </div>
          </div>
          <!-- 指标权重 -->
          <div class="clow3">
            <div v-for="(item2,index2) in item.children">
              <div style="float: left;" class="clow3Top" :style="{height:item2.length*40+'px'}">
                {{ item2.name }}
              </div>
              <!-- 树最近组件 -->
              <div v-if="item2.children" style="float: left;width: 1018px;">
                <AsideItem :asideChildren="item2.children"></AsideItem>
              </div>
            </div>
          </div>

        </div>

      </div>
    </div>
  </div>
</template>
<script>
import AsideItem from "./tree.vue";
export default {
  name: "environmentEntry",
  components: { ceshi, AsideItem },
  data() {
    return {
      Tabheight: 440,
      total: 14,
      dataInfo: [
        {
          id: "1",
          name: "单位环境管理信息20分",
          total: 14,
          children: [
            {
              id: "2",
              name: "环保责任制3分",
              length: 7,
              // 数据看这
              children: [
                {
                  id: "31",
                  name: "1-1",
                  length: 4,
                  children: [
                    {
                      id: "41",
                      name: "21",
                      length: 2,
                      children: [
                        {
                          id: "51",
                          name: "最后一层",
                        },
                        {
                          id: "52",
                          name: "大花脸",
                        },
                      ],
                    },
                    {
                      id: "41",
                      name: "地二层",
                    },
                    {
                      id: "41",
                      name: "第二程2层",
                    },
                  ],
                },
                { id: "32", name: "1-2" },
                { id: "33", name: "1-3" },
                { id: "34", name: "1-4" },
              ],
            },
            {
              id: "3",
              name: " 2分",
              length: 2,
              children: [
                { id: "35", name: "2" },
                {
                  id: "36",
                  name: "2",
                },
              ],
            },
            {
              id: "4",
              name: "3",
              length: 3,
              children: [
                { id: "37", name: "3" },
                { id: "38", name: "3" },
                { id: "39", name: "3" },
              ],
            },
            {
              id: "5",
              name: "4",
              length: 2,
              children: [
                { id: "40", name: "4" },
                { id: "41", name: "4" },
              ],
            },
          ],
        },
        {
          id: "6",
          name: "环保行政管理信息75分",
          total: 8,
          // 最后一层所有的条数
          children: [
            {
              id: "7",
              name: "环保行政许可管理25分",
              length: 8,
              children: [
                {
                  id: "8",
                  name: "建设项目环境影响评价文件审批",
                  length: 2,
                  children: [
                    { id: "9", name: "登记表类项目未依法备案" },
                    {
                      id: "11",
                      name: "报告书类项目(未依法报批、未重新报批、未报请重新审核,未经批准、未经原审批部门重新审核)",
                    },
                  ],
                },
                {
                  id: "12",
                  name: "排污许可证管理",
                  length: 1,
                  children: [{ name: "实行排污许可重点管理的企业事业单位" }],
                },
                {
                  id: "13",
                  name: "危险废物经营许可证管理",
                  length: 1,
                  children: [
                    {
                      name: "实行排污许可重点管理的企业事业单位",
                      id: "56",
                      length: 1,
                      children: [{ name: "hihuihuihuihiuh" }],
                    },
                  ],
                },
                { id: "14", name: "危险废物转移审批" },
                { id: "15", name: "危险废物转移审批23" },
                { id: "16", name: "危险废物转移审批45" },
                { id: "17", name: "危险废物转移审批45" },
              ],
            },
          ],
        },
      ],
    };
  },
  mounted() {},
  watch: {},
  created() {},
  methods: {
    tips(data) {
      console.log(1111111);
      console.log(data, "pppp");
    },
    manage(data) {
      console.log(data, "pppp");
    },
  },
};
</script>
<style lang="less" scoped>
.title {
  text-align: center;
  font-size: 16px;
}
.table_box {
  width: 1361px;
  margin: 0 auto;
}
.tableBody {
  // border-bottom: 1px solid #888888;
  border-right: 1px solid #888888;
}
.border_righttopleft {
  border-top: 1px solid #888888;
  border-left: 1px solid #888888;
}
.clow {
  display: flex;
  text-align: center;
  align-items: center;
  // div{border:1px solid #888888;}
}
.clow1 {
  width: 141px;
  border-right: 1px solid #888888;
  border-bottom: 1px solid #888888;
}
.clow2 {
  width: 60px;
  border-right: 1px solid #888888;
}
.clow2Bottom {
  border-bottom: 1px solid #888888;
}
.clow3 {
  width: 1160px;
  display: flex;
  flex-direction: column;
  flex-wrap: nowrap;
}
.clow4 {
  width: 900px;
}
.clow5 {
  width: 60px;
}
.clow6 {
  width: 60px;
}
.clow3Top {
  width: 140px;
  border-right: 1px solid #888888;
  border-bottom: 1px solid #888888;
}
.clow3Top:last-child {
  // border-bottom: none;
}
.content {
  border-right: 1px, solid #888888;
  border-bottom: 1px solid #888888;
}
.content:last-child {
  // border-bottom:none;
}
.button {
  border-left: 1px, solid #888888;
}
.border_bottomright {
  border-bottom: 1px solid #888888;
  border-right: 1px solid #888888;
}

.border_bottom {
  border-bottom: 1px solid #888888;
}

.border_bottomleft {
  border-bottom: 1px solid #888888;
  border-left: 1px solid #888888;
  border-right: 1px solid #888888;
}

/* 头部样式 */
.head_box {
  display: flex;
}

.head_title {
  font-size: 12px;
  text-align: center;
}

.border_bottomleft {
  font-size: 12px;
}

.head_1 {
  width: 140px;
  height: 30px;
  line-height: 30px;
}

.head_2 {
  width: 60px;
  height: 30px;
  line-height: 30px;
  text-align: center;
}

.head_3 {
  width: 140px;
  height: 30px;
  line-height: 30px;
}

.head_4 {
  width: 900px;
  height: 30px;
  line-height: 30px;
}

/*  .head_5 {
	width: 170px;
	height: 30px;
	line-height: 30px;
}  */
.head_6 {
  width: 60px;
  height: 30px;
  line-height: 30px;
}

.head_7 {
  width: 60px;
  height: 30px;
  line-height: 30px;
}

/*  .body_5 {
	width:170px;
} */
.body_6 {
  width: 60px;
}

.body_1 {
  width: 140px;
}
.secondText {
  border-bottom: 1px solid #888888;
  border-right: 1px solid #888888;
}
.thirdText {
  border-bottom: 1px solid #888888;
}
</style>

子组件

<template>
  <div class="clowTable">
    <div class="clotableList" v-if="asideChildren" v-for="(item,index) in asideChildren" v-bind:key="index">
      <div v-if="item.children" class="content" :style="{height:item.length*40+'px',with:900-(item.ceng)*200+'px'}">
        <div class="secondary-title" style="text-align: left;width: 200px;" :style="{height:item.length*40+'px'}">
          {{item.name}}
        </div>
        <div>
          <AsideItem :asideChildren="item.children"></AsideItem>
        </div>
      </div>
      <!-- 无子集 -->
      <!-- 层数 -->
      <div v-if="!item.children" class="content" style="height: 40px;" :style="{with:1018-(item.ceng)*200+'px'}">
        <div style="float: left;"> {{ item.name }}</div>
        <div style="float: right;width: 120px;display: flex;height: 40px;">
          <div class="button" style="width: 58px;border-left: 1px solid #888888;"><Button type="primary" size="small"
              @click="tips(item)">提示</Button></div>
          <div class="button" style="width: 58px; border-left: 1px solid #888888;"><Button type="primary" size="small"
              @click="manage(item)">管理</Button></div>
        </div>
      </div>
    </div>
  </div>
</template>
 
<script>
export default {
  name: "AsideItem",
  props: {
    asideChildren: {
      type: Array,
    },
  },
  methods:{
    tips(data) {
      console.log(1111111);
      console.log(data, "pppp");
    },
    manage(data) {
      console.log(data, "pppp");
    },
  }
};
</script>
<style scoped>
.clowTable {
}
.content {
  border-bottom: 1px solid #88888888;
}
.secondary-title {
  border-right: 1px solid #888888;
  float: left;
}
</style>


最终实现功能。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单的示例代码,可以帮助你自定义一个表格组件: ```vue <template> <table> <thead> <tr> <th v-for="header in headers" :key="header">{{ header }}</th> </tr> </thead> <tbody> <tr v-for="(row, index) in rows" :key="index"> <td v-for="(item, key) in row" :key="key">{{ item }}</td> </tr> </tbody> </table> </template> <script> export default { props: { headers: { type: Array, required: true }, rows: { type: Array, required: true } } } </script> ``` 在这个组件中,我们使用了两个 props:headers 和 rows。headers 表示表格的表头,是一个字符串数组;rows 表示表格的数据行,是一个对象数组。在组件的模板中,我们使用了 v-for 指令来渲染表头和数据行。在表头中,我们使用了 v-for 和 :key 指令来循环渲染表头中的每一个单元格。在数据行中,我们使用了 v-for 和 :key 指令来循环渲染每一行中的每一个单元格。 你可以在父组件中使用这个组件来渲染一个自定义表格: ```vue <template> <div> <my-table :headers="headers" :rows="rows"></my-table> </div> </template> <script> import MyTable from './MyTable.vue' export default { components: { MyTable }, data () { return { headers: ['Name', 'Age', 'Gender'], rows: [ { name: 'Alice', age: 25, gender: 'Female' }, { name: 'Bob', age: 30, gender: 'Male' }, { name: 'Charlie', age: 20, gender: 'Male' } ] } } } </script> ``` 在这个父组件中,我们通过调用 MyTable 组件,并传入 headers 和 rows 两个 props 来渲染一个自定义表格。注意,父组件中的数据格式必须符合 MyTable 组件 props 的要求,即 headers 必须是一个字符串数组,rows 必须是一个对象数组。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值