Sort json?
Hi everyone -
I'm trying to create an HTML table but want to be able to sort it, probably by using a choice field that would modify the order. I can't just sort the select because one of the fields is a sum() of a collection of records. I have this code:
let getData := (select Reservations);
let monthList := sort(unique(getData.month_calc));
for loop1 in monthList do
let x := getData[month_calc = loop1];
{
monthName: first(x.month_name),
Nights: sum(x.Nights)
}
end
which gives me the following data:
[{"monthName":"January","Nights":5},{"monthName":"March","Nights":10},{"monthName":"July","Nights":13},{"monthName":"August","Nights":13},{"monthName":"September","Nights":3},{"monthName":"October","Nights":8},{"monthName":"November","Nights":4}]
I would like to sort by Nights or by monthName.
Is there another way of doing this?
11 replies
-
Can you send a sample database?
-
Fred
I don't know if this is what you need, I have this HTML table and by 'Busqueda por Categoria' (Multiple Choice dynamic) I can search my spenses by each category, example by Gas, Water etc
-
a JSON variable is like table result. You use functions sum, order, first, last…
To sort your result, just add « Order by Nights » after the for loop :
or, use one variable like « var res := for i…. » and after sort res as you whant : « if sortByNights then res order by Nights else res order by monthName.
-
Thanks . I was hoping for something simple and you have provided. I did not know you could add order by to the end of a for loop.
Here is the final code:
let getData := (select Reservations); let monthList := sort(unique(getData.month_calc)); for loop1 in monthList do let x := getData[month_calc = loop1]; { monthName: first(x.month_name), Nights: sum(x.Nights) } end order by -number(Nights)
For some reason Ninox does not treat Nights as a number field so I had to add the number() command before Ninox would accept the " - " sign to do a reverse sort.
Here is what the table looks like after the sort:
Excuse the red background, I took the code from another field and haven't bothered to fuss with the formatting while I worked on making things work.
Content aside
- Status Answered
- 1 yr agoLast active
- 11Replies
- 189Views
-
5
Following