【发布时间】:2021-11-10 22:39:31
【问题描述】:
我不确定如何根据每个分组表创建自定义标题。
目前表格组合在一起,但它只显示 1 行带有一个加号,您几乎看不到。
我想要尝试做的是用第一个 td 项目的标题名称替换行信息以显示行中有多少项目。
我想要做什么的例子
//Making timesheet <td>
fetch(timehsheetUrl + date)
.then(response => response.json())
.then(data => makeTable(data.contracts))
.catch(error => console.log(`Oh no! ${error}`))
const makeTable = (contracts) => {
console.log(contracts);
const body = document.getElementById('tsdata');
contracts.forEach((person, index) => {
person.details.forEach((entry) => {
const dateString = entry.timesheetDate;
const dateObject = new Date(dateString);
const dd = dateObject.getDate();
const mm = dateObject.toLocaleString('en-us', {
month: 'short'
});
const yyyy = dateObject.getFullYear();
const chartDate = `${dd}-${mm}-${yyyy}`
console.log(chartDate);
const htmlTemplate = `
<tr class = "testDeleteTimesheet">
<td>${entry.contractCode}</td>
<td class="timesheetDetailsID" style="display:none;">${entry.timesheetDetailsId}</td>
<td>${entry.activityCode}</td>
<td>${entry.otFlag}</td>
<td>${entry.notes}</td>
<td>${chartDate}</td>
<td class = "totalHours">${entry.hours}</td>
<td><button id=\"edit-" + counter + "\" class=\"btn editRow btnStyle btn-primary btn-sm\"><span class=\ "bi bi-pencil\"></span></button>
<button id=\"delete-" + counter + "\" class=\"btn delRow btnStyle btn-danger btn-sm\"><span class=\"bi bi-eraser\"></span></button></td>
</tr>
`;
body.innerHTML += htmlTemplate;
});
//Making timesheet easier to read
const tables = $('table')[0];
const rowGroups = {};
//loop through the rows excluding the first row (the header row)
while(tables.rows.length > 1){
const row = tables.rows[1];
const id = $(row.cells[0]).text();
if(!rowGroups[id]) rowGroups[id] = [];
if(rowGroups[id].length > 0){
row.className = 'subrow';
$(row).slideUp();
}
rowGroups[id].push(row);
tables.deleteRow(1);
}
//loop through the row groups to build the new table content
for(let id in rowGroups){
const group = rowGroups[id];
for(let j = 0; j < group.length; j++){
const row = group[j];
if(group.length > 1 && j == 0) {
//add + button
const lastCell = row.cells[row.cells.length - 1];
$("<span class='collapsed'>").appendTo(lastCell).click(plusClick);
}
tables.tBodies[0].appendChild(row);
}
}
//function handling button click
function plusClick(e){
const collapsed = $(this).hasClass('collapsed');
const fontSize = collapsed ? 14 : 0;
$(this).closest('tr').nextUntil(':not(.subrow)').slideToggle(400)
.css('font-size', fontSize);
$(this).toggleClass('collapsed');
}
});
};
我的 api 样例:
"contracts":
[
{
"contractID": "Test1",
"details": [
{
"timesheetDetailsId": 111111,
"timesheetId": 0,
"timesheetDate": "2021-11-01T00:00:00",
"timesheetDayNumber": 1,
"contractCode": "Test1",
"activityCode": "GRA",
"hours": 7.5,
"otFlag": false,
"notes": "Testing",
"approved": false,
"overUnder": 0.0,
"employeeCode": "N-0510"
},
{
"timesheetDetailsId": 111113,
"timesheetId": 0,
"timesheetDate": "2021-11-03T00:00:00",
"timesheetDayNumber": 3,
"contractCode": "Test1",
"activityCode": "GRA",
"hours": 7.5,
"otFlag": false,
"notes": "Testing",
"approved": false,
"overUnder": 0.0,
"employeeCode": "N-0510"
}
]
"contractID": "Test2",
"details": [
{
"timesheetDetailsId": 111112,
"timesheetId": 0,
"timesheetDate": "2021-11-02T00:00:00",
"timesheetDayNumber": 2,
"contractCode": "Test2",
"activityCode": "GRA",
"hours": 7.5,
"otFlag": false,
"notes": "Testing",
"approved": false,
"overUnder": 0.0,
"employeeCode": "N-0510"
}
]
]
【问题讨论】:
-
请提供一个来自您的 API 的代表性数据的小示例。我会构建一次表格,一次构建您的标题和组。
-
@JonP 我已经更新了我的问题,提供了我的 api 的样子
-
在
details对象中contractID是否总是等于contractCode? -
是的,Api 将始终遵循合同 ID 的contactCode
标签: javascript jquery ajax fetch