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);
    
            }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 C++扑克牌游戏的编程
  • ¥15 有人能看一下我宿舍管理系统的报修功能该怎么改啊?链表那里总是越界
  • ¥15 cs loadimage运行不了,easyx也下了,没有用
  • ¥15 r包runway详细安装教程
  • ¥15 Html中读取Json文件中数据并制作表格
  • ¥15 谁有RH342练习环境
  • ¥15 STM32F407 DMA中断问题
  • ¥15 uniapp连接阿里云无法发布消息和订阅
  • ¥25 麦当劳点餐系统代码纠错
  • ¥15 轮班监督委员会问题。