在JSP中使用Jquery Ajax自动完成

时间:2013-03-28 14:03:19

标签: json jsp jquery autocomplete

我正在尝试关注this示例 我在JSP页面中有以下内容

(getData.jsp)

Department t = new Department ();    
    String query = request.getParameter("q");    
    List<String> tenders = t.getDepartments(query); 

    Iterator<String> iterator = tenders.iterator();
    while(iterator.hasNext()) {
        String deptName= (String)iterator.next();
        String depto = (String)iterator.next();
        out.println(deptName);
    }

如何在Jquery autcomplete中使用上述内容?当我尝试时,没有输出。

我的Jquery自动完成代码

 <script>
$(function() {

$( "#dept" ).autocomplete({
source: function( request, response ) {
$.ajax({
url: "getData.jsp",
dataType: "jsonp",
data: {
featureClass: "P",
style: "full",
maxRows: 12,
name_startsWith: request.term
},
success: function( data ) {
response( $.map( data.<??>, function( item ) {
return {
label: item.name + (item.<??> ? ", " + item.<??> : "") + ", " + item.<??>,
value: item.name
}
}));
}
});
},
minLength: 2,
select: function( event, ui ) {
alert(ui.item.label);
}
});
});
</script>

1 个答案:

答案 0 :(得分:1)

您的回复是否为JSON格式?

以下是我使用Jquery UI自动完成时的操作:

  1. 创建一个类,其参数是您在说item.name

    时将使用的参数
    public string Pam1{ get; set; }
    
    public string Pam2{ get; set; }
    
    public string Pam3{ get; set; }
    
    
    public SomeResponse(string SomePam)
    {
        // Pam1 = ???
        // Pam2 = ???
        // Pam3 = ??? 
    }
    
  2. 在您的处理程序中:

        context.Response.ContentType = "application/json";
        string query = (string)context.Request.QueryString["query"];
        var json = new JavaScriptSerializer();
    
        context.Response.Write(
            json.Serialize(new SomeResponse(query))
        );
    
        context.Response.Flush();
        context.Response.End();
    
  3. 修改

    1. javascript(以下是一个示例,用户可以选择多个选项,以逗号分隔。如果您不想这样,请将其删除。)txt_autocomplete是TextBox的类。

      $(function () {
          function split(val) {
              return val.split(/,\s*/);
          }
      
          function extractLast(term) {
              return split(term).pop();
          }
      
          $(".txt_autocomplete")
          // don't navigate away from the field on tab when selecting an item
          .bind("keydown", function (event) {
              if (event.keyCode === $.ui.keyCode.TAB &&
                  $(this).data("ui-autocomplete").menu.active) {
                  event.preventDefault();
              }
          })
              .autocomplete({
              source: function (request, response) {
                  $.getJSON("handlers/autocomplete.ashx?query=" + extractLast(request.term), {
                      term: extractLast(request.term)
                  }, response);
              },
              search: function () {
                  var term = extractLast(this.value);
                  if (term.length < 2) {
                      return false;
                  }
              },
              focus: function () {
                  return false;
              },
              select: function (event, ui) {
                  var terms = split(this.value);
                  terms.pop();
                  terms.push(ui.item.Pam1);
                  terms.push("");
                  this.value = terms.join(", ");
                  console.log("Pam1 is :" + ui.item.Pam1 + " Pam2 is: " + ui.item.Pam2 + " Pam 3 is : " + ui.item.Pam3);
                  return false;
              }
          });
       });