I am in search of a dynamic method to generate Quasar Table column definitions. While the existing method shown below does work, I believe that replacing the lengthy switch statement with a for loop would be a more efficient solution.
How can I implement a for loop to achieve the same outcome as the code provided:
const createTableColumns = (numberOfColumns: number) => {
const tableCols: QTableProps["columns"] = [];
const numberOfColumnsStr = numberOfColumns.toString();
switch (numberOfColumnsStr) {
// Cases for each specific column
}
tableCols.sort((a, b) => parseInt(a.name) - parseInt(b.name));
return tableCols;
};
I have attempted the following alternative approach using a for loop:
const tableCols2: QTableProps["columns"] = [];
for (let colIndex = 0; colIndex < numberOfColumns; colIndex++) {
const rowKeyArray = [...]; // Array containing keys for each column
const colNumber: number = colIndex + 1;
const rowKey: string = rowKeyArray[colIndex];
const myObj = {
name: colNumber.toString(),
align: "center",
label: colNumber.toString(),
field: (row) => row[rowKey].value,
format: (val, row) => (row[rowKey].isSlected ? `** ${val} **` : `${val}`),
sortable: false,
};
tableCols2.push(myObj);
}