Skip to content

Commit

Permalink
Implemented #483 - Multiple Textboxes - A Text in Tables visualizer d…
Browse files Browse the repository at this point in the history
…oesn't display field titles/column headers
  • Loading branch information
tsv2013 committed Oct 21, 2024
1 parent ab0a4bf commit 4c2b8d1
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 2 deletions.
8 changes: 8 additions & 0 deletions src/text.scss
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,11 @@
background-color: $background-color;
min-height: $form-element-height;
}

.sa-text-table__cell--number {
text-align: end;
}

th.sa-text-table__cell {
font-weight: 600;
}
26 changes: 24 additions & 2 deletions src/text.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Question } from "survey-core";
import { Question, QuestionMultipleTextModel } from "survey-core";
import { VisualizerBase } from "./visualizerBase";
import { VisualizationManager } from "./visualizationManager";
import { localization } from "./localizationManager";
Expand Down Expand Up @@ -27,10 +27,22 @@ export class TextTableAdapter {
tableNode.style.backgroundColor = this.model.backgroundColor;
container.appendChild(tableNode);

if(this.model.columns) {
var row = DocumentHelper.createElement("tr");
this.model.columns.forEach(column => {
var td = DocumentHelper.createElement("th", "sa-text-table__cell", {
textContent: column.title,
});
row.appendChild(td);
});
tableNode.appendChild(row);
}

data.forEach((rowData) => {
var row = DocumentHelper.createElement("tr");
for (var i = 0; i < columnCount; i++) {
var td = DocumentHelper.createElement("td", "sa-text-table__cell", {
const column = this.model.columns[i];
var td = DocumentHelper.createElement("td", "sa-text-table__cell" + (column?.type == "number" ? " sa-text-table__cell--number" : ""), {
textContent: rowData[i],
});
row.appendChild(td);
Expand Down Expand Up @@ -60,6 +72,16 @@ export class Text extends VisualizerBase {
this._textTableAdapter = new TextTableAdapter(this);
}

public get columns(): Array<{ name: string, title: string, type: string }> {
const columns = [];
if(this.question.getType() == "multipletext") {
(this.question as QuestionMultipleTextModel).items.forEach(item => {
columns.push({ name: item.name, title: item.title, type: item.inputType });
});
}
return columns;
}

protected getCalculatedValuesCore(): any {
let result: Array<Array<string>> = [];
let columnCount = 0;
Expand Down
79 changes: 79 additions & 0 deletions tests/text.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { QuestionMultipleTextModel, QuestionTextModel } from "survey-core";
import { Text, TextTableAdapter } from "../src/text";

const multipleTextQuestionData = [{
"bloodPressure": {
"systolic": 90,
"diastolic": "100",
"pulse": "80"
}
},
{
"bloodPressure": {
"systolic": 80,
"diastolic": "110",
"pulse": "70"
}
},
{
"bloodPressure": {
"systolic": 85,
"diastolic": "120",
"pulse": "77"
}
}];

test("multiple text visualizer", async () => {
const question = new QuestionMultipleTextModel("bloodPressure");
question.fromJSON({
"type": "multipletext",
"name": "bloodPressure",
"title": "Blood Pressure",
"items": [
{
"name": "systolic",
"inputType": "number",
"title": "Systolic",
},
{
"name": "diastolic",
"title": "Diastolic",
},
{
"name": "pulse",
"title": "Pulse:"
}
]
});
const text = new Text(question, multipleTextQuestionData);

const columns = text.columns;
expect(columns.length).toBe(3);
expect(columns[0].name).toBe("systolic");
expect(columns[0].title).toBe("Systolic");
expect(columns[0].type).toBe("number");
expect(columns[1].name).toBe("diastolic");
expect(columns[1].title).toBe("Diastolic");
expect(columns[1].type).toBe("text");

const { columnCount, data }: any = await text.getCalculatedValues();
expect(columnCount).toBe(3);
expect(data.length).toBe(3);
});

test("text visualizer - columns", async () => {
const question = new QuestionTextModel("systolic");
question.fromJSON({
"name": "systolic",
"type": "text",
"title": "Systolic",
});
const text = new Text(question, [{ "systolic": "83" }]);

const columns = text.columns;
expect(columns.length).toBe(0);

const { columnCount, data }: any = await text.getCalculatedValues();
expect(columnCount).toBe(1);
expect(data.length).toBe(1);
});

0 comments on commit 4c2b8d1

Please sign in to comment.