0

我在这里只需要一点帮助来显示我的表查询结果。当我检查来自我的 ajax 的响应时,我显示。但是当传递给视图时,它不会显示。这是我的代码:

控制器/user.php

        <?php

        class User extends CI_Controller{

            public function __construct(){
                parent::__construct();
                $this->load->model('user_model');
            }

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

            public function read() {
                echo json_encode( $this->user_model->getAll() );
            }


        }

    ?>

模型/user_model.php

        <?php

        class User_Model extends CI_Model{

            public function __construct(){
                parent::__construct();
            }

            public function getAll(){

                $qGetAllData = $this->db->get('user');
                if($qGetAllData->num_rows() > 0){
                    return $qGetAllData->result();
                }else{
                    return array();
                }

            }

        }

    ?>

    View/index.php

        <html>
      <head>
        <title><?php echo $title; ?></title>
        <base href="<?php echo base_url(); ?>" />
        <link type="text/css" rel="stylesheet" href="css/smoothness/jquery-ui-1.8.2.custom.css" />
        <link type="text/css" rel="stylesheet" href="css/styles.css" />
      </head>

      <body>

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

        <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"></div>

        </div>




        <script type="text/javascript" src="js/jquery-1.4.2.min.js"></script>
        <script type="text/javascript" src="js/jquery-ui-1.8.2.min.js"></script>
        <script type="text/javascript" src="js/jquery-templ.js"></script>

        <script type="text/template" id="readTemplate">
          <tr>
            <td>${id}</td>                  //doesn't display ID
            <td>${name}</td>                //doesn't display Name
            <td>${email}</td>               //doesn't display EMial
          </tr>
        </script>

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

      </body>

    </html>

Javascript/myjs.js

            $(document).ready(function(){

        $( '#tabs' ).tabs({
                fx: { height: 'toggle', opacity: 'toggle' }
            });

            $.ajax({
                url: 'index.php/user/read',
                datatype: 'json',
                success: function(response){
                    $('#readTemplate').render(response).appendTo('#records');
                }
            });

        });

这就是所有的家伙。我只是不知道我的错误在哪里。

4

1 回答 1

1

我认为您对仅渲染数据有疑问。

请查看此链接以供参考。

请尝试以下代码。

Your html : View/index.php
<html>
  <head>
    <title><?php echo $title; ?></title>
    <base href="<?php echo base_url(); ?>" />
    <link type="text/css" rel="stylesheet" href="css/smoothness/jquery-ui-1.8.2.custom.css" />
    <link type="text/css" rel="stylesheet" href="css/styles.css" />
  </head>
  <body>
    <table id="records"></table>
    <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"></div>
    </div>
    <script type="text/javascript" src="js/jquery-1.4.2.min.js"></script>
    <script type="text/javascript" src="js/jquery-ui-1.8.2.min.js"></script>
    <script type="text/javascript" src="js/jquery-templ.js"></script>
    <script type="text/javascript" src="js/myjs.js"></script>
  </body>
</html>

Your JS : js/myjs.js
var readtpl = "<tr><td>${id}</td><td>${name}</td><td>${email}</td></tr>";
$.template( "readTemplate", readtpl );
 $(document).ready(function(){

    $( '#tabs' ).tabs({
            fx: { height: 'toggle', opacity: 'toggle' }
        });

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

    });
于 2013-09-23T07:44:55.820 回答