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
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
vxe-table是一个基于vue表格组件,支持增删改查、虚拟滚动、懒加载、快捷菜单、数据校验、树形结构、打印导出、表单渲染、数据分页、模态窗口、自定义模板、灵活的配置项、丰富的扩展插件等... 设计理念: 面向现代浏览器,高效的简洁 API 设计 模块化表格、按需加载、插件化扩展 为单行编辑表格而设计,支持增删改查及更多扩展,强大的功能的同时兼具性能 功能: Basic table (基础表格) Grid (高级表格) Size (尺寸) Striped (斑马线条纹) Table with border (带边框) Cell style (单元格样式) Column resizable (列宽拖动) Maximum table height (最大高度) Resize height and width (响应式宽高) Fixed column (固定列) Grouping table head (表头分组) Highlight row and column (高亮行、列) Table sequence (序号) Radio (单选) Checkbox (多选) Sorting (排序) Filter (筛选) Rowspan and colspan (合并行或列) Footer summary (表尾合计) Import (导入) Export (导出) Print (打印) Show/Hide column (显示/隐藏列) Loading (加载中) Formatted content (格式化内容) Custom template (自定义模板) Context menu(快捷菜单) Virtual Scroller(虚拟滚动) Expandable row (展开行) Pager(分页) Form(表单) Toolbar(工具栏) Tree table (树形表格) Editable CRUD(增删改查) Validate(数据校验) Data Proxy(数据代理) Keyboard navigation(键盘导航) Modal window(模态窗口) Charts(图表工具) 更日志: v3.3.6 table 修复 getRadioReserveRecord、getCheckboxReserveRecords 方法错误获取数据问题 grid 修复数据代理删除行为存在新增数据的问题
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、付费专栏及课程。

余额充值