SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,407
    Mentioned
    81 Post(s)
    Tagged
    3 Thread(s)

    jquery autocomplete problem: multiple text boxs with same content + text file source

    I am having a hard time with jquery autocomplete and hope somebody can give me some tips.

    1/ I am getting my data from a database to populate the array and this is what I have when I view source:
    Code:
      <script>
      $(function() {
        var availableTags = [
    "13th Floor Records",
    "77 Records",
    "A Cappella Records",
    "A&E Records",
    "Flair Records",
    "FRE Records",
    "Goldenrod Records",
    "Hermes records",
    "Reprise Records",
    "Rhythm King",
    "Xemu Records"   ];
        $( "#tags" ).autocomplete({
          source: availableTags
        });
      });
      </script>
    This works OK but I have the same autocomplete box 4 times on one page and I thought I would call the same code 4 times on the page but this will not work. The first time it is called is the only one that will work.
    Code:
    <label for="tags">Recording company: </label>
    <input name="tags" id="tags" /><br/>
    So I have 4 copies of the script with different names.

    2/ There are going to be a lot of items in the array and as above was not working how I wanted I thought I would use a text file pre generated from the database and found this example which will not work at all:
    Code:
    <script>
    jQuery.get( 'companies.txt' ).done(function( data ) {
    $( "#performer3" ).autocomplete({
    source: 'companies.txt'
    });
    });
    </script>
    Also any comments on wether 4 database calls are better/worse than reading a text file 4 times?

  2. #2
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,407
    Mentioned
    81 Post(s)
    Tagged
    3 Thread(s)
    I will checkout mootools and see if that is any better.

  3. #3
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,407
    Mentioned
    81 Post(s)
    Tagged
    3 Thread(s)
    Just getting back to this after working on the database section and have decided to give up with the Jquery UI autocomplete.
    I can not sort my problems or find an answer that works online and thought about joining the Jquery UI forum. The last question in the autocomplete section was in 2010 and only one or two of the questions there have received an answer.

  4. #4
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,939
    Mentioned
    215 Post(s)
    Tagged
    12 Thread(s)
    Hi Rubble,

    I just saw this having been away for a while (holiday).
    If you fancy, we could have a shot at making this work with jQuery UI autocomplete.
    I'm sure it won't be too difficult to sort out (famous last words ).
    Just let me know.

  5. #5
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,407
    Mentioned
    81 Post(s)
    Tagged
    3 Thread(s)
    Thanks for the offer Pullo.
    I also spent about three hours last night trying to get the Devbridge version working. The trouble is with both these pieces of code is there are not enough examples/instructions for somebody who knows nothing about JavaScript!

    I am at work at the moment and can not do very much but this is some code that I have working to some extent. I have got around the multiple versions on one page by calling each part of the code a different name e.g. performer_A1, performer_A2 and using different sources available Performer1, availablePerformer2.

    Code:
       <script>
      $(function() {
        var availablePerformer1 = [ 
    "Anna Netrebko",
    "Enrico Caruso",
    "Joan Sutherland",
    "Jose Carreras ",
    "Kiri Te Kanawa",
    "Luciano Pavarotti ",
    "Maria Callas",
    "Placido Domingo",
    "qwerty"   ];
    $( "#performer_A1" ).autocomplete({
    source: availablePerformer1
    });
    });
    </script>
    
       <script>
      $(function() {
        var availablePerformer2 = [ 
    "Anna Netrebko",
    "Enrico Caruso",
    "Joan Sutherland",
    "Jose Carreras ",
    "Kiri Te Kanawa",
    "Luciano Pavarotti ",
    "Maria Callas",
    "Placido Domingo",
    "qwerty"   ];
    $( "#performer_A2" ).autocomplete({
    source: availablePerformer2
    });
    });
    </script>
    The wordlist is being populated by a php query ( which I can not post at the moment as I do not have access to it ) but this is not very efficient.

    As I say I can not do a lot now until I get to my PC and I can post more of the code.

  6. #6
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,939
    Mentioned
    215 Post(s)
    Tagged
    12 Thread(s)
    Hi Rubble,

    No problem

    Having two different arrays with the same content is not optimal and I'm sure we can do that better.

    Just to check that I've understood everything correctly thus far:

    You want four autocomplete boxes on the same page which are populated from a database.
    They should all contain the same data.
    Using jQuery autocomplete (with the source attribute) you got the first of them working, but the other three are refusing to play ball.

    Is that right?

    I should be around tonight, so I'm sure we'll get this sorted out soon.

  7. #7
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,407
    Mentioned
    81 Post(s)
    Tagged
    3 Thread(s)
    Using jQuery autocomplete (with the source attribute) you got the first of them working, but the other three are refusing to play ball.
    They all work now ( except for one doubles up the results for some reason ) but I need a database call for each one and they are all named differently. I tried creating a text file and using that as the source as it seemed the best way to do it - one database call when the page loads and then use the text file as the source - but I could not get that to work.

    There are also 12 of these on the one page all using the same data and so you can see the reason for trying to streamline it!

  8. #8
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,407
    Mentioned
    81 Post(s)
    Tagged
    3 Thread(s)
    Pullo came up with the answer:

    Reference all of the necessary elements using the "attribute starts with" selector:

    $("input[name^='performer_'").autocomplete({
    source: availablePerformers
    });

    and pass them the variable we build up using the MySQL query.


    Ideally, the PHP code should be removed from the HTML and into its own file.
    E.g.

    $("input[name^='performer_'").autocomplete({
    source: getPerformers.php
    });
    This populates all 12 autocomplete boxes with the same data.

    The form fields were in this format:
    Code:
    <label for="performer_B1">Artist B1: </label>
    <input name="performer_B1" id="performer_B1" /><br/>
      
    <label for="performer_B2">Artist B2: </label>
    <input name="performer_B2" id="performer_B2" /><br/>
    
    <label for="performer_B3">Artist B3: </label>
    <input name="performer_B3" id="performer_B3" /><br/>


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •