Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/pxweb2 319 add metainfo to cells #337

Merged
merged 3 commits into from
Nov 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 35 additions & 6 deletions libs/pxweb2-ui/src/lib/components/Table/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type DataCellMeta = {
varId: string; // id of variable
valCode: string; // value code
varPos: number; // variable position in stored data
htmlId: string; // id used in th. Will build up the headers attribute for datacells. For accesability
};

/**
Expand All @@ -44,7 +45,12 @@ export function Table({ pxtable, className = '' }: TableProps) {

// Loop through all header variables. j is the header variable index
for (let j = 0; j < pxtable.heading.length; j++) {
const dataCellMeta: DataCellMeta = { varId: '', valCode: '', varPos: 0 };
const dataCellMeta: DataCellMeta = {
varId: '',
valCode: '',
varPos: 0,
htmlId: '',
};
dataCellCodes[j] = dataCellMeta; // add empty object
}
headingDataCellCodes[i] = dataCellCodes;
Expand All @@ -53,6 +59,7 @@ export function Table({ pxtable, className = '' }: TableProps) {
return (
<table
className={cl(classes.table, classes[`bodyshort-medium`]) + cssClasses}
aria-label={pxtable.metadata.label}
>
<thead>{createHeading(pxtable, tableMeta, headingDataCellCodes)}</thead>
<tbody>{createRows(pxtable, tableMeta, headingDataCellCodes)}</tbody>
Expand Down Expand Up @@ -89,7 +96,6 @@ export function createHeading(
</th>
);
}

// Otherwise calculate columnspan start value
columnSpan = tableMeta.columns - tableMeta.columnOffset;

Expand All @@ -101,7 +107,6 @@ export function createHeading(
) {
// Set the column span for the header cells for the current row
columnSpan = columnSpan / table.heading[idxHeadingLevel].values.length;

const variable = table.heading[idxHeadingLevel];
let columnIndex = 0;
// Repeat for number of times in repetion, first time only once. idxRepetitionCurrentHeadingLevel is the repetition counter
Expand All @@ -112,8 +117,17 @@ export function createHeading(
) {
// loop trough all the values for the header variable
for (let i = 0; i < variable.values.length; i++) {
const htmlId: string =
'H' +
idxHeadingLevel +
'.' +
variable.values[i].code +
'.I' +
idxRepetitionCurrentHeadingLevel;
headerRow.push(
<th
id={htmlId}
scope="col"
colSpan={columnSpan}
key={getNewKey()}
className={cl({
Expand All @@ -132,6 +146,7 @@ export function createHeading(
variable.values[i].code;
headingDataCellCodes[columnIndex][idxHeadingLevel].varPos =
table.data.variableOrder.indexOf(variable.id);
headingDataCellCodes[columnIndex][idxHeadingLevel].htmlId = htmlId;
columnIndex++;
}
}
Expand Down Expand Up @@ -167,6 +182,7 @@ export function createRows(
createRow(
0,
tableMeta.rows - tableMeta.rowOffset,
0,
table,
tableMeta,
datacellCodes,
Expand Down Expand Up @@ -201,6 +217,7 @@ export function createRows(
function createRow(
stubIndex: number,
rowSpan: number,
stubIteration: number,
table: PxTable,
tableMeta: columnRowMeta,
stubDataCellCodes: DataCellCodes,
Expand All @@ -214,11 +231,16 @@ function createRow(

// Loop through all the values in the stub variable
for (let i = 0; i < table.stub[stubIndex].values.length; i++) {
if (stubIndex === 0) {
stubIteration++;
}

const val = table.stub[stubIndex].values[i];
const cellMeta: DataCellMeta = {
varId: table.stub[stubIndex].id,
valCode: val.code,
varPos: table.data.variableOrder.indexOf(table.stub[stubIndex].id),
htmlId: 'R.' + stubIndex + val.code + '.I' + stubIteration,
};
stubDataCellCodes.push(cellMeta);
// Fix the rowspan
Expand All @@ -228,6 +250,9 @@ function createRow(

tableRow.push(
<th
id={cellMeta.htmlId}
scope="row"
role="rowheader"
className={cl(classes.stub, classes[`stub-${stubIndex}`])}
key={getNewKey()}
>
Expand All @@ -253,6 +278,7 @@ function createRow(
createRow(
stubIndex + 1,
rowSpan,
stubIteration,
table,
tableMeta,
stubDataCellCodes,
Expand Down Expand Up @@ -320,11 +346,14 @@ function fillData(
const maxCols = tableMeta.columns - tableMeta.columnOffset;

// Loop through all data columns in the table

for (let i = 0; i < maxCols; i++) {
// Merge the metadata structure for the dimensions of the stub and header cells
const dataCellCodes = stubDataCellCodes.concat(headingDataCellCodes[i]);
//console.log(dataCellCodes);

const datacellIds: string[] = dataCellCodes.map((obj) => obj.htmlId);
const headers: string = datacellIds
.toString()
.replace(new RegExp(',', 'g'), ' ');
const dimensions: string[] = [];
// Arrange the dimensons in the right order according to how data is stored is the cube
for (let j = 0; j < dataCellCodes.length; j++) {
Expand All @@ -341,7 +370,7 @@ function fillData(
const dataValue = getPxTableData(table.data.cube, dimensions);

tableRow.push(
<td key={getNewKey()}>
<td key={getNewKey()} headers={headers}>
{t('number.simple_number', { value: dataValue ?? '' })}
</td>
); // TODO: Handle null values
Expand Down