2011-06-08 61 views
0

我试图按照这里找到教程:http://www.ifadey.com/2010/06/crud-using-jquery-and-codeigniter/我的代码进行更新,以适应对CI和JQ更新。但是当试图从我的数据库加载数据并将其显示在我的视图中时,没有任何反应/显示。指向javascript(JQ)代码的所有链接均可工作,并且所有内容均使用MAMP在本地运行。任何和所有的帮助,不胜感激。新手:笨,jQuery和阿贾克斯 - 没有返回/显示值

我的代码如下,

型号:

<?php 
class mUsers extends CI_Model { 
    public function getAll() { 

     //get all records from users table 
     $this->db->_compile_select(); 
     $query=$this->db->get('users')->result();   
     //OR $query = $this->db->get('users'); 
     echo $this->db->last_query(); 

     if($query->num_rows() > 0) { 
      return $query->result(); 
     } else { 
      return array(); 
     } 

    } //end getAll 

} //end class 
?> 

控制器:

<?php 
class Site extends CI_Controller{ 
    public function index(){ 
     $this->load->model('mUsers');  
     $this->load->view('home'); 
    } 

public function read(){ 
    header('Content-Type: application/json',true); 
    echo json_encode($this->mUsers->getAll()); 

} 

} 

?> 

查看:

<html> 
<head> 
    <title>Test</title> 

     <base href="<?php echo base_url(); ?>" /> 
     <link type="text/css" rel="stylesheet" href="css/styles.css" /> 
     <link type="text/css" rel="stylesheet" href="css/ui-lightness/jquery-ui-1.8.12.custom.css" /> 


</head> 
<body> 


    <div id="tabs"> 

    <ul> 
     <li><a href="#read">Read</a></li> 
     <li><a href="#create">Create</a></li> 
    </ul> 

    <div id="read"> 
     <table id="records"></table> 
    </div> 

    <div id="create">Create form goes here...</div> 

</div> <!-- end tabs --> 

<script type="text/javascript" src="js/jquery-1.6.1.min.js"></script> 
<script type="text/javascript" src="js/jquery-ui-1.8.13.custom.min.js"></script> 
<script type="text/javascript" src="js/jquery.tmpl.js"></script> 

<script type="text/template" id="readTemplate"> 
     <tr> 
      <td>${id}</td> 
      <td>${name}</td> 
      <td>${email}</td> 
     </tr> 
    </script> 


<script type="text/javascript" src="js/all.js"></script> 

</body> 
</html> 

回答

0

终于想通了。 Ajax调用应包含.tmpl,而不是.render:

$.ajax({ 
     url: 'index.php/site/read', 
     dataType: 'json', 
     success: function(response) { 
      $('#readTemplate').tmpl(response).appendTo("#records"); 
0

我以为你不自动加载mUsers模型所以Site-> read()会见了hod不会输出数据库中的任何数据,因为它不知道模型的getAll()方法。

您应该加载模型中每个需要它或者更确切地说,在类的构造方法,因为可能大部分网站控制器的方法将使用它:

class Site extends CI_Controller 
{ 
    function __construct() 
    { 
     parent::__construct(); 
     $this->load->model('mUsers'); 
    } 

    function index() 
    { 
     $this->load->view('home'); 
    } 

    function read() 
    { 
     header('Content-Type: application/json',true); 
     echo json_encode($this->mUsers->getAll()); 
    } 
} 
+0

尝试了您的建议,但仍然没有运气。有没有建议的方式来调试代码?谢谢! – user464180 2011-06-08 19:02:37

+0

如果我编辑我的控制器函数以包含$ this-> load-> model('mUsers');之前的回波json_encode,然后我粘贴直接访问控制器(http://127.0.0.1/CI/index.php/site/read)的URL我最终得到的数据返回为:[{“ID”: “1”, “名”: “哈桑Fawad”, “电子邮件”: “[email protected]”},{ “ID”: “2”, “名”: “比尔·盖茨”, “电子邮件”:“法案@ test.com “},{” ID “:” 3" , “名”: “史蒂夫·乔布斯”, “电子邮件”: “[email protected]”},{ “ID”: “4”, “名” : “函数naveed艾哈迈德”, “电子邮件”: “[email protected]”},{ “ID”: “5”, “名”: “Zee的先生”, “电子邮件”: “[email protected]”}] ,但仍然无法将其加载到显示器中 – user464180 2011-06-08 19:54:10

+0

您的ajax调用获取该数据的位置在哪里?在你的代码中没有这样的JavaScript。 – 2011-06-09 10:02:34

0

上就NETTUTS模板的优秀教程。

试着改变你的模板脚本这个

<script id="readTemplates" type="text/x-jquery-tmpl"> 

林没有真正达到上的模板,但不要你需要定义的$ id $名$电子邮件?这是我的一个模板从twitter获取内容的一个例子。该信息可从萤火虫接收

<script id="tweets" type="text/x-jquery-tmpl"> 
    <li class="display"> 
     <img src="${imgSource}" alt="${userName}" /> 
     <h4> ${username} </h4> 
     <p> ${tweet} </p> 
     <hr /> 

    </li> 
    </script> 

    <ul id="twitters"></ul> 
    <script type="text/javascript"> 
    $.ajax({ 
     type : 'GET', 
     dataType : 'jsonp', 
     url : 'http://search.twitter.com/search.json?q=my id', 

     success : function(tweets) { 

      var twitter = $.map(tweets.results, function(obj, index) { 
       return { 
       username : obj.from_user, 
       tweet : obj.text, 
       imgSource : obj.profile_image_url, 
       geo : obj.geo 
       }; 
      }); 

      $('#tweets').tmpl(twitter).appendTo('#twitters'); 
     } 
    }); 

    </script> 

请注意,我用的返回值是在脚本的底部限定。这也许不能解决你的问题,而是给你一个更好的主意模板是如何工作的