原生table实现表格合并(简单的第一列合并)

html部分用原生table渲染,其中allFields表示除第一列外的所有字段,editFields表示哪几列(哪几个字段)需要编辑

<table border="1" class="table">
  <thead>
    <tr>
      <th>类别</th>
      <th>内容</th>
      <th>综合得分</th>
      <th>原因</th>
    </tr>
  </thead>
  <tbody>
    <template v-for="item in tableData">
      <template v-for="(_item, _index) in item.children">
        <tr :key="_index">
          <td v-if="_index === 0" :rowspan="item.children.length">
            {{ item.category }}
          </td>
          <template v-for="field in allFields">
            <td :key="field + _index">
              <template v-if="editFields.includes(field)">
                <Input v-model="_item[field]" style="width: 100px"></Input>
              </template>
              <template v-else>
                {{ _item[field] }}
              </template>
            </td>
          </template>
        </tr>
      </template>
    </template>
  </tbody>
</table>

其中tableData为后端返回并经过处理的数据,格式如下:

tableData: [
  {
    category: "书籍",
    children: [
      {
        name: "古代书籍",
        category: "书籍",
        score: 10,
        reason: "",
      },
      {
        name: "近代书籍",
        category: "书籍",
        score: 20,
        reason: "",
      },
    ]
  },
  {
    category: "音乐",
    children: [
      {
        name: "古典音乐",
        category: "音乐",
        score: 30,
        reason: "",
      },
      {
        name: "轻音乐",
        category: "音乐",
        score: 40,
        reason: "",
      },
    ]
  },
],

allFields值为:[‘name’, ‘score’, ‘reason’]
editFields值为:[‘score’]

当需要动态改变哪几列可编辑时,只需要改变editFields即可

实现效果如下:
在这里插入图片描述

  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
实现 HTML 表格宽度拖拽,可以使用原生 JavaScript 来实现。以下是一个简单实现方式: HTML 代码: ``` <table id="myTable"> <tr> <th>1</th> <th>2</th> <th>3</th> </tr> <tr> <td>内容1</td> <td>内容2</td> <td>内容3</td> </tr> <tr> <td>内容4</td> <td>内容5</td> <td>内容6</td> </tr> </table> ``` JavaScript 代码: ``` var table = document.getElementById("myTable"); var isResizing = false; var lastDownX = 0; // 设置每的初始宽度 var columnWidths = [100, 100, 100]; for (var i = 0; i < table.rows[0].cells.length; i++) { table.rows[0].cells[i].style.width = columnWidths[i] + "px"; } // 鼠标按下时记录位置 table.addEventListener("mousedown", function (e) { isResizing = true; lastDownX = e.clientX; }); // 鼠标移动时改变宽度 table.addEventListener("mousemove", function (e) { if (!isResizing) { return; } var cell = e.target.parentElement; var cellIndex = Array.from(cell.parentElement.children).indexOf(cell); var widthDiff = e.clientX - lastDownX; // 改变当前的宽度 var newWidth = columnWidths[cellIndex] + widthDiff; if (newWidth > 0) { cell.style.width = newWidth + "px"; columnWidths[cellIndex] = newWidth; lastDownX = e.clientX; } }); // 鼠标抬起时停止改变宽度 table.addEventListener("mouseup", function (e) { isResizing = false; }); ``` 以上代码会在表格中添加一个鼠标事件监听器,当用户按下鼠标并移动时,会根据鼠标的位置改变被选中的宽度。需要注意的是,当用户拖动宽度时,应该记录每的初始宽度(在这里使用了 `columnWidths` 数组来记录),并在移动过程中修改当前的宽度,而不是仅仅修改当前的宽度。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值