dongshen6060 2017-06-22 19: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-22 19: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);
    
            }
    

    展开全部

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部