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

Any*_*are 5 javascript c# jquery loops datatables

如果我有两种类型的行:

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
Run Code Online (Sandbox Code Playgroud)

等等

NOTE: ALWAYS I SHOULD HAVE ONE DATA ROW FOR THE TOTAL DATA ROWS.
Run Code Online (Sandbox Code Playgroud)

我想遍历数据行的列并映射到相关的标题列数据:

"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;
    }
Run Code Online (Sandbox Code Playgroud)

让我们看第二个例子:我有两个标题行:

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
Run Code Online (Sandbox Code Playgroud)

我的代码中的问题是无论标题行的数量如何,它总是映射到第一个标题行。

为了解决这个问题,我想做一个条件循环。

所以如果the number of header rows = array[36] /18 = 2标题行:

  • 第一个数据行迭代: parsedData.columns[meta.col].toString().split("-")[0]
  • 第二个数据行迭代: parsedData.columns[meta.col-18].toString().split("-")[0]
  • 第三行数据迭代: parsedData.columns[meta.col].toString().split("-")[0]
  • 第四次数据行迭代: parsedData.columns[meta.col-18].toString().split("-")[0]
  • 第五个数据行迭代:(总计)所以第一个 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
Run Code Online (Sandbox Code Playgroud)

根据评论,这里有一个例子:

在此处输入图片说明

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
Run Code Online (Sandbox Code Playgroud)
  • 本例中的标题行 = 一个数组 [36]
  • 数据行是多个数组(5) each array = array[18]

因为我有 2 个标题行 ---> 我有 5 个数据行

2 + 2 + 1(total).
Run Code Online (Sandbox Code Playgroud)

现在我想将数据行中的每个工具提示单元格映射到其对应的标题行,如上所示。但它总是映射到第一个标题行。

Mak*_*kan 1

为了找到对应的标题,我们需要在计算中考虑总标题行。

然后下面的 javascript 函数将返回标头位置 (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
    
    
Run Code Online (Sandbox Code Playgroud)