vue 新增table表格、新增加一行数据

需求背景

基于arcgis api为实现多个图层服务能够同时根据不同的key值进行过滤,并将结果高亮展示而封装的组件 过程中的table表格部分。table表格要求可以新增相同表头的table表格 ,行数据可以新增且每个表格中的数据互不影响。
在这里插入图片描述

<template>
  <div id="TableHome">
    <div class="TableTitle" v-for="(table, tableIndex) in tables" :key="tableIndex">
      <div style="border-bottom: 2px solid #797676;margin: 5px;display: flex;justify-content: space-around;">
        <a-space class="verticalInput" direction="vertical">
          <a-input v-model:value="tableTitleInput[tableIndex]" type="text" placeholder="请输入过滤器名称" @change="InputSearch" />
        </a-space>
        <div style="width: 70%"></div>
        <a-button class="CloseDiv" type="primary" @click="DeleteArr(tableIndex)">删除</a-button>

      </div>
      <a-button class="ButStyle" type="primary" @click="addRow(tableIndex)">新增</a-button>
      <table class="TableHead">
        <thead class="TableHeadTitle" style="background-color: #fafafa;">
        <tr>
          <th>图层</th>
          <th>字段</th>
          <th>操作</th>
        </tr>
        </thead>
        <tbody>
        <tr v-for="(row, rowIndex) in table" :key="rowIndex">
          <td>
            <select v-model="row.option1" @change="updateOptions(rowIndex,row,tableIndex)">
              <option v-for="option in options1" :key="option" :value="option">{{ option }}</option>
            </select>
          </td>
          <td>
            <select v-model="row.option2" @change="updateOptions(rowIndex,row,tableIndex)">
              <option v-for="option in options2" :key="option" :value="option">{{ option }}</option>
            </select>
          </td>
          <td><a-button  @click="deleteRow(tableIndex, rowIndex)" type="primary" danger>删除</a-button></td>
        </tr>
        </tbody>
      </table>
    </div>
    <div class="tableBottom">
      <a-button type="primary" @click="addTable">添加新群组</a-button>
      <div style="width: 85%"></div>
      <a-button type="primary" @click="DataSure">确认</a-button>
    </div>


  </div>
</template>

<script>
import {ref, toRaw, watch} from 'vue';

export default {
  setup() {

    const tableTitleInput = ref([''])
    //最终所有数据
    const AllLayerTestData = ref([])
    const AllLayerData = ref([])

    const tables = ref([[]]);
    const options1 = ref(['A', 'B', 'C']);
    const options2 = ref(['X', 'Y', 'Z']);

    //表格内添加行
    const addRow = (tableIndex) => {
      if (tables.value[tableIndex] === undefined) {
        tables.value[tableIndex] = [];
      }
      tables.value[tableIndex].push({ option1: '', option2: '' });
    };
    //添加添加新群组
    const addTable = () => {
      tables.value.push([]);
      tableTitleInput.value.push('');
      console.log(toRaw(tables.value),"tables.value")
    };

    //表格中的行内容删除
    const deleteRow = (tableIndex, rowIndex,) => {
      tables.value[tableIndex].splice(rowIndex, 1);
      let DeleteData = toRaw(AllLayerTestData.value)
      DeleteData = DeleteData.map(item=>{
        if(item.key === tableIndex && item.urlList[0].key === rowIndex){
          return undefined
        }
        return item
      })
      AllLayerTestData.value = DeleteData.filter(element =>element!== undefined)
    };


    //table表格的删除
    const DeleteArr = (tableKey) => {
      let inputValue = toRaw(toRaw(tableTitleInput).value)
      if(tableKey!== -1){
        tables.value.splice(tableKey,1)
        inputValue.splice(tableKey,1)
      }
      let DeleteTableData = toRaw(AllLayerTestData.value)
      DeleteTableData = DeleteTableData.map(item=>{
        if(item.key === tableKey){
          return undefined
        }
        return item
      })
      AllLayerTestData.value = DeleteTableData.filter(element =>element!== undefined)


      console.log(toRaw(AllLayerTestData.value),"itremitemitem")



    }


    //input 输入框
    const search = () => {
      let inputValue = toRaw(toRaw(tableTitleInput).value)

    };
    const InputSearch = debounce(search,1000);
    function debounce(fn, duration = 1000) {
      let timerId = null;
      return function (...args) {
        if (timerId) {
          clearTimeout(timerId);
        }
        let context = this;
        timerId = setTimeout(() => {
          fn.apply(context, args);
        }, duration);
      };
    }

    const updateOptions = (index,row,tableIndex) => {
      let inputValue = toRaw(toRaw(tableTitleInput).value)
      let selectValue = toRaw(row)
      let keyTable = tableIndex
      let ArrData = []

      let obj = {
            key:"",
            value: "",
            urlList: []
          }
      let ObjUrlList ={
        url: "",
        value: "",
        key:""
      }
      if(inputValue[keyTable]){
        obj.value = inputValue[keyTable]
        obj.key = keyTable
        if(selectValue.option1 && selectValue.option2){
          ObjUrlList.key = index
          ObjUrlList.url = selectValue.option1
          ObjUrlList.value = selectValue.option2
          obj.urlList.push(ObjUrlList)
          toRaw(AllLayerTestData.value).push(obj)
        }
      }else{
        alert("请输入")
      }


    }

    const DataSure = () => {
      let localData = (toRaw(AllLayerTestData.value))

      const mergedData = localData.reduce((acc,obj)=>{
        if(!acc[obj.key]){
          acc[obj.key] = {key:obj.key,value:obj.value,urlList:[],}
        }
        acc[obj.key].urlList = acc[obj.key].urlList.concat(obj.urlList)
        return acc
      },{})
      console.log(mergedData)
    }

    return {
      tables,
      options1,
      options2,
      addRow,
      addTable,
      deleteRow,
      DeleteArr,
      tableTitleInput,
      InputSearch,
      updateOptions,
      AllLayerData,
      AllLayerTestData,
      DataSure
    };
  }
};
</script>

<style lang="less" scoped>
#TableHome{
  height: 100vh;
  width: 100%;
  position: relative;
}
.tableBottom{
  margin: 2%;
  display: flex;
  justify-content: space-around;
}
erticalInput{
  margin:1%;
}
.CloseDiv{
  margin:1%;
}
td,th{
  border: 1px solid black;
  padding: 5px;
}
.TableTitle{
  margin: 2%;
  border: 1px solid #151514;
}
.ButStyle{
  margin:1%;
}
select{
  width: 80%;
  height: 80%;
  border: 1px solid #868484;
  border-radius: 5px;
  outline:none
}
.TableHead{
  margin: 1%;
  width: 98%;
  height: 50px;
  text-align: center;
  line-height: 50px;
  font-weight: 100;
  font-family: 宋体, SinSun,seif;
}
.TableHeadTitle{
  font-size: 15px;
}
</style>
  • 25
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Vue中,可以通过v-for指令和响应式数据来实现动态表格行的新增。以下是一种实现方式: 首先,在Vue组件的data选项中定义一个数组(比如叫做tableData),用于存储表格数据。这个数组的初始值可以为空,也可以包含一些默认的表格数据。 然后,在template中,使用v-for指令遍历tableData数组,将每个元素渲染成表格一行。例如: ```html <table> <tr v-for="item in tableData" :key="item.id"> <td>{{ item.name }}</td> <td>{{ item.age }}</td> <!-- 其他列的数据 --> </tr> </table> ``` 接下来,当需要新增表格行时,可以通过点击按钮或其他交互方式来触发一个方法(比如叫做addTableRow),在这个方法中向tableData数组中添加一个的行数据。例如: ```html <button @click="addTableRow">新增表格行</button> ... methods: { addTableRow() { // 创建一个的行数据对象 const newTableRow = { id: this.tableData.length + 1, name: '', age: 0, // 其他列的数据 }; // 向tableData数组添加的行数据 this.tableData.push(newTableRow); } } ``` 在上述代码中,addTableRow方法会创建一个的行数据对象,并将其添加tableData数组中。由于tableData是响应式的,所以当数组发生变化时,表格的视图会自动更新增的行会被渲染出来。 以上就是使用Vue实现动态表格新增的一个简单示例。当点击新增按钮时,会动态地添加一行空白的表格行。你可以根据实际需求来修改和扩展这个示例。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值