在Codeigniter中自动完成多个值

时间:2017-02-21 07:15:17

标签: javascript php jquery json codeigniter

我在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">

没有工作和可用标签只读取控制器中的addres url no函数 什么错误的人请帮助我,谢谢

2 个答案:

答案 0 :(得分:0)

您需要输出哪种类型输出如果您需要这样

例如。 名称, 地址, blood_group。

输出自动填充功能

答案 1 :(得分:0)

抱歉,对于迟到的回复,我的工作非常繁忙 这是JS调用Controller

<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();
    }
}
?>

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

相关问题