1

用 Codeigniter 和 jquery 撕扯我的头发。

我目前有一个非常基本的代码集来解决这个问题。

目前我的索引函数调用我的视图。视图在这种情况下完美地处理了 jquery 自动完成。

如果我将 index 更改为 indextest 或任何其他名称,自动完成将停止工作。几乎就像 jquery 输出被专门传递给页面的默认方法一样,即 index.php。

有没有办法指定必须返回或显示的方法?

我的控制器是:

<?Php

class Sales extends MY_Controller{

  function __construct() {
    //call parent constructor
    parent::__construct();
    $this->load->model('sales_model');
  }

  function index(){
    $this->load->view('sales/new_order_details');
  }

  function get_customers(){
    $this->load->model('Sales_model');
    if (isset($_GET['term'])){
      $q = strtolower($_GET['term']);
      $this->Sales_model->get_customer($q);
    }
  }

  function login() {
    redirect('/pointer/login');
  }

  function logout() {
    redirect('/pointer/logout');
  }
}

我的模特

<?php
// (Array of Strings)
class Sales_model extends MY_Model{

  function get_customer($q){
    $this->db->select('CustomerName');
    $this->db->like('CustomerName', $q);
    $query = $this->db->get('Customers');
    if($query->num_rows > 0){
      foreach ($query->result_array() as $row){
        $row_set[] = htmlentities(stripslashes($row['CustomerName'])); //build an array
      }
      echo json_encode($row_set); //format the array into json data
    }
  }
}

我的观点

<html>
   <head>
      <title>
         Capture New Order
      </title>

      <link href="<?php echo base_url() ?>application/css/ui-lightness/jquery-ui-1.8.custom.css" media="screen" type="text/stylesheet" rel="stylesheet">
      <script src="<?php echo base_url() ?>application/scripts/jquery-1.9.1.min.js" type="text/javascript"></script>
      <script src="<?php echo base_url() ?>application/scripts/jquery-ui-1.10.1.custom.min.js" type="text/javascript"></script>
      <script src="<?php echo base_url() ?>application/scripts/autocomplete.js" type="text/javascript"></script> 
   </head>
<body>
   <div  data-role="page"  data-theme="a">
      <div class="wrap-header">
         <div data-role="header" data-mini="true"  data-ajax="false">
            <a data-icon="grid" data-mini="true"  data-theme="a" onclick="window.location.href='/pointer'">Menu</a>
            <h3>New Order Details</h3>
         </div>
      </div>   
   <div data-role="content">
   <form>
      <label for="customer">Customer</label>
      <input type="text" id="customer" />
   </form>
   </div>

</body>
</html>

自动完成.js

$(function(){
  $("#customer").autocomplete({
    source: "sales/get_customers"
  });
});

如前所述,如果我将控制器的 index() 方法更改为 indextest() 并直接浏览到该方法,则自动完成功能将停止工作。

我错过了一些简单的事情还是有更大的原因我不能锻炼?

一如既往地感谢您的帮助,

根据FABIO 更新 google chrome 开发人员在调用自动​​完成脚本时的输出

正常索引()工作) 在此处输入图像描述

索引文本() 在此处输入图像描述

4

2 回答 2

3

在这

    $(function(){
  $("#customer").autocomplete({
    source: "sales/get_customers"
  });
});

做这个

$(function(){
  $("#customer").autocomplete({
    source: "<?=site_url('sales/get_customers')?>"
  });
});
于 2013-03-13T10:19:07.743 回答
1

如果不存在第二个 uri 参数,则默认调用index控制器中的方法,您实际上已经回答了自己。Sales

因为您现在使用sales/indextest需要调整sourceurl 的 url 加载视图,sales/get_customers所以是一个相对 url,当您在不指定第二个参数的情况下加载自动完成时有效indextest,至于解决方案,您可能希望将完整 url 添加到源例子soruce:"<?=base_url();?>sales/indextest"

您可能还想按以下方式输出 json:

$this->output->set_content_type('application/json')->set_output(json_encode($data));

安装:

json_encode($row_set);
于 2013-03-13T10:16:35.840 回答