创建表格渲染

export function createTable(list: any[], column: { prop: string; label: string}[]) {
  // 创建一个表格
  const table = document.createElement('table')
  table.setAttribute('id', 'table_box')
  // 创建表头
  const thead = document.createElement('thead')
  const thead_tr = document.createElement('tr')
  for (let i = 0; i < column.length; i++) {
    const item = column[i]
    const th = document.createElement('th')
    th.innerHTML = item.label
    thead_tr.appendChild(th)
  }
  thead.appendChild(thead_tr)
  table.appendChild(thead)
  // 创建表格主体
  const tbody = document.createElement('tbody')
  for (let i = 0; i < list.length; i++) { // 外面的for循环 是 行tr
    const item = list[i]
    const tr = document.createElement('tr')
    tbody.appendChild(tr)
    // 往tr每一行里面创建单元格
    // td单元格的数量取决 column
    // 面的for循环是 列
    for (let i = 0; i < column.length; i++) {
      const columnItem = column[i]
      if (item[columnItem.prop + '-span'] && item[columnItem.prop + '-span'].rowspan === 0) {
        continue
      }
      const td = document.createElement('td')
      if (item[columnItem.prop + '-span'] && item[columnItem.prop + '-span'].rowspan > 1) {
        td.setAttribute('rowspan', item[columnItem.prop + '-span'].rowspan)
      }
      if (columnItem.prop === 'user_name' || columnItem.prop === 'workTaskName') {
        td.setAttribute('class', 'table_props_center')
      }
      td.innerHTML = item[columnItem.prop]
      tr.appendChild(td)
    }
  }
  table.appendChild(tbody)
  return table
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值