【问题标题】:Autocomplete Multiple Values in Codeigniter在 Codeigniter 中自动完成多个值
【发布时间】:2017-07-10 16:42:47
【问题描述】:

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

没有工作,availableTags 只读取地址 url 在控制器中没有功能 有什么问题请各位大侠帮忙,谢谢

【问题讨论】:

  • 添加代码不是图片!!
  • 你想要什么样的样本输出?
  • 尝试回显 $user = $_GET['term'];在控制器中
  • @AbdullaNilam 代码创建

标签: javascript php jquery json codeigniter


【解决方案1】:

抱歉回复晚了,我的工作很忙 这是对控制器的 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();
    }
}
?>

有什么问题评论我今天上线

【讨论】:

    【解决方案2】:

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

    例如。 姓名, 地址, 血组。

    带有自动完成调用的输出

    【讨论】:

    • 我想从 kota 表中显示 nama_kota
    • 只有姓名或其他字段
    • 我想在表 kota 的字段 nama_kota 中显示记录
    • 请检查这个网址 项目名称:管理员控制器:kota 功能:搜索
    • 如果 admin 是项目名称,则从您已经在基本 url 中提供给他的代码中删除
    猜你喜欢
    • 2012-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-27
    • 1970-01-01
    • 1970-01-01
    • 2018-10-09
    相关资源
    最近更新 更多