CodeIgniter AJAX分页删除问题

时间:2017-06-09 08:44:56

标签: javascript php jquery ajax codeigniter

我已设法在我的代码中实现ajax分页和删除。但是,我只能删除一个。第一次成功。然而,下一次给我一个错误,说明没有这样的文件或目录要取消链接。我怀疑它与我的ajax分页的工作方式有关,但我不确定如何修复它。 我的代码的工作方式是当我点击分页链接时,会加载ajax_pagination.php。 javascript仅包含在index.php视图中。不幸的是,将脚本添加到两个页面都不会起作用。

这是我的控制器

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());
//        print_r($totalRec);
//        print_r($data['datatable']);
        $this->input->is_ajax_request()?'':$this->load->view('layout/admins/common/header');
        $this->input->is_ajax_request()?'':$this->load->view('layout/admins/common/navigation');
        $this->input->is_ajax_request()?'':$this->load->view('layout/admins/common/title');
        $this->input->is_ajax_request()?'':$this->load->view('layout/admins/common/errors');
        $this->input->is_ajax_request()?'':$this->load->view('layout/admins/common/search');
        $this->load->view('admins/documents/index',$data);
        $this->input->is_ajax_request()?'':$this->load->view('layout/admins/common/footer');
    }

    public function search(){
        $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);
        $this->load->view('admins/documents/ajax_pagination', $data, false);
    }



public function delete(){
        $id = $this->input->post('id');
        unlink($this->DocumentModel->admin_get($id)['file_location']);
        $result = $this->DocumentModel->delete($id);
        $activity = array(
            'page'=>'Documents',
            'action'=>'Deleted document',
            'created_on'=>date('Y-m-d'),
            'time_on'=>date('h:i:s')
        );
        $this->ActivitiesModel->admin_create($activity);
        if($result===false){
            echo "false";
        }else{
            echo "true";
        }
    } 

我的观点

<script>
function searchFilter(page_num) {
    page_num = page_num?page_num:0;
    var keywords = $('#search').val();
    $.ajax({
        type: 'POST',
        url: '<?php echo site_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 deleteDocument(input){
    var id = input;
    console.log("javascript id "+id);
    $.ajax({
        type:'POST',
        url:'<?php echo site_url('/AdminDocuments/Delete/'); ?>',
        data:'id='+id,
        beforeSend: function () {
            $('.loading').show();
        },
        success:function(result){
            console.log(result);
            // $('#list').html(result);
            searchFilter();
            $('.loading').fadeOut("slow");
        }
    });
}
</script>
<div id="maincontainer">
    <div id="list" class="row">
        <div class="col-sm-12 table-responsive">
            <table class="table">
                <thead>
                    <tr class="text-left">
                        <td>Period</td>
                        <td>Name</td>
                        <td>Policy Number</td>
                        <td>Agent ID</td>
                        <td>Client ID</td>
                        <td>Policy Type</td>
                        <td>Transaction ID</td>
                        <td>File Name</td>
                        <td></td>
                        <td></td>
                    </tr>
                </thead>
                <tbody>
                    <?php 
                        if(!empty($datatable)){
                            foreach ($datatable as $data){
                    ?>
                        <tr class="text-left">
                            <td><?php echo $data['month']."/"; ?><?php echo $data['year']; ?></td>
                            <td><?php echo $data['first_name']; ?> <?php echo $data['last_name']; ?></td>
                            <td><?php echo $data['policy_number']; ?></td>
                            <td><?php echo $data['agent_id']; ?> </td>
                            <td><?php echo $data['client_id']; ?> </td>
                            <td><?php echo $data['policy_type']; ?></td>
                            <td><?php echo $data['transaction_id']; ?></td>
                            <td><?php echo $data['file_name']; ?></td>
                            <td><a href="<?php echo $data['file_link'];?>"><i class="fa fa-file" aria-hidden="true"></i> View</a></td>
                            <!--<td><a href="<?php echo site_url('AdminDocuments/Delete/'.$data['document_id']);?>"><i class="fa fa-trash" aria-hidden="true"></i> Delete</a></td>-->
                            <td><a href="#" id="delete" onclick="deleteDocument(<?php echo $data['document_id'];?>)"><i class="fa fa-trash" aria-hidden="true"></i> Delete</a></td>
                        </tr>
                    <?php 
                        }
                    }else{
                    ?>
                        <tr>
                            <td colspan="7">You do not have any items at the moment</td>
                        </tr>
                    <?php
                    }
                    ?>
                </tbody>
            </table>
        </div>
        <div class="pagination-links pull-right">
            <?php echo $links; ?>
        </div>
    </div>
    <div class="loading" style="display: none;">
        <div class="content">
            <img src="<?php echo base_url().'assets/images/loading/loading.gif'; ?>"/>
        </div>
    </div>
</div>

Ajax分页页面是选择分页链接时加载的内容

<div id="list" class="row">
    <div class="col-sm-12 table-responsive">
        <table class="table">
            <thead>
                <tr class="text-left">
                    <td>Period</td>
                    <td>Name</td>
                    <td>Policy Number</td>
                    <td>Agent ID</td>
                    <td>Client ID</td>
                    <td>Policy Type</td>
                    <td>Transaction ID</td>
                    <td>File Name</td>
                    <td></td>
                    <td></td>
                </tr>
            </thead>
            <tbody>
                <?php 
                    if(!empty($datatable)){
                        foreach ($datatable as $data){
                ?>
                    <tr class="text-left">
                        <td><?php echo $data['month']."/"; ?><?php echo $data['year']; ?></td>
                        <td><?php echo $data['first_name']; ?> <?php echo $data['last_name']; ?></td>
                        <td><?php echo $data['policy_number']; ?></td>
                        <td><?php echo $data['agent_id']; ?> </td>
                        <td><?php echo $data['client_id']; ?> </td>
                        <td><?php echo $data['policy_type']; ?></td>
                        <td><?php echo $data['transaction_id']; ?></td>
                        <td><?php echo $data['file_name']; ?></td>
                        <td><a href="<?php echo $data['file_link'];?>"><i class="fa fa-file" aria-hidden="true"></i> View</a></td>
                        <td><a href="<?php echo site_url('AdminDocuments/Delete/'.$data['document_id']);?>"><i class="fa fa-trash" aria-hidden="true"></i> Delete</a></td>
                    </tr>
                <?php 
                    }
                }else{
                ?>
                    <tr>
                        <td colspan="7">You do not have any items at the moment</td>
                    </tr>
                <?php
            }
                ?>
            </tbody>
        </table>
    </div>
    <div class="pagination-links pull-right">
        <?php echo $links; ?>
    </div>
</div>

位于库中的分页javascript

<script>
        function getData(page){
//            console.log(page);
            $.ajax({
                method: "POST",
                url: "<?php echo $this->base_url; ?>"+page,
                data: { page: page },
                beforeSend: function(){
                    $('<?php echo $this->loading; ?>').show();
                },
                success: function(data){
                    $('<?php echo $this->loading; ?>').hide();
                    $('<?php echo $this->target; ?>').html(data);
                }
            });
        }
        </script>

我不确定如何从页面本身获取页码。

1 个答案:

答案 0 :(得分:0)

你必须在你的函数中传递page_num:

检查以下代码的评论:

  success:function(result){
                console.log(result);
                // $('#list').html(result);
                searchFilter();     // look at here 
                $('.loading').fadeOut("slow");
            }

  function searchFilter(page_num)   // look at here