2017-02-21 53 views
1

我有在Codeigninter和jQuery 自动完成的问题,我有一个控制器在笨自动完成多值

<?php 
public function search() { 
    $user = $_GET['term']; 
    $query = $this 
      ->db 
      ->select('nama_kota') 
      ->like('nama_kota', $user) 
      ->get('kota'); 
    if ($query->num_rows() > 0) { 
     foreach ($query->result_array() as $row) { 
      $row_set[] = htmlentities(stripslashes($row['nama_kota'])); 
     } 
     echo json_encode($row_set); 
    } 
} 
?> 

我有一个观点

<script> 
    $(function() { 
     var availableTags = "<?php echo base_url('admin/kota/search'); ?>"; 
     $("#user-input").autocomplete({ 
      source: availableTags 
     }); 
    }); 
</script> 
<input id="user-input" type="text" name="nama_kota" placeholder="Search User" autocomplete="on"> 

一切的好 但我想多值

<script> 
    $(function() { 
     var availableTags = "<?php echo base_url('admin/kota/search'); ?>"; 
     function split(val) { 
      return val.split(/,\s*/); 
     } 
     function extractLast(term) { 
      return split(term).pop(); 
     } 
     $("#user-input").autocomplete({ 
      minLength: 0, 
      source: function (request, response) { 
       // delegate back to autocomplete, but extract the last term 
       response($.ui.autocomplete.filter(
         availableTags, extractLast(request.term))); 
      }, 
      focus: function() { 
       // prevent value inserted on focus 
       return false; 
      }, 
      select: function (event, ui) { 
       var terms = split(this.value); 
       // remove the current input 
       terms.pop(); 
       // add the selected item 
       terms.push(ui.item.value); 
       // add placeholder to get the comma-and-space at the end 
       terms.push(""); 
       this.value = terms.join(", "); 
       return false; 
      } 
     }); 
    }); 
</script> 
<input id="user-input" type="text" name="nama_kota" placeholder="Search User" autocomplete="on"> 

没有工作和效用ableTags只读ADDRES URL在控制器 没有函数什么是错的家伙请帮助我,谢谢

+0

添加代码不是图像! –

+0

你想要什么样的输出? –

+0

尝试echo $ user = $ _GET ['term'];在控制器 – Shibon

回答

0

如果你需要这样的

例如哪种类型关闭你需要输出。 名称, 地址, blood_group。

与自动完成呼叫的输出

+0

我想从哥打表 –

+0

唯一的名字或其他领域 –

+0

我希望显示表哥打现场nama_kota记录 –

0

对不起,迟到的回复我很对我的工作 很忙,这是JS调用控制器

<script> 

    jQuery("#h_student_name").autocomplete({ 
     minLength: 0, 
     source: "DropdownController/hostel_students/" + $("#h_student_name").val(), 
     autoFocus: true, 
     scroll: true, 
     dataType: 'jsonp', 
     select: function (event, ui) { 
      jQuery("#h_student_name").val(ui.item.contactPerson); 
      jQuery("#h_student_id").val(ui.item.code); 
     } 
    }).focus(function() { 
     jQuery(this).autocomplete("search", ""); 
    }); 
</script> 

,这是控制呼叫

<?php 
//Hostel Student Auto compelete 
public function hostel_students(){ 


    $term = trim(strip_tags($this->input->get('term'))); 
    if($term == ''){ 
     $like = $term; 
     $result_set = $this->DropdownModel->hostel_students(array('hostel_status_id' => 1)); 
     $labels = array(); 
     foreach ($result_set as $row_set) { 
      $labels[] = array(
       'label' => $row_set->student_name.' S/D '.$row_set->father_name.' ,Form# '.$row_set->form_no.' ', 
       'code' => $row_set->hostel_id, 
       'value' => $row_set->student_name, 
      ); 
     } 
     $matches = array(); 
     foreach($labels as $label){ 
      $label['value'] = $label['value']; 
      $label['code'] = $label['code']; 
      $label['label'] = $label['label']; 

      $matches[] = $label; 
     } 
     $matches = array_slice($matches, 0, 10); 
     echo json_encode($matches); 
    } else if($term != ''){ 
     $like = $term; 
     $result_set = $this->DropdownModel->hostel_students(array('hostel_status_id' => 1), $like); 
     $labels = array(); 
     foreach ($result_set as $row_set) { 
      $labels[] = array(
       'label' => $row_set->student_name.' S/D '.$row_set->father_name.' ,Form# '.$row_set->form_no.' ', 
       'code' => $row_set->hostel_id, 
       'value' => $row_set->student_name, 
      ); 
     } 
     $matches = array(); 
     foreach($labels as $label){ 
      $label['value'] = $label['value']; 
      $label['code'] = $label['code']; 
      $label['label'] = $label['label']; 

      $matches[] = $label; 
     } 
     $matches = array_slice($matches, 0, 10); 
     echo json_encode($matches); 
    } 
} 
?> 

这是呼叫模型

<?php 
// Hostel student autocomplete  
public function hostel_students($where, $like = NULL){ 
    if($like): 
     $this->db->like('student_name', $like); 
     $this->db->or_like('form_no', $like); 
     $this->db->or_like('college_no', $like); 
    endif; 
     $this->db->join('student_record', 'student_record.student_id=hostel_student_record.student_id'); 
     return $this->db->where($where)->get('hostel_student_record')->result(); 
    } 
} 
?> 

有任何问题评论我我今天在线