0

我在我的有这个功能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 中挖掘我发现了这个主题,我可以在其中获取 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>

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

4

2 回答 2

3

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

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

于 2013-07-09T11:41:37.837 回答
0

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

<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)
}
于 2013-07-12T09:56:32.047 回答