Code Igniter Pagination和JQuery动作

时间:2011-03-09 05:13:14

标签: php jquery codeigniter

我正在创建一个图书管理网络应用程序,当我使用分页时,我在删除图书时遇到问题。

在我的控制器中,我有这段代码:

<?php
class Books extends CI_Controller {

    function __construct()
    {
        parent::__construct();
                #$this->load->model('books_model');
                $this->load->helper('path');
    }

        function index() {

            $config['base_url'] = base_url().'books/index';
            $config['total_rows'] = $this->db->count_all('tbl_books');
            $config['per_page'] = 2;

            $this->pagination->initialize($config);

            $data['books'] = $this->db->get('tbl_books',$config['per_page'], $this->uri->segment(3));
            $data['pagination'] = $this->pagination->create_links();
            $data['page'] = 'Books';
            $this->load->view('books_view', $data);
        }

}
?>

在我的books_view中我有这段代码:

        <p class="pagination ta-right">
    <?=$pagination?>
    </p>
    <?php foreach($books->result() as $row): ?>
            <a href="<?=base_url()?><?=$row->image?>" class="nyroModal"> <img src="<?=base_url()?><?=$row->image?>" alt="" /></a>
            <a href="#<?=$row->id?>" title="<?=$row->title?>" class="nyroModal"><b class="big"><?=$row->title?></b></a> &middot; 
    <?php
      $sql = $this->db->query("select * from tbl_transactions where bookid=".$row->id." and (type='reserve' or type='borrowed')");
      $book_info = $sql->result();
            if($sql->num_rows() > 0) {
                    if($book_info[0]->type == 'reserve') { ?>
                           <span class="label label-red">Reserved</span>
                      <?php }elseif($book_info[0]->type == 'borrowed') { ?>
                            <span class="label label-blue">Borrowed</span>
                      <?php } else { ?>
                            <span class="label label-green">Available</span>
                      <?php }
                      } else { ?>
                            <span class="label label-green">Available</span>
                      <?php } ?>
                            Author: <b><?=$row->authors?></b> |  Category: </small><br/>
                      <div id="action-<?=$row->id?>">
                            <a href="" id="remove-<?=$row->id?>">remove</a> &middot; <a href="#">edit</a>
                      </div>
                            <img src="<?=base_url()?>img/small-loader.gif" style="display:none;" id="ajax-load-<?=$row->id?>" />
                 <div id="<?=$row->id?>" style='display: none;'><h3><?=$row->title?></h3><p align="justify"><?=$row->description?></p></div>
                     <?php endforeach; } ?>

<script type="text/javascript">
$(document).ready(function() {
<?php foreach($books->result() as $row): ?>
    $('#remove-<?=$row->id?>').click(function() {
        var stats = confirm('Are you sure you want to delete this entry?');

        if(stats) {

                        $('#action-<?=$row->id?>').hide();
                        $('#ajax-load-<?=$row->id?>').show();

                        $.ajax({
                         type: 'POST',
                         url: "<?=base_url()?>bookacts/delbook/",
                         data: ({id: <?=$row->id?>}),
                         cache: false,
                         success: function (msg){

                           if(msg == ""){
                                $('#ajax-load-<?=$row->id?>').hide();
                                $('.box-error').fadeIn("slow");
                                $('#action-<?=$row->id?>').fadeIn();
                            } else {
                                $('#ajax-load-<?=$row->id?>').hide();
                                $('.box-success').fadeIn("slow")
                                                 .animate({opacity: 1.0}, 2000)
                                                 .fadeOut('slow');
                                $('#action-<?=$row->id?>').fadeIn();
                            }
                         }
                        }); return false;
        } else {
            return false;
        }

    });
<?php endforeach; ?>
});
</script>

我的问题是代码只会在我第2页开始时删除该条目。但是当我在第1页时,脚本就不起作用了。

1 个答案:

答案 0 :(得分:1)

我没有马上看到问题。但是在我看来你使用CodeIgniter和Jquery都是错误的,所以我会给你一些提示。

首先,您可能知道Codeigniter使用MVC模式。并且视图并不真正意味着SQL。更好的是,大多数时候Controller甚至不用于SQL。您应该尝试将SQL保留在模型中。

另一个小的Codeigniter事。我觉得使用起来更好:

function index($page=0) {

然后:

$data['books'] = $this->db->get('tbl_books',$config['per_page'], $page);

而不是使用$ this-&gt; uri-&gt; segment(3)

其次你也应该使用Jquery'dynamic'。您现在正在为每本书制作一个功能,而您总是希望执行相同的操作。

添加课程:

<a href="" id="remove-<?=$row->id?>" class="book-remove">remove</a>

然后作为JS:

<script type="text/javascript">
    $(document).ready(function() {
        $('.book-remove').each(function() {
            $(this).click(function() {

            var stats = confirm('Are you sure you want to delete this entry?');

            if(stats) {
                var bookID = $(this).attr("id").replace("remove-", "");
                $('#action'+bookID).hide();
                $('#ajax-load-'+bookID).show();

                $.ajax({
                    type: 'POST',
                    url: "<?=base_url()?>bookacts/delbook/",
                    data: ({id: bookID}),
                    cache: false,
                    success: function (msg){

                        if(msg == ""){
                            $('#ajax-load-'+bookID).hide();
                            $('.box-error').fadeIn("slow");
                            $('#action-'+bookID).fadeIn();
                        } else {
                            $('#ajax-load-'+bookID).hide();
                            $('.box-success').fadeIn("slow")
                                 .animate({opacity: 1.0}, 2000)
                                 .fadeOut('slow');
                            $('#action-'+bookID).fadeIn();
                        }
                    }
                }); return false;
            } else {
                return false;
            }
        });
    });
</script>

但是我认为即使你改变了这些东西,你的问题也可能还没有解决。但是如果不了解它的作用就很难找出问题。

如果javascript正确(带ID),只需查看源代码检查您是否获得了JS确认消息。检查是否有AJAX,发出一些警报。检查是否存在javascript错误。等

祝你好运。

相关问题