Codeigniter和AJAX从表中删除项目使页面空白

时间:2017-06-08 03:47:59

标签: php jquery ajax codeigniter

我设法将ajax集成到我的分页和搜索中。到目前为止似乎工作正常。但是,当我删除一个项目并尝试刷新列表时,我的页面只返回空白。我希望有人可以指导我正确的方法,因为我尝试了各种方法,并想尝试使用ajax。谢谢。

我的观点

<script>
function searchFilter(page_num) {
    page_num = page_num?page_num:0;
    var keywords = $('#search').val();
        console.log(keywords);
        console.log('<?php echo site_url('/AdminDocuments/'); ?>'+page_num);
    $.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(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);
            $('.loading').fadeOut("slow");
        }
    });
}
</script>
<div id="successid" style="display: none;" class="alert alert-success">
    <button type = "button" class = "close" data-dismiss = "alert" aria-hidden = "true">
        &times;
    </button>
    Success 
</div>
<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>Account Number</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['account_number']; ?></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>

控制器

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";
        }
    } 

2 个答案:

答案 0 :(得分:1)

deleteDocument函数成功的这一行$(&#39; #list&#39;)。html(result);导致#list div被true或false替换  您正在寻找的是成功再次调用分页脚本

答案 1 :(得分:0)

我认为你犯了一个错误。请对此行// $('#list').html(result);发表评论,因为这会将#list div替换为结果数据

function deleteDocument(input){
    var id = input;
    console.log(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);
            $('.loading').fadeOut("slow");
        }
    });
}

如果它不适合你,请告诉我。