freemarker写select组件

freemarker写select组件

freemarker写select组件

1、宏定义

<#macro select id datas value="" key="" text="" headKey="" headValue="">
     <select id="${id}" name="${id}">
          <option>---请选择---</option>
          <#if headKey!="">
               <option value="${headKey}">${headValue}</option>
          </#if>
          <#if datas?is_hash_ex>
             <#local keys=datas?keys/>
             <#list keys as key>
                  <#if key==value>
                      <option value="${key}" selected>${datas[key]}</option>
                  <#else>
                      <option value="${key}">${datas[key]}</option>
                  </#if>
             </#list>
          <#else>
             <#list datas as data>
              <#if key!="">
                  <#if value == data[key]?string>
                 	<option value="${data[key]}" selected>${data[text]}</option>
                  <#else>
                   <option value="${data[key]}">${data[text]}</option>
                 </#if>
              <#else>
                  <#if value == data>
                 	<option value="${data}" selected>${data}</option>
                 <#else>
                 <option value="${data}">${data}</option>
              </#if>
             </#if>
          </#list>
        </#if>

     </select>
</#macro>

2、宏引入

<@items.select id="sex" datas={"0":"请选择学历","1":"小学","2":"初中","3":"高中","4":"本科"} />

3、运行结果

<select id="sex" name="sex">
          <option>---请选择---</option>
                      <option value="0">请选择学历</option>
                      <option value="1">小学</option>
                      <option value="2">初中</option>
                      <option value="3">高中</option>
                      <option value="4">本科</option>

     </select>
04-15 03:04