0

I have a problem using the autocomplete jquery tool

client side :

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
 });

the action is called :

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 is an ArrayList of Param

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 shows me that the json answer is good the success answer is triggered on client side

However the list under the input appears to be empty

Can you help on that ?

Thank you

4

1 回答 1

0

Solution

On client-side :

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,
于 2012-05-30T11:21:36.467 回答