使用在Codeigniter上构建的Jquery Java刷新html表

时间:2015-07-06 05:59:35

标签: javascript php jquery codeigniter refresh

伙计我有一个来自外包表的数据,我在表中列出。原始数据来自Json,我正在使用PHP函数进行解码并显示。

我一直在谷歌搜索自动刷新该数据并找到了这个 Auto refresh table without refreshing page PHP MySQL

我已经设法构建了Ticked答案中的建议,并且似乎第一次工作,但它没有刷新。所以当页面加载时

我在说这个

$(document).ready (function () {
                    var updater = setTimeout (function () {
                        $('div#user_table').load ('get_new_data', 'update=true');
                        /*  location.reload(); */
                    }, 1000);
                });

从控制器文件加载名为get_new_data的函数并加载到

 <div class="container-fluid" id="user_table">

                       </div>

Codeigniter控制器功能

 public function get_new_data(){

    $url = 'https://gocleanlaundry.herokuapp.com/api/users/';
    $result = $this->scripts->get_data_api($url);
    $data_row = $result;


                            $tbl    = '';
                            $tbl .= '<table class="table table-striped table-responsive" id="tableSortableRes">
                               <thead>
                                   <tr>
                                       <th>Name</th>
                                       <th>Email Address</th>
                                       <th>Role</th>
                                       <th>Status</th>
                                       <th>Action</th>
                                   </tr>
                               </thead>
                               <tbody>';

                                foreach($data_row as $row){
                                $tbl .= '<tr class="gradeX">
                                       <td>' . $row->name . '</td>
                                       <td>' . $row->email . '</td>
                                       <td>' . $row->role . '</td>
                                       <td><p id="' . $row->_id. '_status">'.( $row->alive == true ?  'Active' : 'Banned').' </p></td>
                                       <input id="' . $row->_id . 'status_val" value="' . $row->alive . '" type="hidden">
                                       <td class="center">

                                      <a href="#" id="' . $row->_id . '_rec" onclick="UpdateStatus(' . $row->_id . ')" class="btn btn-mini ">'
                                       . ($row->alive == '1' ? '<i class="fa fa-thumbs-o-up fa-fw"></i>' :  '<i class="fa fa-thumbs-o-down fa-fw"></i>' ) . '</a>
                                       </td>

                                   </tr>
                               ';
                                }
                                $tbl .= '</tbody>
                           </table>';

                                echo $tbl;

}

正如我所说,当页面加载显示数据时,某些东西正在运行,但是当我向数据库添加新数据时,不会自动显示。我想要函数来调用和刷新数据。对不起,我是javascript和jQuery的新手,所以请放轻松我:)

谢谢你们

1 个答案:

答案 0 :(得分:3)

要刷新每15个secondes(每分钟4次),你应该使用setInterval函数代替setTimeout。

您的代码将是这样的:

$(document).ready (function () {
    var updater = setInterval (function () {
        $('div#user_table').load ('get_new_data', 'update=true');
    }, 25000);
});

编辑:但是如果您想确保不会使页面过载,并且如您所知,首先加载数据然后定期刷新。您必须执行以下操作:

  1. 第一次加载您的数据。
  2. 完成后,请在25秒内调用setTimeout刷新。
  3. 刷新页面时,请调用SetTimeout以在25秒内刷新它。
  4. 又一次又一次。
  5. 这比使用setInterval更好。为此,您必须使用jQuery加载函数http://jqapi.com/#p=load的完整回调。

    function refresh() {
        setTimeout(function() {
              $('div#user_table').load ('get_new_data', 'update=true', refresh);
        }, 25000);
    }
    
    $(document).ready (function () {
        $('div#user_table').load ('get_new_data', 'update=true', refresh);
    });