在更新的div上使用cakephp2的ajax

时间:2013-01-11 22:54:31

标签: jquery ajax recursion cakephp-2.0

我目前将cakePHP应用程序从1.3迁移到2.2.4。它使用树行为管理任务。我使用jQuery作为ajax。

当我单击任务时,会触发ajax请求,响应(子任务)会更新任务div的内容。这适用于第一级。但是第二级没有生成任何javascript代码,所以点击导致一个空白页面,只显示简单的ajax响应(只有第二级的子任务)。

如何告诉cakePHP生成更多的Javascript代码来更新div?正在生成HTML ID。

任何帮助都会很棒!

在TasksController.php中

函数展开

function expand($id = NULL, $check = 1) {
/*
    get details of given task ID
*/
// if not expanded yet
if ((!$this->Session->check('Task.expand.'.$id))||($check==0)) {
    // set session variable to "expanded"
    $this->Session->write('Task.expand.'.$id, 1);

    // get task details
    $zahlen = array(1 => 'eins', 2 => 'zwei', 3 => 'drei', 4 => 'vier', 5 => 'fünf');
    $this->set('commentlimit', $zahlen[$this->Session->read('Profile.commentcount')]);
    $this->Task->Behaviors->attach('Containable');
    $this->Task->recursive = -1;
    $this->Task->contain(array('Assignee' => array('fields' => array('id', 'name', 'firstname')), 'Tasktype', 'Comment' => array('fields' => 'text', 'limit' => $this->Session->read('Profile.commentcount'), 'order' => 'lft DESC', 'user_id', 'User' => array('fields' => array('name', 'firstname')))));
    $this->set('task', $this->Task->find('first', array('conditions' => array('Task.id' => $id, 'Task.user_id' => $this->Session->read('UserId')))));
}
// if yet expanded, collapse / unload
else {
    $this->Session->delete('Task.expand.'.$id);
    $this->redirect(array('controller' => 'tasks', 'action' => 'collapse'));
}
    //  pr ($this->Session->read('Task'));

    $this->render('expand', 'ajax'); 

    }

这是我的任务,为每项任务提出要求并填写:

<div class="task <?php echo $thisclass; ?>">
    <?php
    echo $this->Js->link($task['Task']['name'], array( 'controller' => 'tasks', 'action' => 'expand', $task['Task']['id'] ), array( 'update' => '#details'.$task['Task']['id'] ));
    echo " <span>(".$this->requestAction('/tasks/countAllChildren/'.$task['Task']['id'])." Unterpunkte, davon ";
    echo $this->requestAction('/tasks/countDirectChildren/'.$task['Task']['id'])." direkt)</span>";
    echo " <span>(".$this->requestAction('/tasks/countAllDoneChildren/'.$task['Task']['id'].'/'.$task['Task']['lft'].'/'.$task['Task']['rght'])." erledigt)</span>";
    ?>
<div id="details<?php echo $task['Task']['id']; ?>">
    <?php
        echo $this->requestAction('/tasks/is_expanded/'.$task['Task']['id']);
    ?>
</div>
    </div>

expand.ctp的相关部分如下所示:

    <?php
    $children = $this->requestAction('/tasks/getChildren/'.$task['Task']['id']);
    if ($children) {
    ?>
<p><h4>Unteraufgaben:</h4>
<?php
echo $this->Html->link('Neu',"/tasks/add/".$task['Task']['id'], array('class'=>'button'));
foreach ($children as $child) {
    $this->set('task', $child);
// todo: das hier eleganter machen, ohne zusätzliche Abfragen
    $this->set('level', $this->requestAction('/tasks/getDepth/'.$task['Task']['id']));
    echo "<tr>".$this->element('taskmesh')."</tr>";
}
    }
    else {
?>
<p><h4>Keine Unteraufgaben</h4>
<?php
echo $this->Html->link('Neu',"/tasks/add/".$task['Task']['id'], array('class'=>'button'));
    }
?>
</p>

1 个答案:

答案 0 :(得分:0)

当我问另一个CakePHP人时,他告诉我使用JavaScript缓冲区并在CakeBook中链接到它:http://book.cakephp.org/2.0/en/core-libraries/helpers/js.html

所以我的问题的答案是:

将以下代码添加到请求的ajax操作视图的底部:

echo $this->Js->writeBuffer(); // Write cached scripts