0

I'm trying to create a search suggestion input box with Cakephp 3. I found a tutorial online and i'm trying to implement it but i don't get a suggestion as mentioned in the tutorial. (http://www.naidim.org/cakephp-3-tutorial-18-autocomplete)

PagesController.php

public function chargesadd()
 {
    $this->loadModel('Charges');

    if ($this->request->is('ajax')) {
    $this->autoRender = false;
    $name = $this->request->query['term'];
    $results = $this->Customers->find('all', [
        'conditions' => [ 'OR' => [
            'name LIKE' => $name . '%',
            'id LIKE' => $name . '%',
        ]]
    ]);
    $resultsArr = [];
    foreach ($results as $result) {
         $resultsArr[] =['label' => $result['name'], 'value' => $result['id']];
    }
    echo json_encode($resultsArr);
 }
}

chargesadd.ctp

<?php use Cake\Routing\Router; ?>

<?php 
  echo $this->Form->input('customer_id', ['type' => 'text']);
?>

<script>
    jQuery('#customer-id').autocomplete({
         source:'<?php echo Router::url(array('controller' => 'Pages', 'action' => 'chargesadd')); ?>',
        minLength: 3
    });
</script>

I have a feeling there's something wrong with the $resultsArr how it's been outputted. But i can't properly figure out what's wrong with it.

I'm using CakePHP 3.4.7

Kasun Wijesekara
  • 167
  • 1
  • 1
  • 13

1 Answers1

0

I tried this method but it didn't work out for some reason, if someone is trying to do an autocomplete feature or a search suggestion feature try this out.

( https://silviomoreto.github.io/bootstrap-select/ )

You need to generate the dropdown. That's basically it. And add the "selectpicker" class and the relevant cdn resources.

<select class="selectpicker">
 <option>Mustard</option>
 <option>Ketchup</option>
 <option>Relish</option>
</select>

Demo - https://codepen.io/Rio517/pen/NPLbpP

Kasun Wijesekara
  • 167
  • 1
  • 1
  • 13