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/issue 3089 on get expression display value #3090

Merged
merged 2 commits into from
Jul 14, 2021
Merged
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions src/base-interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ export interface ISurvey extends ITextProcessor, ISurveyErrorOwner {
processHtml(html: string): string;
getSurveyMarkdownHtml(element: Base, text: string, name: string): string;
getRendererForString(element: Base, name: string): string;
getExpressionDisplayValue(
question: IQuestion,
value: any,
displayValue: string
): string;
isDisplayMode: boolean;
isDesignMode: boolean;
areInvisibleElementsShowing: boolean;
Expand Down
13 changes: 9 additions & 4 deletions src/question_expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,15 @@ export class QuestionExpressionModel extends Question {
}
protected getDisplayValueCore(keysAsText: boolean, value: any): any {
var val = this.isValueEmpty(value) ? this.defaultValue : value;
if (this.isValueEmpty(val)) return "";
var str = this.getValueAsStr(val);
if (!this.format) return str;
return (<any>this.format)["format"](str);
var res = "";
if (!this.isValueEmpty(val)) {
var str = this.getValueAsStr(val);
res = !this.format ? str : (<any>this.format)["format"](str);
}
if (!!this.survey) {
res = this.survey.getExpressionDisplayValue(this, val, res);
}
return res;
}
/**
* You may set this property to "decimal", "currency", "percent" or "date". If you set it to "currency", you may use the currency property to display the value in currency different from USD.
Expand Down
24 changes: 24 additions & 0 deletions src/survey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -900,6 +900,17 @@ export class SurveyModel extends Base
SurveyModel
> = this.addEvent<SurveyModel>();

/**
* The event is fired before expression question convert it's value into display value for rendering.
* <br/> `sender` - the survey object that fires the event.
* <br/> `options.question` - The expression question.
* <br/> `options.value` - The question value.
* <br/> `options.displayValue` - the display value that you can change before rendering.
*/
public onGetExpressionDisplayValue: EventBase<SurveyModel> = this.addEvent<
SurveyModel
>();

//#endregion

constructor(jsonObj: any = null) {
Expand Down Expand Up @@ -1572,6 +1583,19 @@ export class SurveyModel extends Base
this.onTextRenderAs.fire(this, options);
return options.renderAs;
}
getExpressionDisplayValue(
question: IQuestion,
value: any,
displayValue: string
): string {
const options = {
question: question,
value: value,
displayValue: displayValue,
};
this.onGetExpressionDisplayValue.fire(this, options);
return options.displayValue;
}
private getBuiltInRendererForString(element: Base, name: string): string {
if (this.isDesignMode) return LocalizableString.editableRenderer;
return undefined;
Expand Down
17 changes: 17 additions & 0 deletions tests/question_expressiontests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,23 @@ QUnit.test("Display text + fraction digitals", function(assert) {
expression.maximumFractionDigits = 2;
assert.equal(expression.displayValue, "21.33", "2 digits");
});
QUnit.test("Display text + survey.onExpressionDisplayValue event", function(
assert
) {
var survey = createSurveyWith3Questions();
survey.onGetExpressionDisplayValue.add((sender, options) => {
//if (options.question.name !== "exp") return;
options.displayValue = "$" + options.displayValue + ".";
});
var expression = <QuestionExpressionModel>(
survey.pages[0].addNewQuestion("expression", "exp")
);
expression.expression = "10";
survey.data = { q1: 4 };
expression.displayStyle = "decimal";
assert.equal(expression.displayValue, "$10.", "Use event");
});

function createSurveyWith3Questions(): SurveyModel {
var survey = new SurveyModel();
var page = survey.addNewPage();
Expand Down