Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Special thanks to Andrew for pointing out this <a href="http://api.jqueryui.com/autocomplete/" rel="nofollow"> api.jqueryui.com/autocomplete</a>. However there is not a real guide to use this one. So i found this <a href="http://cakephp.1045679.n5.nabble.com/CakePHP-2-2-and-Autocomplete-Jquery-td5712209.html" rel="nofollow">post</a>, which explains what Abhishek's second link says but I could understand it better. So here is my solution if anyone is interested:</p> <p>1 - Download from the autocomplete page the .js you need. Save it in app/webroot/js</p> <p>2 - Either in your app/View/Layouts/default.ctp or in the view you want to use the autocomplete add:</p> <pre><code>echo $this-&gt;Html-&gt;script('jquery-1.9.1.js'); echo $this-&gt;Html-&gt;script('jquery-ui-1.10.3.custom.js'); echo $this-&gt;fetch('script'); </code></pre> <p>3 - In your view add (mine was add_goal.ctp):</p> <pre><code>&lt;script&gt; $(document).ready(function(){ var myselect = document.getElementById("EventTeam"); //I needed to know which team I was looking players from. var team = myselect.options[myselect.selectedIndex].value; //"EventTeam" was a dropdown list so I had to get the selected value this way. $("#EventPlayer").autocomplete({ source: "/events/autoComplete/" + team, minLength: 2, //This is the min ammount of chars before autocomplete kicks in autoFocus: true }); $("input:submit").button(); $("#EventPlayerId").autocomplete({ select: function(event, ui) { selected_id = ui.item.id; $('#EventAddGoalForm').append('&lt;input id="EventPlayerId" type="hidden" name="data[Event][player_id]" value="' + selected_id + '" /&gt;'); } }); $("#EventPlayerId").autocomplete({ open: function(event, ui) { $('#EventPlayerId').remove(); } }); }); &lt;/script&gt; </code></pre> <p>4 - In your Controller (mina was EventController.php):</p> <pre><code>public function autoComplete($team = null){ Configure::write('debug', 0); $this-&gt;autoRender=false; $this-&gt;layout = 'ajax'; $query = $_GET['term']; $players = $this-&gt;Event-&gt;Player-&gt;find('all', array( 'conditions' =&gt; array('Player.team_id' =&gt; $team, 'Player.name LIKE' =&gt; '%' . $query . '%'), 'fields' =&gt; array('name', 'id'))); $i=0; foreach($players as $player){ $response[$i]['id']=$player['Player']['id']; $response[$i]['label']=$player['Player']['name']; $response[$i]['value']=$player['Player']['name']; $i++; } echo json_encode($response); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload