Bring SQL data to jquery availabletag

I'm trying to make an autocomplete textbox, but how can I include the SQL Data in the jquery available tag and loop it? I was unable to execute a function based on the following code. Any help would be appreciated! Thanks to

This is my expected output: Expected Output Demo

Error in jquery code

My textbox only shows the last row of data from the database. enter image description here

<%
    String FRUIT_CODE = "";
    String FRUIT_DESCP= "";
    Vector vFruit     = new Vector();

    TEST.makeConnection();
    String SQL = "SELECT CODE,DESCP FROM TB_FRUIT WITH UR";
    TEST.executeQuery(SQL);     
    while(TEST.getNextQuery())
    {
        FRUIT_CODE  = TEST.getColumnString("CODE");
        FRUIT_DESCP = TEST.getColumnString("DESCP ");
        Vector vRow = new Vector();
       vRow.addElement(FRUIT_CODE);
       vRow.addElement(FRUIT_DESCP);
       vFruit.addElement(vRow);
    }   
    TEST.takeDown();    
   %>

  <html>
  <head>
  <meta charset="utf-8">
  <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
  <script src="//code.jquery.com/jquery-1.10.2.js"></script>
  <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
  <link rel="stylesheet" href="/resources/demos/style.css">

  <script>
  $(function() {
  var availableTags = 
   [{
   <%
     String COMBINE = "";
     String CODE2   = "";
     String DESC1   = "";
     for(int i=0;i<vFruit.size();i++)
     {
        Vector vRow     = (Vector) vFruit.elementAt(i);
            CODE2       = (String) vRow.elementAt(0);
            DESC1       = (String) vRow.elementAt(1);
            COMBINE     += "\"" + CODE2 +"    "+ DESC1 + "\",";     
     }
        COMBINE  = COMBINE.substring(0, COMBINE.length()-1);

       //Combine result = "10000 Apple","20000 Orange", "30000 Mango", "40000 Banana"
    %>

        "value":  <%=CODE2%>,
        "label":  <%=COMBINE%>
    }]; 

   $("#MODEL").autocomplete({
      source: availableTags,
       focus: function (event, ui) {
        event.preventDefault();
        $("#MODEL").val(ui.item.value);
       }
     });
  });
  </script>    

  <body>
       <div class="ui-widget">
         <label for="tags">Tags: </label>
         <input id="MODEL">
       </div>
  </body>
</html>

      

+3


source to share


1 answer


The create script availableTags

does not add a new object for each value in the vFruit

Vector, only the last one. It should be fixed as follows:

var availableTags = [
<%
  String CODE2   = "";
  String DESC1   = "";
  for(int i=0;i<vFruit.size();i++)
  {
    Vector vRow     = (Vector) vFruit.elementAt(i);
    CODE2       = (String) vRow.elementAt(0);
    DESC1       = (String) vRow.elementAt(1);
    if (i > 0) out.print(",");
%>
    {
      "value":  "<%= CODE2 %>",
      "label":  "<%= DESC1 %>"
    }  
<%
  }
%>
]; 

$("#MODEL").autocomplete({
...

      



Btw. Why Vector

, and not, for example ArrayList

? Need a thread-safe implementation? It doesn't seem like it.

0


source







All Articles