• 使用动态数据的autocomplete标签


        autocomplete标签支持Ajax方式获取动态数据,如果下拉框庞大,只能使用动态数据。

     1 <body>
     2 
     3 
     4 <s:url id="dataUrl" value="/autocompleteAjaxData.jsp" />       <!-- 获取数据的URL -->
     5 
     6 请选择国家:
     7 <struts:autocompleter name="country" theme="ajax" href="%{dataUrl}"
     8     loadOnTextChange="true" loadMinimumCount="1" autoComplete="false"
     9     showDownArrow="true" indicator="indicator"  />             <!-- 指定href、指定indicator -->
    10     <img id="indicator" src="images/loading.gif" />            <!-- indicator图片 -->
    11 
    12 
    13 </body>

        href属性指定获取数据的URL。该URL会根据当前输入的内容动态地筛选内容,返回下拉框数据。indicator属性可以为下一个图片或者一段文字,autocomplete获取数据时会显示indicator。获取数据的URL代码如下:

     1 <%@ page language="java" contentType="text/html; charset=UTF-8"%>
     2 <%!Log log = LogFactory.getLog(getClass());%>
     3 <%
     4     out.clear();                                       // 清空所有输出
     5 
     6     request.setCharacterEncoding("UTF-8");             // 设置request编码
     7 
     8     response.setHeader("Pragma", "no-cache");          // 禁止缓存
     9     response.setHeader("Cache-Control", "no-cache");   // 禁止缓存
    10     response.setDateHeader("Expires", 0);              // 禁止缓存
    11 
    12     String[] countries = { "Afghanistan", "Aland Islands", "Albania",  // 国家代码
    13             "Algeria", "American Samoa (USA)", "Andorra", "Angola",
    14             "Anguilla", "Antarctica", "Antigua and Barbuda",
    15             "Argentina", "Armenia", "Aruba", "Australia", "Austria",
    16             "Azerbaijan", "Bahamas", "Bahrain", "Bangladesh",
    17             "Barbados", "Belarus", "Belgium", "Belize", "Benin",
    18             "Bermuda", "Bhutan", "Bolivia", "Bosnia and Herzegovina",
    19             "Botswana", "Bouvet Island", "Brazil",
    20             "British Indian Ocean Territory (UK)", "Brunei Darussalem",
    21             "Bulgaria", "Burkina Faso", "Burundi",};
    22 
    23     String country = request.getParameter("country");    // 当前autocomplete已填写的内容
    24 
    25     if (country == null)                                 // 如果没有填写,当做空字符串
    26         country = "";
    27 
    28     StringBuffer buffer = new StringBuffer();            // StringBuffer
    29 
    30     for (int i = 0; i < countries.length; i++) {         // 遍历所有的国家
    31         if (countries[i].toLowerCase()
    32             .startsWith(country.toLowerCase())) {
    33             if (buffer.length() != 0)
    34         buffer.append(",");
    35             buffer.append("['" + countries[i] + "']");   // 如果包含已输入的字符串则显示
    36         }
    37     }
    38     
    39     Thread.sleep(500);                                   // 为演示indicator,让数据延迟0.5秒钟显示
    40 
    41     out.print("[" + buffer + "]");                       // 输出数据
    42 
    43     log.info("queryString: " + request.getQueryString() + ", result: ["
    44             + buffer + "]. ");
    45             
    46 %>

        autocomplete标签能够接受JSON格式的数据,因此该JSP会把数据以JSON格式输出,例如:[['Andorra'],['Angola'],[Anguilla']]

  • 相关阅读:
    oracle重新学习,主要是命令实用,有借鉴其他人博客
    cdn缓存立刻刷新
    python3发送需要双向认证的wss请求
    【2021.08.03】平等交流
    【2021.07.31】正面回应才是面对挑战的最好方式
    【2021.07.28】向晚
    【2021.07.25】过个生日
    【2021.07.17】Asoul——那些无法杀死我的,终会使我变得更加强大
    phaser3微信小游戏2
    eve-ng 配置apache2的虚拟目录
  • 原文地址:https://www.cnblogs.com/Nickzerui/p/4624325.html
Copyright © 2020-2023  润新知