How to use jQuery's formatItem with autocomplete?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • BaseballGraphs
    New Member
    • Sep 2010
    • 75

    How to use jQuery's formatItem with autocomplete?

    Hello,

    I am trying to format the output of my data with the autocomplete function from jQuery. So far, I've been able to build the entire process, however, I want to limit the information I show in the autocomplete, but I still want the autocomplete to search through the entire string to find possible matches.

    I understand that the formatItem function used in conjunction with the autocomplete from jQuery can accomplish this.

    To give you some background here is the entire sequence of my code:
    Code:
     $sql = ( 'SELECT cr, title, author, content, id FROM da_labels' );
      $result = mysql_query( $sql );
      $Response = array();
      while( $row = mysql_fetch_object( $result ) ){
           $Response[] = array(
                              "id" => $row->id,
                              "name" => $row->cr . ": " . $row->title . ": " . $row->content,
                             );
          }
    The above is the php I use to select the data for the autocomplete to look up. The problem is that the data associated with 'content' is enormously large and I would prefer that it did not show in the autocomplete suggestions, but I still would like the autocomplete to look at it in order to come up with a suggestion.

    Code:
    <script>
          var AllCRData = <?= json_encode ( $Response ); ?>;
          var CRs = [];
    
          for(var i in AllCRData){
             CRs.push(AllCRData[i].name);
          }
    
          function getIdFromCR(_name){
              for(var i in AllCRData){
                  if(_name == AllCRData[i].name){
                      return AllCRData[i].id;
                  }
              }
          }
    
      $(document).ready(function(){
             $("#Responses").autocomplete({
                 source: CRs,
                  select: function(e, ui){
                     $("#hidden_cr").val( getIdFromCR($("#Responses").val()) );
                  }});
         });
    </script>
    The above is the javascript I'm using to populate the autocomplete. I believe that the formatItem function would appear somewhere in the final portion of the javascript, but I am not sure how to implement it for my purposes.

    I would like to format my autocomplete, so that it only shows $row->cr and $row->title from the php above, while hiding the $row->content, yet, still searching through the 'content' in order to find the most appropriate option.

    I would really appreciate your assistance on how to implement the formatItem function for the autocomplete for jQuery.

    Thanks!
Working...