jsOMS/Model/Action/Dom/Table/Append.js
Dennis Eichhorn 7b75ec58f7
Some checks failed
CodeQL / Analyze (javascript) (push) Has been cancelled
CI / general_module_workflow_js (push) Has been cancelled
fix permissions
2025-04-02 14:15:07 +00:00

50 lines
1.3 KiB
JavaScript

/**
* Apped element to a table.
*
* @param {Object} action Action data
* @param {function} callback Callback
*
* @since 1.0.0
*/
export function tableAppend (action, callback)
{
'use strict';
const table = document.getElementById(action.id);
const tbody = table !== null && typeof table !== 'undefined' ? table.getElementsByTagName('tbody')[0] : null;
const headers = table !== null && typeof table !== 'undefined' ? table.getElementsByTagName('thead')[0].getElementsByTagName('th') : null;
const dataLength = action.data.length;
const headerLength = headers !== null && typeof headers !== 'undefined' ? headers.length : 0;
let row;
let cell;
let rawText;
for (let i = 0; i < dataLength; ++i) {
if (tbody === null) {
break;
}
row = tbody.insertRow(tbody.rows.length);
for (let j = 0; j < headerLength; ++j) {
if (row === null) {
break;
}
cell = row.insertCell(j);
rawText = action.data[i][headers[j].getAttribute('data-name')];
if (typeof rawText === 'undefined') {
rawText = '';
}
cell.appendChild(document.createTextNode(rawText));
}
}
if (typeof callback === 'function') {
callback();
}
};