0

我写了一些codeigniter代码,当我单击按钮添加数据时,表格将重新加载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?>
...
4

1 回答 1

3

这是一个简单的技术,您可以如何实现这一点。

布局(模板)

<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
    } 
});
于 2013-03-15T06:46:58.273 回答