gpt4 book ai didi

javascript - 如何有条件地循环以将标题行映射到数据行

转载 作者:行者123 更新时间:2023-12-01 15:25:33 27 4
gpt4 key购买 nike

如果我有两种类型的行:header rowsdata rows .并且任何行都应由 18 columns 组成.
数据行数基于标题行数,例如:

If I have 2 header rows, I should have (2 datarows: the first one map to the first header row, the second one map to the second header row + 2 datarows: the first one map to the first header row, the second one map to the second header row + 1 data rows for total: maps to the first header row ) = 5 datarows per group

If I have 3 header rows, I should have (3 datarows: the first one map to the first header row, the second one map to the second header row, the third one maps to the third header row + 3 datarows: the first one maps to the first header row, the second one maps to the second header row, the third one mapss to the third header row + 1 for total: maps to the first header row) = 7 datarowsper group
等等
NOTE: ALWAYS I SHOULD HAVE ONE DATA ROW FOR THE TOTAL DATA ROWS.
我想遍历数据行的列并映射到相关的标题 col 数据:
"render": function (data, type, full, meta) {

let tooltip = isNaN(data) ? data : Number(data);

return type === 'display' ? '<div id="tooltip" data-tooltip="' +parsedData.columns[meta.col].toString().split("-")[0] + " = " + tooltip + "|" + meta.row + '">' + data : data;
}

让我们看第二个例子:我有两个标题行:
parsedData.columns = array[36] where 36 = 2 * 18 = number of header rows * number of columns per row.

There are multiple data rows one array for each data row = one row array[18]

meta.row = row index
我的代码中的问题是,无论标题行的数量是多少,它总是映射到第一个标题行。
为了解决这个问题,我想做一个条件循环。
所以如果 the number of header rows = array[36] /18 = 2标题行:
  • The first data row Iteration : parsedData.columns[meta.col].toString().split("-")[0]
  • the second data row iteration : parsedData.columns[meta.col-18].toString().split("-")[0]
  • The third data row Iteration : parsedData.columns[meta.col].toString().split("-")[0]
  • The fourth data row iteration : parsedData.columns[meta.col-18].toString().split("-")[0]
  • The fifth data row iteration:(for total) so the first parsedData.columns[meta.col].toString().split("-")[0]
and repeat this pattern for each group of row data(5 data rows per group), may I have 3 groups so I have 15 data rows

根据评论,这里有一个例子:
enter image description here
528.00  -->Tooltip should = A1:528.00
52.80 -->Tooltip should = B9:52.80
240.00- -->Tooltip should = B10:240.00-
91.52 -->Tooltip should = T5:91.52
  • 此示例中的标题行 = 数组 [36]
  • 数据行是多个数组(5) 每个数组 = array[18]

  • 因为我有 2 个标题行 ---> 我有 5 个数据行
    2 + 2 + 1(total).
    现在我想将数据行中的每个工具提示单元格映射到其等效的标题行,如上图所示。但它总是映射到第一个标题行。

    最佳答案

    要找到对应标题,我们需要在计算中考虑总标题行。
    然后下面的 javascript 函数将返回 header 位置 (meta.col)

    function getHeaderCol(totalHeaderRows, dataRow, dataCol){
    const groupRows = (2 * totalHeaderRows) + 1
    const newDataRow = dataRow % groupRows
    return ((newDataRow % totalHeaderRows) * 18) + dataCol
    }

    console.log(getHeaderCol(2,4,10)) // result 10 , group 1
    console.log(getHeaderCol(2,5,10)) // result 10 , group 2

    关于javascript - 如何有条件地循环以将标题行映射到数据行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62400109/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com