Page 118 - Framework Codeigniter 2
P. 118

www.koder.web.id – Kumpulan tutorial komputer

               15. {
               16.    $this->load->view('autocomplete/index');
               17. }
               18.
               19. function lookup()
               20. {
               21.    $keyword=$this->input->post('term');
               22.    $data['response']='false';
               23.    $query=$this->kota_model->find($keyword);
               24.    if(! empty($query))
               25.    {
               26.         $data['response']='true';
               27.         $data['message']= array();
               28.         foreach($query as $row)
               29.         {
               30.            $data['message'][]= array(
               31.            'id'=>$row['id_kota'],
               32.            'value'=>$row['nama_kota']
               33.            );
               34.         }
               35.    }
               36.    echo json_encode($data);
               37. }
               38. }
               39. /* End of file autocomplete.php */
               40. /* Location: ./application/controllers/autocomplete.php */

               Fungsi index pada baris ke-14 hanya berisi sebuah  perintah untuk me-load sebuah view yaitu
               view autocomplete/index. Adapun isi view tersebut adalah  :


               1.  <!DOCTYPE HTML>
               2.  <htmllang="en-US">
               3.  <head>
               4.  <title>Codeigniter dan jQuery Autocomplete</title>
               5.  <link rel="stylesheet" href=
               6.  "<?php echo base_url();?>public/jquery.ui.all.css"
               7.  type="text/css" media="all"/>
               8.  <link rel="stylesheet" href="<?php echo base_url();?>public/ui-
                    lightness/jquery-ui-1.8.10.custom.css"
                   type="text/css" media="all"/>
               9.  <script src="<?php echo base_url();?>public/js/jquery-
                    1.4.4.min.js" type="text/javascript"></script>
               10.  <script src="<?php echo base_url();?>public/js/jquery-ui-
                    1.8.10.custom.min.js" type="text/javascript"></script>
               11.
               12.  <script type="text/javascript">
               13.  $(this).ready( function(){
               14.    $("#id_kota").autocomplete({
               15.    minLength:1,
               16.    source:
               17.    function(req, add){
               18.         $.ajax({
               19.         url:"<?php echo base_url();?> "
               20.         +"index.php/autocomplete/lookup",
               21.       dataType:'json',
               22.       type:'POST',
                                                                                                         113
   113   114   115   116   117   118   119   120   121   122   123