自动完成jquery struts 2

时间:2012-05-30 09:29:12

标签: jquery autocomplete struts2

使用自动填充jquery工具时出现问题

客户方:

JQElement.autocomplete({
   source:  function (request, response) {
        $.ajax({
            url: jsonAction,
            dataType: "json",
            data: {
                maxRows: 10,
                startsWith: request.term
            },
            success: function( data ) {
                response( $.map( data , function( item ) {
                        return {
                                label: item.label,
                                value: item.value
                        }
                }));
                //response(data);
            },
            error: function(message, status, errorThrown) {
                alert("une erreur s'est produit lors de la recherche des éléments correspondant à la saisie. Contacter les créateurs du programme");
            }
        });
    },
    minLength: 1
 });

该行动被称为:

public String getInsuredNumbers() {
    try {
        String maxRows = request.getParameter("maxRows");
        String startsWith = request.getParameter("startsWith");

        if(maxRows.equals("")) maxRows = "10";
        if(startsWith.equals("")) startsWith = "17";

        String sql = "select assure.ASS_nni, assure.ASS_nom, assure.ASS_prenom from assure where "
                + "assure.ASS_nni like '" + startsWith + "%' limit " + maxRows;
        ResultSet rs = this.getResponse(sql);

        while(rs.next()) {
            Param p = new Param(rs.getString(1), rs.getString(1) + " " + rs.getString(2) + " " + rs.getString(3));
            data.add(p);
        }


    } catch (SQLException ex) {
        Logger.getLogger(AutocompleteAction.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        return SUCCESS;
    }
}

public List<Param> getData() {
    return data;
}

data是Param的ArrayList

public class Param {
  private String value;
  private String label;

  public Param(String value, String label) {
    this.value = value;
    this.label = label;
  }

  public String getLabel() {
    return label;
  }

  public String getValue() {
    return value;
  }

}

Firebug bug告诉我json答案很好 成功答案在客户端触发

但是输入下的列表似乎是空的

你可以帮忙吗?

谢谢

1 个答案:

答案 0 :(得分:0)

解决方案

在客户端:

JQElement.autocomplete({
   source:  function (request, response) {
        $.ajax({
            url: jsonAction,
            dataType: "json",
            data: {
                maxRows: 10,
                startsWith: request.term
            },
            success: function( data ) {
                var mydata;

                $.map(data, function(item, i) {
                    if(i == "data") {
                        mydata = item;
                    }
                });

                response( $.map( mydata , function( item) {
                        return {
                            label: item.label,
                            value: item.value
                        }
                }));
            },
            error: function(message, status, errorThrown) {
                alert("une erreur s'est produit lors de la recherche des éléments correspondant à la saisie. Contacter les créateurs du programme");
            }
        });
    },
    minLength: 1,
相关问题