2013-01-11 62 views
0

我目前已将我的cakePHP应用程序从1.3迁移到2.2.4。它用树的行为来管理任务。我使用jQuery作为ajax。ajax与cakephp2更新div

当我点击一个任务时,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'); 

    } 

,这是我taskelement,这是beeing请求并填写每个任务:

<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> 
+0

这将是很难帮助没有看到我们的代码 –

+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