mdbootstrap / bootstrap-templates

A collection of free Bootstrap 5 templates.
https://mdbootstrap.com/freebies/
3.07k stars 1.02k forks source link

Question get tags #580

Closed tasmanwebsolutions closed 2 years ago

tasmanwebsolutions commented 7 years ago

I use codeigniter and I encode my json data like

public function tags() {
$data = array();

$query = $this->db->get('category');

$results = $query->result_array();

foreach ($results as $result) {
    $data[] = array(
        'category_id' => $result['category_id'],
        'name' => $result['name']
    );
}

$this->output ->set_content_type('application/json');
$this->output->set_output(json_encode($data));
}

Which the json output results

[{"category_id":"1","name":"codeigniter"},{"category_id":"2","name":"php"},{"category_id":"3","name":"html"},{"category_id":"4","name":"css"},{"category_id":"5","name":"javascript"}]

I when I click on the input the list does not drop down for me to select category

What am I doing wrong? Bloodhound not defined but scripts are loaded.

<input type="text" id="input-tag" />

<script type="text/javascript">
$(document).ready(function() {

var categories = new Bloodhound({
    datumTokenizer: Bloodhound.tokenizers.obj.whitespace('text'),
    queryTokenizer: Bloodhound.tokenizers.whitespace,
    prefetch: "<?php echo base_url('questions/tags');?>"
});

categories.initialize();

var elt = $('#input-tag');
elt.tagsinput({
   itemValue: 'value',
   itemText: 'text',
   typeaheadjs: {
   name: 'tags',
   displayKey: 'text',
  source: categories.ttAdapter()
}
});

});
dblleaf commented 7 years ago

I spent a lot of time on this issue. You can fix it like this: var categories = new Bloodhound({ datumTokenizer: Bloodhound.tokenizers.obj.whitespace('text'), queryTokenizer: Bloodhound.tokenizers.whitespace, remote:{ url: "<?php echo base_url('questions/tags');?>" } });