dongshen6060 2017-06-23 03:26
浏览 47
已采纳

CodeIgniter ajax重装页面问题

I have a page with ajax pagination on it, I am currently able to check if the session exists and process accordingly. However, I cannot seem to remove the menu or reload the page properly if the session has expired. Only the menu remains and the login page shows in the small area where the table was. I hope someone can help.

Controller code

public function index()
    {
        $conditions = array();
        $data = array();
        $totalRec = count($this->DocumentModel->admin_get_and_search($conditions));
        $config['target']      = '#list';
        $config['base_url']    = site_url('/AdminDocuments/Search');
        $config['total_rows']  = $totalRec;
        $config['per_page']    = $this->get_per_page();
        $this->ajax_pagination->initialize($config);
        $data['links'] = $this->ajax_pagination->create_links();
        $data['datatable'] = $this->DocumentModel->admin_get_and_search(array('limit'=>$this->get_per_page()));
        $data['user'] = $this->AccountModel->get_person($this->get_person_id());
        $data['current_page'] = $this->ajax_pagination->getCurrPage();
        $this->load->view('layout/admins/common/header');
        $this->load->view('layout/admins/common/navigation');
        $this->load->view('layout/admins/common/title');
        $this->load->view('layout/admins/common/errors');
        $this->load->view('layout/admins/common/search');
        $this->load->view('admins/documents/index',$data);
        $this->load->view('layout/admins/common/footer'); 
    }

    public function search(){
        if($this->input->is_ajax_request()){
            if(!$this->logged_in()){
                $this->index();
            }else{
                $conditions = array();
                $page = $this->input->post('page');
                if(!$page){
                    $offset = 0;
                }else{
                    $offset = $page;
                }
                $keywords = $this->input->post('keywords');
                if(!empty($keywords)){
                    $conditions['search']['keywords'] = $keywords;
                }
                $totalRec = count($this->DocumentModel->admin_get_and_search($conditions));
                $config['target']      = '#list';
                $config['base_url']    = site_url('/AdminDocuments/Search');
                $config['total_rows']  = $totalRec;
                $config['per_page']    = $this->get_per_page();
                $this->ajax_pagination->initialize($config);
                $conditions['start'] = $offset;
                $conditions['limit'] = $this->get_per_page();
                $data['links'] = $this->ajax_pagination->create_links();
                $data['datatable'] = $this->DocumentModel->admin_get_and_search($conditions);
                $data['current_page'] = $this->ajax_pagination->getCurrPage();
                $this->load->view('admins/documents/ajax_pagination', $data, false);
            }
        }
    }

My JS Code that is placed in the view

<script>

function searchFilter(page_num) {
    page_num = page_num?page_num:0;
    var keywords = $('#search').val();
    $.ajax({
        type: 'POST',
        url: 'url/AdminDocuments/Search/'+page_num,
        data:'page='+page_num+'&keywords='+keywords,
        beforeSend: function () {
            $('.loading').show();
        },
        success: function (html) {
            $('#list').html(html);
            $('.loading').fadeOut("slow");
        }
    });
}

function changeStatus(input){
    var id = input;
    $.ajax({
        type:'POST',
        url:'url/AdminDocuments/ChangeStatus/',
        data:'id='+id,
        beforeSend: function () {
            $('.loading').show();
        },
        success:function(result){
            console.log(result);
            searchFilter(0);
            $('.loading').fadeOut("slow");
        }
    });
}

function deleteDocument(input){
    var id = input;
    $.ajax({
        type:'POST',
        url:'url/AdminDocuments/Delete/',
        data:'id='+id,
        beforeSend: function () {
            $('.loading').show();
        },
        success:function(result){
            searchFilter(0);
            $('.loading').fadeOut("slow");
        }
    });
}
</script>
  • 写回答

1条回答 默认 最新

  • douzen3516 2017-06-23 03:37
    关注

    i am assuming $('#list').html(html); code loads the html in the dom. instead of directly sending the html from php you can send a json containing the html as well the login status. like this.

    $data = [
      'login_status' => 1 // or 0,
      'html' => $html // full html your are sending now
    ];
    
    echo json_encode($data);
    

    then in ajax success.

    function searchFilter(page_num) {
        page_num = page_num?page_num:0;
        var keywords = $('#search').val();
        $.ajax({
            type: 'POST',
            url: 'url/AdminDocuments/Search/'+page_num,
            data:'page='+page_num+'&keywords='+keywords,
            beforeSend: function () {
                $('.loading').show();
            },
            success: function (response) {
                var data = $.parseJSON(response);
    
            if(data.login_status == 0)
            {
              window.location.href = 'redirect to login page';
            }
    
            if(data.login_status == 1)
            {
              $('#list').html(data.html);
            }
                $('.loading').fadeOut("slow");
            }
        });
    }
    

    controller method :

    public function search(){
            if($this->input->is_ajax_request()){
    
                    $conditions = array();
                    $page = $this->input->post('page');
                    if(!$page){
                        $offset = 0;
                    }else{
                        $offset = $page;
                    }
                    $keywords = $this->input->post('keywords');
                    if(!empty($keywords)){
                        $conditions['search']['keywords'] = $keywords;
                    }
                    $totalRec = count($this->DocumentModel->admin_get_and_search($conditions));
                    $config['target']      = '#list';
                    $config['base_url']    = site_url('/AdminDocuments/Search');
                    $config['total_rows']  = $totalRec;
                    $config['per_page']    = $this->get_per_page();
                    $this->ajax_pagination->initialize($config);
                    $conditions['start'] = $offset;
                    $conditions['limit'] = $this->get_per_page();
                    $data['links'] = $this->ajax_pagination->create_links();
                    $data['datatable'] = $this->DocumentModel->admin_get_and_search($conditions);
                    $data['current_page'] = $this->ajax_pagination->getCurrPage();
                    $html = $this->load->view('admins/documents/ajax_pagination', $data, true);
        $res['html'] = $html;
        $res['login_status'] = ($this->logged_in()) ? '1' : '0';
    
           echo json_encode($res);
    
            }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?