2011-03-09 75 views
2

我正在創建圖書管理Web應用程序,並且在使用分頁時刪除圖書時出現問題。代碼點火器分頁和JQuery動作

在我的控制器我有這樣的代碼:

<?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

我沒有馬上看到問題。然而在我看來,你使用CodeIgniter和Jquery都是錯誤的,所以我只給你一些提示。

首先Codeigniter使用MVC模式,正如您可能知道的那樣。而一個視圖並不是真正意義上的SQL。更好的是,大多數時候Controller甚至不用於SQL。您應該嘗試將SQL保留在模型中。

另一個小的Codeigniter的事情。我認爲這是更好的使用:

function index($page=0) { 

然後:

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

而不是使用$這個 - > URI->段(3)

其次,你應該使用jQuery '動態'太。您現在正在爲每本書製作功能,同時您始終想要執行相同的操作。

添加類:

<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錯誤。等等

祝你好運。