javascript - 使用基于 Codeigniter 的 Jquery Java 刷新 html 表

标签 javascript php jquery codeigniter refresh

伙计们,我有一个数据来 self 在表中显示的外包表。原始数据来自 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);
                });

它从 Controller 文件加载一个名为 get_new_data 的函数并加载到

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

                       </div>

Codeigniter Controller 函数是

 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 的新手,所以请放轻松 :)

谢谢大家

最佳答案

要每 15 秒(每分钟 4 次)刷新一次,您应该使用 setInterval 函数代替 setTimeout。

你的代码将是这样的:

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

编辑:但是如果您想确保不让您的页面过载并且按照您的要求首先加载数据然后定期刷新。您必须执行以下操作:

  1. 第一次加载您的数据。
  2. 完成后,调用 setTimeout 以在 25 秒后刷新。
  3. 当您的页面刷新时,调用 SetTimeout 以在 25 秒后刷新它。
  4. 一次又一次。

这比使用 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);
});

关于javascript - 使用基于 Codeigniter 的 Jquery Java 刷新 html 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31238631/

相关文章:

java - 如何在 php web 脚本中获取 java 应用程序的输出?

javascript - 未捕获的类型错误 : Object [object Object] has no method 'val'

javascript - 无法读取未定义的属性 '#<Object>' 出现错误?

javascript - 举升状态不工作

javascript - 函数名后面的空格不对?

php - 如何使用 codeigniter 框架从主键数组中获取查询

jquery - 用 jQuery 除以 css 计算宽度而不是元素宽度属性

javascript - 在对象内完成 ajax 调用后触发 Javascript 回调

javascript - 我可以在数据库中保护此 JavaScript 代码吗?

javascript - JQuery对话框内容打开后清空