Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get last record from list in freemarker

    <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

like image 330
Anil Avatar asked May 04 '11 06:05

Anil


3 Answers

You could do something like

<#list payrollYears as year> 
      <option value="${year.year}" <#if !(year_has_next)>selected</#if> >${year.yeardesc}</option>
</#list> 
like image 127
Dirk Avatar answered Nov 01 '22 01:11

Dirk


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

like image 20
jpllosa Avatar answered Nov 01 '22 00:11

jpllosa


<#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.***
like image 3
Vibhu Garg Avatar answered Nov 01 '22 01:11

Vibhu Garg