duannue2455 2018-02-21 15:07
浏览 48

如何从数据库中获取数据并将其与自动完成和Bootstrap Tagsinput一起使用? 在Code Igniter中

I have gone through Bootstrap Tags Input docs and identified one of the examples provided and i am using it to integrate tags input in my senders field in the view. The problem i have is calling the controller in my script which fetches data from the database. Please help

Here is my script, controller and model respectively:

<script>
    //we are one
 
    $(document).ready(function() {
        var contactdetails = new Bloodhound({
              datumTokenizer: Bloodhound.tokenizers.obj.whitespace('text'),
              queryTokenizer: Bloodhound.tokenizers.whitespace,
              prefetch: '<?php echo base_url('tags/get_tags');?>'
            });
            contactdetails.initialize();

            var elt = $('input-tag');
            elt.tagsinput({
              itemValue: 'value',
              itemText: 'text',
              typeaheadjs: {    
                name: 'contactdetails',
                displayKey: 'text',
                source: contactdetails.ttAdapter()
              }
            });
            elt.tagsinput('add', { "value": 1 , "text": "Charles"   , "continent": "Europe"    });
            elt.tagsinput('add', { "value": 4 , "text": "David"  , "continent": "America"   });
            elt.tagsinput('add', { "value": 7 , "text": "Peter"      , "continent": "Australia" });
            elt.tagsinput('add', { "value": 10, "text": "Sam"     , "continent": "Asia"      });
            elt.tagsinput('add', { "value": 13, "text": "Ritchie"       , "continent": "Africa"    });
        });

    });

</script>

<?php defined( 'BASEPATH' ) OR die('No direct script access allowed!');

    class tags extends CI_Controller {
        function __construct() {
          parent::__construct();
            $this->load->database(); // load database
            $this->load->model('tags_model');

        }

        function get_tags($number) {
     $res =  $this->tags_model->get_tag($number);
    echo "<pre>";  print_r($res);     }
     }

    }

    /* End of file sms.php */

<?php defined( 'BASEPATH' ) OR die('No direct script access allowed!');

class tags_model extends CI_Model {
    function __construct() {
        // Call the Model constructor
        parent::__construct();
    }

 function get_tag($number){
  $this->db->select("firstname,lastname,number"); 
  $this->db->from('contactdetails', $number);
  $query = $this->db->get();
  return $query->result();
 }

}

/* End of file sms.php */ 

</div>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 关于大棚监测的pcb板设计
    • ¥20 sim800c模块 at指令及平台
    • ¥15 stm32开发clion时遇到的编译问题
    • ¥15 lna设计 源简并电感型共源放大器
    • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
    • ¥15 Vue3地图和异步函数使用
    • ¥15 C++ yoloV5改写遇到的问题
    • ¥20 win11修改中文用户名路径
    • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
    • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计