2013-03-13 60 views
1

用Codeigniter和jquery撕裂我的头发。jQuery与Codeigniter的自动完成,不能在索引函数外工作

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

目前我的索引函数调用了我的视图。该视图完全处理jquery自动完成与这种情况。

如果我将索引更改为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> 

autocomplete.js

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

所以如前所述,如果我改变指数()控制器的方法indextest(),并直接浏览到方法的自动完成停止工作。

我是否缺少一些简单的东西或者是否有更大的理由让我无法解决?

感谢一如既往的帮助,

UPDATE AS PER FABIO 从谷歌Chrome浏览器开发的输出上调用自动完成脚本

正常指数()工作) enter image description here

indextext () enter image description here

+0

我第一次拍摄是 “销售/ get_customers” 遗漏的这一请求。检查浏览器是否发送请求并从服务器获取响应。 – Saram 2013-03-13 10:14:57

+0

谢谢Saram,我可以请你引导我检查浏览器发送和响应文件。我确实有firebug和铬的firefox。再次感谢。 – Smudger 2013-03-13 10:32:09

+1

我可以看到你有答案,你的屏幕显示了我的意思 - 查找源错过。 – Saram 2013-03-13 11:54:43

回答

3

在此

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

做这个

$(function(){ 
    $("#customer").autocomplete({ 
    source: "<?=site_url('sales/get_customers')?>" 
    }); 
}); 
+0

是的,你可以尝试一下,如果你没有改变任何东西,base_url会回显,但没有index.php url – 2013-03-13 10:41:33

+1

帮助我们的另一件事是,你使用谷歌浏览器吗?如果是的话打开开发者工具,(只需在你的页面右击并选择inspect元素选项),然后进入标签网络,在底部有一个标签,上面写着XHR,按下它。现在尝试调用自动完成功能,并查看是否有任何请求出现在控制台上 – 2013-03-13 10:44:02

+0

因此,在您的帮助下,找不到路径'indextest?term = a /sales/sales/get_customers'?我不知道为什么销售/销售重复。想法? – Smudger 2013-03-13 11:11:23

1

index控制器内的方法Sales默认情况下会被调用,如果第二个uri参数不存在,那么您实际上已经为您解答了您的问题。

因为你现在使用sales/indextest您需要调整您的source上的网址加载视图,sales/get_customers是一个相对URL,当您加载自动完成,工程没有指定第二个参数indextest为您可能希望解决方案添加一个完整的URL,源例如soruce:"<?=base_url();?>sales/indextest"

您如下可能还需要输出JSON:

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

insted的的:

json_encode($row_set); 
+0

谢谢埃尔达尔,尝试了这两种解决方案,但都没有运气。任何其他想法?谢谢, – Smudger 2013-03-13 10:30:40