get last record from list in freemarker
Asked Answered
C

4

6
    <select name="showYears">
            <#list payrollYears as year> 
                <option value="${year.year}">${year.yeardesc}</option>
            </#list>        
    </select>

i am getting payrollyears list from my controller and i am iterating the list in freemarker and adding value to select box i want my last value of list should be selected value in last how can i do that

Clamant answered 4/5, 2011 at 6:37 Comment(0)
S
12

You could do something like

<#list payrollYears as year> 
      <option value="${year.year}" <#if !(year_has_next)>selected</#if> >${year.yeardesc}</option>
</#list> 
Second answered 4/5, 2011 at 7:0 Comment(0)
S
8

For FreeMarker 2.3.24, you can do something like year?has_next instead of year_has_next.

  • item_has_next (deprecated by item?has_next): Boolean value that tells if the current item is the last in the sequence or not.

See FreeMarker Docs

Succulent answered 10/5, 2016 at 13:28 Comment(0)
D
3
<#list body.result as school_names_list>
{
  "NAME": <#if school_names_list.NAME??>"${school_names_list.NAME}"<#else>""</#if>,
  "ADDRESS": <#if school_names_list.ADDRESS??>"${school_names_list.ADDRESS}"<#else>""</#if>,
   <#if school_names_list?is_last><#else>,</#if>
</#list>


//Here **school_names_list** is a list and we check the last element though **school_names_list?is_last** (where list name is school_names_list)

//In this example, if it the last element, ***we'll avoid adding "," else we add "," as per JSON rules of a list.***
Diversify answered 31/10, 2018 at 10:54 Comment(1)
In this example we are making a list which is JSON.Hence, as in a json list, all the elements are separated by "," except the last element.Diversify
B
0

Old question, but you can use is_last.

Using your code you could write your code like this;

<select name="showYears">
  <#list payrollYears as year>
    <#if year?is_last>
      <option value="${year.year}" selected>${year.yeardesc}</option>
    <#else>
      <option value="${year.year}">${year.yeardesc}</option>
    </#if>
  </#list>        
</select>

A better way would be to add the selected straight to the HTML, and drop the whole if check around it, but not entierly sure how to add this directly to HTML as valid syntax.

So this probably WON'T work, but my line of thinking is something like this;

<select name="showYears">
  <#list payrollYears as year>
    <option value="${year.year}" ${year?is_last?then('selected', '')}>${year.yeardesc}</option>
  </#list>        
</select>
Blasphemy answered 1/9, 2023 at 13:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.