const data = {
thead: [
[
{ rowspan: 5, text: 'No.' },
{ colspan: 3, rowspan: 3, text: '固定价格' },
{ rowspan: 5, text: '防水' },
{ colspan: 6, text: 'MAIETYPE' },
{ colspan: 2, rowspan: 2, text: '板材' },
{ colspan: 2, rowspan: 3, text: '模块类型' }
],
[
{ colspan: 3, className: 'x28', text: 'TYPE1' },
{ colspan: 3, className: 'x28', text: 'TYPE2' }
],
[
{ colspan: 3, className: 'x28', text: 'TYPE1 报价' },
{ colspan: 3, className: 'x28', text: 'TYPE2 报价' },
{ rowspan: 3, text: 'FRONT' },
{ rowspan: 3, text: 'REAR' }
],
[
{ colspan: 3, text: 'Pin数' },
{ colspan: 3, text: 'PIN数' },
{ colspan: 3, text: 'Pin数' },
{ rowspan: 2, text: 'MODULE TYPE1' },
{ rowspan: 2, text: 'MODULE TYPE2' }
],
[
{ text: '1--4' },
{ text: '4--5' },
{ text: '6UP' },
{ text: '1--4' },
{ text: '4--5' },
{ text: '6UP' },
{ text: '1--4' },
{ text: '4--5' },
{ text: '6UP' }
]
],
tbody: [
[
{ text: 'tbody 1' },
{ text: 852 },
{ text: 357 },
{ text: 159 },
{ text: 789 },
{ text: 9123 },
{ text: 4456 },
{ text: 1452 },
{ text: 1342 },
{ text: 1578 },
{ text: 1320 },
{ text: 147 },
{ text: 258 },
{ text: 369 },
{ text: 963 }
]
]
};
function createTable(data) {
const table = document.createElement('table');
// Create table head
const thead = document.createElement('thead');
data.thead.forEach(rowData => {
const row = document.createElement('tr');
rowData.forEach(cellData => {
const th = document.createElement('th');
th.innerHTML = cellData.text;
if (cellData.rowspan) th.rowSpan = cellData.rowspan;
if (cellData.colspan) th.colSpan = cellData.colspan;
if (cellData.className) th.className = cellData.className;
row.appendChild(th);
});
thead.appendChild(row);
});
table.appendChild(thead);
// Create table body
const tbody = document.createElement('tbody');
data.tbody.forEach(rowData => {
const row = document.createElement('tr');
rowData.forEach(cellData => {
const td = document.createElement('td');
td.innerHTML = cellData.text;
if (cellData.rowspan) td.rowSpan = cellData.rowspan;
if (cellData.colspan) td.colSpan = cellData.colspan;
if (cellData.className) td.className = cellData.className;
row.appendChild(td);
});
tbody.appendChild(row);
});
table.appendChild(tbody);
return table;
}
const table = createTable(data);
document.getElementById('tableContainer').appendChild(table);
// ... Existing code ...
function handleInputChange(event) {
console.log('Input changed:', event.target.name, event.target.value);
}
function handleSelectChange(event) {
console.log('Select changed:', event.target.name, event.target.value);
}
function handleCheckboxChange(event) {
console.log('Checkbox changed:', event.target.name, event.target.checked);
}
button.onclick = () => {
const newRowData = [
{ text: 'tbody 2', tagName: 'th' },
{ tagName: 'td', children: [{ tagName: 'input', type: 'text', name: 'pin1', id: 'pin1', value: '', onchange: handleInputChange }] },
{ tagName: 'td', children: [{ tagName: 'select', name: 'test1', id: 'test1', onchange: handleSelectChange, options: [{ value: 'a', text: 'A Value' }, { value: 'b', text: 'B ue' }, { value: 'c', text: 'C alu' }] }] },
{ tagName: 'td', children: [{ tagName: 'input', type: 'checkbox', name: 'test3', id: 'test3', value: '1', checked: true, onchange: handleCheckboxChange }] },
{ tagName: 'td', children: [{ tagName: 'input', type: 'number', name: 'pin3', id: 'pin3', value: 0, onchange: handleInputChange }] },
{ text: 1452 },
{ tagName: 'td', children: [{ tagName: 'input', type: 'text', name: 'pin2', id: 'pin2', value: '一手', onchange: handleInputChange }] },
{ text: 1342 },
{ tagName: 'td', children: [{ tagName: 'select', name: 'test2', id: 'test2', onchange: handleSelectChange, options: [{ value: 'd', text: 'D Value'}, { value: 'e', text: 'E ue'}, { value: 'f', text: 'F alu'}] }] },
{ tagName: 'td', children: [{ tagName: 'input', type: 'checkbox', name: 'test4', id: 'test4', value: '0', onchange: handleCheckboxChange }] },
{ tagName: 'td', children: [{ tagName: 'input', type: 'number', name: 'pin4', id: 'pin4', value: 0, onchange: handleInputChange }] },
{ text: 4456 },
{ text: 1578 },
{ text: 1320 },
{ text: 147 },
];
addRowAboveButtonRow(table, newRowData);
};
// ... Existing code ...
// Replace addRow FUNCTION with the following
function addRowAboveButtonRow(table, rowData) {
const tbody = table.querySelector('tbody');
const lastRow = tbody.querySelector('tr:last-child');
const newRow = document.createElement('tr');
rowData.forEach(cellData => {
const cell = document.createElement(cellData.tagName || 'td');
if (cellData.text) {
cell.innerText = cellData.text;
}
if (cellData.children) {
cellData.children.forEach(childData => {
const child = document.createElement(childData.tagName);
if (childData.tagName === 'input' || childData.tagName === 'select') {
child.type = childData.type;
child.name = childData.name;
child.id = childData.id;
child.value = childData.value;
if (childData.tagName === 'input' && childData.type === 'checkbox') {
child.checked = childData.checked;
}
if (childData.tagName === 'select' && childData.options) {
childData.options.forEach(optionData => {
const option = document.createElement('option');
option.value = optionData.value;
option.text = optionData.text;
child.add(option);
});
}
if (childData.onchange) {
child.addEventListener('change', childData.onchange);
}
}
cell.appendChild(child);
});
}
newRow.appendChild(cell);
});
tbody.insertBefore(newRow, lastRow);
}