codeigniter ajax load table


codeigniter ajax load table

我写了一些编码器代码,当我点击按钮添加数据时,表格将ajax重新加载,而不是重新加载页面。如何修改我的代码?由于

这是我的控制器

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Big_category extends CI_Controller {
  function __construct() {
     ...
  }
  function index() {
    if($this->session->userdata('logged_in')) {
      $this->page(0);
    } else {
      show_404('page');
    }
  }
  function page($page) {
     ....
    $data['pagelist']=$this->pagination->create_links();
    $data["main_content"] = "big_category_view";
    $this->load->view('template', $data);
  }

这是我的模型

class Big_category_model extends CI_Model
{
  ...
  function get_big_category($limit, $start)
  {
    $this->db->limit($limit, $start);
    $query = $this->db->get_where('category1', array('c1_status' => 1)); //SELECT * FROM category1
    return json_encode($query->result());
  }

这是我的观点

...
    <? $data = json_decode($all_big_category); ?>
    <? foreach($data as $key => $value): ?>
    <tr class="modify_tr">
      <td><?=($key+1)?></td>
      <td id="td_id_<?=$value->c1_id?>" class="modify_td">
        <span id="c1_id_<?=$value->c1_id?>"><?=$value->c1_name?></span>
        <form class="form-search td_id_<?=$value->c1_id?>">
          <input type="text" name="input_c1_id" id="input_c1_id_<?=$value->c1_id?>">
          <button class="btn modify" id="button_c1_id_<?=$value->c1_id?>" c1-id="<?=$value->c1_id?>" type="button"><i class="icon-edit"></i></button></td>
        </form>
      <td><button class="btn btn-danger"><i class="icon-remove-sign"></i></button></td>
    </tr>
    <? endforeach ?>
  </table>
  <?=$pagelist?>
...

这里有一个简单的技巧来实现这个目标。

布局(模板)

<html>
<body>
    <div><!--other stuff here--></div>
    <div id = 'form_id'>
    <?php echo $content?>
    </div>
</body>
</html>
控制器

function index() 
{
    if($this->input->is_ajax_request()){
        //load form here
        $this->load->view('form');      
    }else{
        //this will load the form for the first time
        //pass third parameter as TRUE so it will returned as string
        //assigned to index content which will be displayed in layout
        $data['content']    =   $this->load->view('form',$data , TRUE);     
        //load template here
        $this->load->view('template', $data);       
    }
}
JQuery

$.ajax({
    type : 'POST',
    url : '<?php echo site_url('controllername/index')?>',
    data : '' // query string
    success : function(formagain){
        $('#form_id').html(formagain);
        //this will replace the content of div with new form
    } 
});