Vue动态添加删除div

栏目: JavaScript · 发布时间: 6年前

内容简介:小结vue实现动态加载div,主要利用vue中的v-for指令,由于与数据源双向绑定的特性,可以通过修改数据源从而动态加载或删除div。

效果图

Vue动态添加删除div

完整源码

<template>

<div class="boxShadow">
    <div>
      <el-button style="width:90px; font-size: 12px">全部扫描</el-button>
      <el-button style="width:90px; font-size: 12px">全部清除</el-button>
    </div>
    <div>

    <div style="margin-top: 15px;" v-for="(item, pos) in divList">
      <el-input placeholder="请输入仓库地址,如有分支则空格隔开加分支名称,默认不加检测master分支。" v-model="item.gitUrl" class="input-with-select">
        
        <!-- 编码规则 -->
        <template>
          <el-select v-model="item.codeType" slot="prepend" placeholder="请选择编码规则" style="width: 150px">
            <el-option
              v-for="opItem in options"
              :key="opItem.value"
              :label="opItem.label"
              :value="opItem.value">
            </el-option>
          </el-select>
        </template>

        <!-- 删除 -->
        <el-button slot="append" @click="deleteNode(pos)">删除</el-button>
      </el-input>
    </div>

    <div style="width: 100%; text-align:center; margin-top: 15px; margin-bottom: 15px">
      <!-- 增加 -->
      <el-button icon="el-icon-plus" @click="addNode()"></el-button>
    </div>

    <el-table
      :data="data"
      :header-cell-style="tableHeaderColor"
      ref='multipleTable'
      border
      stripe
      style="width: 100%; margin-top: 20px; font-size: 10px">

        <el-table-column
          align="center"
          prop="projectName"
          label="仓库名"
          width="200">
          <template slot-scope="scope">
            <div class="tv-branch"> {{ scope.row.projectName }}</div>
          </template>
        </el-table-column>

        <el-table-column
          class="tv-branch"
          align="center"
          prop="jiraId"
          label="分支名">
          <template slot-scope="scope">
            <div class="tv-branch"> {{ scope.row.jiraId }}</div>
          </template>
        </el-table-column>

        <el-table-column label="文件数" min-width="130px" align="center">
          <template slot-scope="scope">
            <div v-for="item in scope.row.appVos" class="tv-branch"> {{ item.appName }}</div>
          </template>
        </el-table-column>

        <el-table-column label="不规范项" min-width="130px" align="center">
          <template slot-scope="scope">
            <div v-for="item in scope.row.appVos" class="tv-branch"> {{ item.branchName }}</div>
          </template>
        </el-table-column>

        <el-table-column label="SonarQube地址" min-width="130px" align="center">
          <template slot-scope="scope">
            <div v-for="item in scope.row.appVos" class="tv-branch"> {{ item.branchName }}</div>
          </template>
        </el-table-column>

        <el-table-column
          class="tv-branch"
          align="center"
          prop="updatedTime"
          label="扫描时间">
          <template slot-scope="scope">
            <div class="tv-branch"> {{ scope.row.updatedTime }}</div>
          </template>
        </el-table-column>

    </el-table>
  </div>
  </div>

</template> 
<script type='text/javascript'>

  export default {
    name: 'xxx',

    methods: {

      // 修改table header的背景色
      tableHeaderColor({ row, column, rowIndex, columnIndex }) {
        if (rowIndex === 0) {
          return 'background-color: #f0f9eb'
          // return 'background-color: #f0f9eb; color: #fff; font-weight: 500;'
        }
      },


      //添加标本div
      addNode() {
        this.divList.push({"codeType": "", "gitUrl": ""});
      },
      //删除样本div
      deleteNode(i) {
        this.divList.splice(i, 1);  //删除index为i,位置的数组元素
      },

    },

    
    data() {
      return {
        data: [],

        options: [{
          value: '选项1',
          label: 'Java'
        }, {
          value: '选项2',
          label: 'C#'
        }, {
          value: '选项3',
          label: 'iOS'
        }, {
          value: '选项4',
          label: 'Js'
        }, {
          value: '选项5',
          label: 'Python'
        }],

        divList: [
          {"codeType": "", "gitUrl": ""}
        ],
      
      }
      
    },

  }


</script>

<style lang="less" scoped>
  .clean{
      clear:both;
  }

  .tv-branch{
    // display: table-cell;
    vertical-align: middle;
    text-align: center;
    margin-top: 10px;
    margin-bottom: 10px;
    margin-left: auto;
    margin-right: auto;
    text-align: left;
}
  .el-select .el-input {
    width: 130px;
  }
  .input-with-select .el-input-group__prepend {
    background-color: #fff;
  }
</style>

小结

vue实现动态加载div,主要利用vue中的v-for指令,由于与数据源双向绑定的特性,可以通过修改数据源从而动态加载或删除div。


以上所述就是小编给大家介绍的《Vue动态添加删除div》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

百面机器学习

百面机器学习

诸葛越、葫芦娃 / 人民邮电出版社 / 2018-8-1 / 89.00元

人工智能领域正在以超乎人们想象的速度发展,本书赶在人工智能彻底占领世界之前完成编写,实属万幸。 书中收录了超过100道机器学习算法工程师的面试题目和解答,其中大部分源于Hulu算法研究岗位的真实场景。本书从日常工作、生活中各种有趣的现象出发,不仅囊括了机器学习的基本知识 ,而且还包含了成为出众算法工程师的相关技能,更重要的是凝聚了笔者对人工智能领域的一颗热忱之心,旨在培养读者发现问题、解决问......一起来看看 《百面机器学习》 这本书的介绍吧!

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具