Giter VIP home page Giter VIP logo

Comments (9)

Yermo avatar Yermo commented on June 30, 2024

It's rather simple. Create a web service that expects to get a term to search and responds with possible matches for that term. In my own code I use JSON for the request and reply.

The web service, in my case, returns an array of possible matches in the format that is included in the hard coded example.

So all you need to do, once you have your web service done replace the hard coded block of autocomplete matches with an ajax call to the web service

callback: function( term, response )
   {
   ddt.log( "# callback with term '" + term + "'" );

   // set up your ajax call here and send it the value of term in the request.
   // you'll probably want to do it asynchronously so you'll set a success callback. which 
   // then forwards the results to autocomplete

   // have your web service respond with a data structure that looks like the following:

   [ 
{ label: 'tag1', value: { value: 'tag1', content: '<span class="ui-button ui-state-default ui-widget ui-corner-all ui-button-text-only">Tag1</span>' } },
{ label: 'tag2', value: { value: 'tag2', content: '<span class="ui-button ui-state-default ui-widget ui-corner-all ui-button-text-only">Tag2</span>' } },
{ label: 'tag3', value: { value: 'tag3', content: '<span class="ui-button ui-state-default ui-widget ui-corner-all ui-button-text-only">Tag3</span>' } },
{ label: 'tag4', value: { value: 'tag4', content: '<span class="ui-button ui-state-default ui-widget ui-corner-all ui-button-text-only">Tag4</span>' } }
]

   // then in your success callback just feed the response and term back into the autocomplete.
   response( $.ui.autocomplete.filter( tags, term ) );
 }

from rich_textarea.

klaasruysschaert avatar klaasruysschaert commented on June 30, 2024

Thank you so much for your reply. I'm trying to implement your suggestion. I get the response from the web service (php) but I can't feed it back to the autocomplete.

callback: function( term, response ) 
     {
     console.log( "# callback with term '" + term + "'" );
     $.ajax({  
          url: "path-to-the-php",  
          data: { term : term },  
          success: function(data)
          {
               var tags = data;
               console.log(tags); /* see below */
               response($.ui.autocomplete.filter(tags, term));
           }  
     });
}

and this is the response I get

[ { label: 'tag1', value: { value: 'tag1', content: '<span>Tag1</span>' } },
{ label: 'tag2', value: { value: 'tag2', content: '<span>Tag2</span>' } },
{ label: 'tag3', value: { value: 'tag3', content: '<span>Tag3</span>' } },
{ label: 'tag4', value: { value: 'tag4', content: '<span>Tag4</span>' } } ]

Really suffering on this... :)

from rich_textarea.

Yermo avatar Yermo commented on June 30, 2024

This looks correct to me.

Are you getting any errors in the javascript error console?

What version of jQuery and jQuery UI are you using?

from rich_textarea.

klaasruysschaert avatar klaasruysschaert commented on June 30, 2024

Thank you for your response. jQuery and jQuery UI are the same as in your demo page.
Weird things going on... It works fine with this:

success: function(tags)
          {
          tags = ['tag1','tag2','tag3'];     
          response($.ui.autocomplete.filter(tags, term));
          }

But is won't work with this:

success: function(tags)
          {
               response($.ui.autocomplete.filter(tags, term));
          }

The only code in my web service php is this:

echo "['tag1','tag2','tag3']";

:'(

from rich_textarea.

Yermo avatar Yermo commented on June 30, 2024

check typeof( tags ). I bet you'll find that it's a string and not an object.

from rich_textarea.

Yermo avatar Yermo commented on June 30, 2024

You probably need to add dataType: 'json' to your ajax call.

from rich_textarea.

klaasruysschaert avatar klaasruysschaert commented on June 30, 2024

Ooooh Yermo! You rock! A simple eval turned my misery into your magic! Thank you so much!
👍

from rich_textarea.

Yermo avatar Yermo commented on June 30, 2024

You're welcome. Let me know if you run into any problems. If your project is ever publicly visible I'd love to take a look at what you're working on.

from rich_textarea.

klaasruysschaert avatar klaasruysschaert commented on June 30, 2024

Will do. Thanks again!

from rich_textarea.

Related Issues (9)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.