【问题标题】:Update table content every 10 seconds每 10 秒更新一次表格内容
【发布时间】:2013-07-09 11:36:59
【问题描述】:

我的actions.class.php中有这个功能:

public function executeIndex(sfWebRequest $request) {
    $id_empresa = $this->getUser()->getGuardUser()->getSfGuardUserProfile()->getIdempresa();

    $this->records = Doctrine_Core::getTable('SdrivingRegistrosEmisores')
            ->createQuery('re')
            ->leftJoin('re.SdrivingTurno t')
            ->leftJoin('re.SdrivingMaquinaEmisor me')
            ->leftJoin('me.SdrivingMaquina m')
            ->leftJoin('re.SdrivingOperador o')
            ->leftJoin('re.SdrivingDetalleEmisores de')
            ->where('o.idempresa = ?', $id_empresa)
            ->execute();
}

渲染这个视图:

<table class="table table-condensed table-striped table-hover table-bordered pull-left" id="data-table">    
            <thead>
                <tr>
                    <th><?php echo __('Turno') ?></th>
                    <th><?php echo __('ID Máquina') ?></th>
                    <th><?php echo __('Operador') ?></th>
                    <th><?php echo __('Semáforo') ?></th>
                    <th>&nbsp;</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($records as $record): ?>
                    <tr>
                        <td><?php echo $record->SdrivingTurno->getTipo(); ?></td>
                        <td><?php echo $record->SdrivingMaquinaEmisor->SdrivingMaquina->getPatente(); ?></td>
                        <td><?php echo $record->SdrivingOperador->getNombre() ?></td>
                        <td>
                            <?php
                            if (count($record->SdrivingDetalleEmisores) > 0):
                                if ($record->SdrivingDetalleEmisores->getFirst()->getRevisado() == 0):
                                    echo image_tag('bullet_red.png');
                                elseif ($record->SdrivingDetalleEmisores->getFirst()->getRevisado() == 1):
                                    echo image_tag('bullet_orange.png');
                                endif;
                            else:
                                echo image_tag('bullet_green.png');
                            endif;
                            ?>
                        </td>
                        <td><?php echo link_to('Ver detalles', 'ver-detalles/' . $record->getIdregistros(), 'class="btn btn-success btn-mini"') ?></td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
</table>

正如标题所说,我需要每 10 秒重新加载一次表格内容,在 Stackoverflow 和 Google 中挖掘我发现了这个 topic,我可以在其中获取 jQuery 代码,但我的问题是:如何更新表格 HTML 元素?

编辑

阅读推荐后,用户将我留在这里,这是我迄今为止尝试过的,但没有成功:

actions.class.php

public function executebuildAjax(sfWebRequest $request) {
        $id_empresa = $this->getUser()->getGuardUser()->getSfGuardUserProfile()->getIdempresa();

        $records = Doctrine_Core::getTable('SdrivingRegistrosEmisores')
                ->createQuery('re')
                ->leftJoin('re.SdrivingTurno t')
                ->leftJoin('re.SdrivingMaquinaEmisor me')
                ->leftJoin('me.SdrivingMaquina m')
                ->leftJoin('re.SdrivingOperador o')
                ->leftJoin('re.SdrivingDetalleEmisores de')
                ->where('o.idempresa = ?', $id_empresa)
                ->execute();

        echo '<table class="table table-condensed table-striped table-hover table-bordered pull-left" id="data-table">';
        echo '<thead>';
        echo '<tr>';
        echo '<th>' . __('Turno') . '</th>';
        echo '<th>' . __('ID Máquina') . '</th>';
        echo '<th>' . __('Operador') . '</th>';
        echo '<th>' . __('Semáforo') . '</th>';
        echo '<th></th>';
        echo '</tr>';
        echo '<tbody>';

        foreach ($records as $record):
            echo '<tr>';
            echo '<td>' . $record->SdrivingTurno->getTipo() . '</td>';
            echo '<td>' . $record->SdrivingMaquinaEmisor->SdrivingMaquina->getPatente() . '</td>';
            echo '<td>' . $record->SdrivingOperador->getNombre() . '</td>';
            echo '<td>';
            if (count($record->SdrivingDetalleEmisores) > 0):
                if ($record->SdrivingDetalleEmisores->getFirst()->getRevisado() == 0):
                    echo image_tag('bullet_red.png');
                elseif ($record->SdrivingDetalleEmisores->getFirst()->getRevisado() == 1):
                    echo image_tag('bullet_orange.png');
                endif;
            else:
                echo image_tag('bullet_green.png');
            endif;
            echo '</td>';
            echo '<td>' . link_to('Ver detalles', 'ver-detalles/' . $record->getIdregistros(), 'class="btn btn-success btn-mini"') . '</th>';
            echo '</tr>';
        endforeach;

        echo '</tbody>';
        echo '</thead>';
        echo '</table>';
    }

indexSuccess.php查看:

<div id="dt_example" class="example_alt_pagination"></div>
<script>
    $(document).ready(function() {
        setInterval(function() {
            $.ajax({
                type: 'get',
                url: '<?php echo url_for('dashboard/buildAjax') ?>',
                datatype: 'html',
                success: function(data) {
                    $("#dt_example").html(data);
                },
                error: function() {
                    alert('<?php echo __('Error loading data!!!') ?>');
                }
            });
        }, 10000);
    });
</script>

但我总是收到警告框加载数据时出错!!!为什么?怎么了?

【问题讨论】:

  • 通常情况下,您应该使用 ajax

标签: jquery html symfony1 symfony-1.4


【解决方案1】:

我建议您使用 ajax 和 setInterval 函数从服务器请求表格并替换客户端的 HTML。

这个答案非常接近你所需要的:jQuery - Call ajax every 10 seconds

【讨论】:

  • 你说得对 AJAX 是解决方案,但我的问题更倾向于如何更新表格的每一行,这意味着 table 的每个 td 元素
  • 您可以返回一个行数组而不是整个表并循环遍历它并替换每个td,但鉴于您当前的函数写出整个表,为什么不直接替换它一次?循环将工作更多但效率更低。
  • 我有一个疑问,如果我使用你的方法来构建整个表格元素而不是循环槽每个td(如果我理解得很好)我的意思是当我展示时的第一个电话页面第一次?我的想法是使用函数executeIndex() 显示第一个表,然后通过 jQuery 和 AJAX 每 10 秒调用一次,并使用数据库中的新记录更新表(如果存在),我错了吗?
  • 在您的登录页面 (index.php) 上放置空表和代码以发出 ajax 请求。您将现有函数放在不同的页面 (table.php) 并针对该 URL 发出 ajax 请求。
  • 是的,我明白了,但是如果我将index.php 留空并只对jQuery 和AJAX 部分进行编码,那么第一次调用会发生什么?表格会不会被渲染?
【解决方案2】:

你应该在你的行动中返回一些东西。 例如,用你的表做部分(_table.php) 在 indexSuccess 你应该有

<div id="table">
<?php include_partial('module/table', array('records' => $records))?>
</div>

在 ajax 操作中,您应该从数据库中获取数据并返回此部分

$records = Query:...;
$html = $this->getPartial('module/table', array('records' => $records));
return $this->renderText($html);

或者更好的使用json

return $this->renderText(json_encode(array('html' => $html)))

然后使用 ajax 成功函数将结果插入到 div 中:

success: function(data){
$('#table').html(data.html)
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-12-29
    • 1970-01-01
    • 2021-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-25
    • 2020-08-14
    相关资源
    最近更新 更多