2016-12-12 6 views
0

コントローラのサブミットデータベースのajaxを呼び出した後、このようなテーブルの内容を更新するにはどうすればよいですか?私はcakephpを使用します。 cakephpでデータベース更新後のAjaxリフレッシュ・テーブル?

<table id="example" class="table"> 
<thead> 
    <tr class="headings"> 
     <th>ID &nbsp;&nbsp;&nbsp;</th> 
     <th>first name </th> 
     <th>last name </th> 
     <th>E-mail </th> 
     <th>birthday </th> 
    </tr> 
</thead> 

<tbody> 
    <?php // empty for now ?> 
</tbody> 

私のAJAX:

(function($) { 
    $.fn.ApplyTask = function() { 
     var task_id = $(this).data('task'); 
     var user_id = $(this).data('user'); 

     $.ajax({ 
      url: 'applyTask', 
      cache: false, 
      type: 'POST', 
      data: { 
       task_id: task_id, 
       user_id: user_id 
      }, 
      success: function(data) { 

       } 
      }); 
     }; 
    })($); 

と機能applyTaskが唯一のデータベースの値を更新します。次に、あなたのtbodyにIDやクラスを追加

<?php foreach ($data as $data): ?> 
     <tr> 
       <td><?= h($data->id) ?></td> 
       <td><?= $data->username ?></td> 
     </tr> 
<?php endforeach; ?> 

、たとえば今

<tbody id="fetch-data"> 
    <?php // empty for now ?> 
</tbody> 

:あなたのapplyTask.ctpファイルで

public function applyTask() { 
    if ($this->request->is('ajax') && !empty($this->request->data)) { 
     $task_id = $this->request->data['task_id']; 
     $user_id = $this->request->data['user_id']; 

     if (!$this->TasksUser->findByTaskIdAndUserId($task_id, $user_id)) { 
      throw new NotFoundException(__('Invalid tasks_users.')); 
     } 

     $this->TasksUser->updateAll(array('is_apply' => TRUE), array('task_id' => $task_id, 'user_id' => $user_id)); 
    } else { 
     throw new MethodNotAllowedException('This method is now allowed.'); 
    } 
} 
+0

ajax呼び出しに対する応答を返すPHPコードはどうですか? –

答えて

1

tr

例では、データをフェッチコールバックでは以下のコードでデータを追加するだけです

success: function(data) { 
    $('#fetch-data').html(data); 
} 

注:applyTaskメソッドで$this->viewBuilder()->layout(false);を使用することを忘れないでください。

関連する問題