Sort and display all times in order.
Hello everyone! In my table, I’ve set up two time fields (as shown in the image). I’m wondering if I can create a code snippet in the ‘View’ table to display all the entries of time1 and time2 in sequential order. I tried some code, but it wasn’t successful. Thanks for your help!
let getCopy := (select 'table');
let pailie := for xx in getCopy do
let times := [];
if xx.'time1' != null then
times := times + [xx.'time1']
end;
if xx.'time2' != null then
times := times + [xx.'time2']
end;
let sortedTimes := sort(times);
let formattedTimes := for time in sortedTimes do
format(time, "YYYY-MM-DD")
end;
formattedTimes
end;
45 replies
-
There is no time data in time2.
Do you mean you want to sort Date then Time?
-
Hi
There are a couple of issues here:
- you are creating an array of dates and trying to add these view element which is not possible - view elements are designed to display records which can then be linked through from the view.
- I have discovered that the array needs to be initialised with a type in order for the addition of elements to work - so let times := [text(void)].
I have created the following code which will display these dates in a formula field (with each on a separate line):
let getTimeRecs := (select Table);
let times := [text(void)];
for timeRec in getTimeRecs do
if timeRec.'Time 1' != null then
times := array(times, [format(timeRec.'Time 1', "YYYY-MM-DD")])
end;
if timeRec.'Time 2' != null then
times := array(times, [format(timeRec.'Time 2', "YYYY-MM-DD")])
end
end;
let sortedTimes := sort(times);
join(sortedTimes, "
")Not sure if this will help you or not but feel free to come back.
-
Hi
You seem to have changed the code so that times are no longer an array so won't sort. I have combined the code I had last time with a simple one column table structure which works for me (note the for loop going through the sorted array to build the data area of the table):
let getTimeRecs := (select Table);
let times := [text(void)];
for timeRec in getTimeRecs do
if timeRec.'Time 1' != null then
times := array(times, [format(timeRec.'Time 1', "YYYY-MM-DD")])
end;
if timeRec.'Time 2' != null then
times := array(times, [format(timeRec.'Time 2', "YYYY-MM-DD")])
end
end;
let sortedTimes := sort(times);
let tableData := "";
for time in sortedTimes do
tableData := tableData + "<tr><td>" + time + "</td></tr>"
end;
html("<table border='1' style='border-collapse: collapse; width: 100%;'>
<tbody>
<tr>
<td style='width: 100%;'>Column Header</td>
</tr>" +
tableData +
"</tbody>
</table>")Hope this helps
-
There is a way I've found (not done this before) which opens the record in a view (I made the view in question a Form View so that no record list showed) - but I haven't discovered how to make it a popup:
let getTimeRecs := (select Table);
let times := [text(void)];
for timeRec in getTimeRecs do
if timeRec.'Time 1' != null then
times := array(times, [format(timeRec.'Time 1', "YYYY-MM-DD") + " #" + timeRec.Id])
end;
if timeRec.'Time 2' != null then
times := array(times, [format(timeRec.'Time 2', "YYYY-MM-DD") + " #" + timeRec.Id])
end
end;
let sortedTimes := sort(times);
let tableData := "";
for time in sortedTimes do
if time then
tableData := tableData + "<tr><td>" + first(split(time, "#")) +
"</td><td><a href = 'https://app.ninox.com/#/teams/{TeamID}/database/{DatabaseID}/module/C/view/{ViewID}/node/C" +
last(split(time, "#")) +
"/tab/0'>Link</a></tr>"
end
end;
html("<table border='1' style='border-collapse: collapse; width: 100%;'>
<tbody>
<tr>
<td>Column Header</td><td>Link</td>
</tr>" +
tableData +
"</tbody>
</table>")As you can see, I added the record ID into the array values preceded with a hash, then spilt it out and used the first and last elements in the table - with the last element being the record ID which I was able to build into a link. You can get the relevant Ids by copying the URL with one of the target records open.
The if time then section removes a blank row which otherwise appears at the top of the table.
-
let XsTable := (select Table); let Xstbody := XsTable.--- <tr onclick="ui.popupRecord('{ Id }')"> <td>{ format('Time 1', "DD-MM-YYYY") }</td> <td>{ format('Time 2', "DD-MM-YYYY") }</td> </tr> ---; html("<div class='bootstrap5'><style></style><table class='table table-striped table-hover caption-top'><thead class='table-primary'><tr><th scope='col'>Time1</th><th scope='col'>Time2</th></tr></thead><tbody>" + Xstbody + "</tbody><tfoot></tfoot></table></div>")
Delete the top two blank lines
-
Hi.
When select the Month on (Seleccion Mes) can't see the Prueba but can see the documents on last 3 month. Some idea ?
-
If you don't want to go the child table route then here is what you need to do make your original code work:
let getTimeRecs := (select Data); let getTime1 := getTimeRecs.Time1; let getTime2 := getTimeRecs.time2; let allTimesSorted := sort(array(getTime1, getTime2)); let newDataTable := for loop1 in allTimesSorted do let rec := first(getTimeRecs[Time1 = loop1 or time2 = loop1]); { id: rec.Id, date: format(loop1, "YYYY,MM,DD"), name: rec.name } end; "-------------------------------------------------------------------------"; let Xstbody := newDataTable.(" <tr onclick=ui.popupRecord('" + raw(first(getTimeRecs[number(Id) = number(Id)])) + "')> <td>" + date + "</td> <td>" + name + "</td> </tr>"); "-------------------------------------------------------------------------";
Lines 1 - 4, I created an array of the data from the Time1 and time2 fields.
Lines 5 - 12, Using the new array from above, I created a JSON that tracks the record Id, the date, and the name associated from the record. It works in the dataset since there are no duplicate dates. If you have a dataset with duplicate dates then that is another solution. :)
Line 16, I used the record Id from the JSON to find the first record where the date matches either Time1 or time2. Then I used the raw() command to just return the table and record Id. So now the ui.popupRecord() now knows which record to open.
Content aside
- Status Answered
- 8 days agoLast active
- 45Replies
- 179Views
-
6
Following