Despite the COVID-19 outbreak, our team continues operating at full speed. We are always here to support and answer all your questions.

Feel free to reach out by filling this quick form.

Fill the form
Get Free Trial

Adding Calculated Value inline code

David Chen asked on March 1, 2016

Can anyone create a jsfiddle showing up to create a calculated field
for example

var report = {
configuratorActive: false,
data: jsondata,
rows: [{uniqueName: “Color”}],
columns: [{uniqueName: “[Measures]”}],
measures: [{uniqueName: “Price”}, {uniqueName: “Quantity”}],
addCalculatedValue: [……]

formats: [{name: “”, nullValue: “blank”}],
showHeaders: false

6 answers

Iryna Kulchytska Flexmonster March 1, 2016

Hello David,

Here is a jsfiddle that addresses your question:
Calculated fields go in ‘measures’ array with all the measures, as follows:
measures: [{uniqueName: “Price”, aggregation: “average”, availableAggregations: [“average”], active: true}, {uniqueName: “Quantity”, aggregation: “average”, availableAggregations: [“average”]}, {uniqueName: “Formula #1”, caption: “Formula #1”, calculated: true, formula: “sum(‘Price’) / sum(‘Quantity’)”, active: true}]
Kind regards,

David Chen March 1, 2016

Thanks. How do I sort a variable in the column descending?

Iryna Kulchytska Flexmonster March 1, 2016

Hi David,
You can sort values in some column by specifying measure, tuple and type in columnSorting property as follows:

columnSorting: {measure: "Price", tuple: "", type: "desc"}

Here is a jsfiddle:
Please let me know if it is what you are looking for.
Kind regards,

David Chen March 1, 2016

so if I want to sort Color first then another row, it would be

columnSorting: [ {measure: "SortFirstVariable", tuple: "", type: "desc"},{measure: "ThenSortSecondVariable", tuple: "", type: "desc"} ]
Iryna Kulchytska Flexmonster March 1, 2016

Thank you for your question.
If you want to sort colors in Color field, then you can do this from report object the following way:

rows: [{uniqueName: "Color", sortName: "desc"}]

The previous example is about the case when you want to sort numbers in columns on the grid. 
Also, please note that the sorting can be done from UI. Then you can call flexmonster.getReport() API call to get the report object with all the settings and you can examine it to find out how the change from UI is stored in the report object.
Please let me know if you need more clarifications.
Kind regards,

Tanya Gryshko Flexmonster December 12, 2016

Hello David and all Flexmonster users,
This updated sample shows how to create a calculated field in version 2.3:
Starting from version 2.3, the structure of Report Object was changed. columnSorting property was renamed to slice.sorting.column. Descending sorting for a variable in the column is defined the following way:

sorting: {
    column: {measure: "Price", tuple: "", type: "desc"}

Open in JSFiddle:
sortName property was renamed to sort. Sorting colors in Color field should be defined like this:

rows: [{uniqueName: "Color", sort: "desc"}]

Please check the updated sample:
Find more details about the new structure of Report Object in the documentation:

Please login or Register to Submit Answer