2010-08-26 54 views
3

在下面的代碼中,html加載並將調用group_map設置python函數。我的問題是系統中啓用了分頁功能,因此如果頁面3中的用戶刪除了一個條目,頁面會重新加載並返回頁面1.How使同一頁面上的用戶逗留刪除條目jquery數據表分頁

DataTable中使用,在從http://www.datatables.net/

    <html> 
       <head> 
       <link rel="stylesheet" type="text/css" href="css/datatable.css" /> 
       <script type="text/javascript" src="js/jquery.dataTables.js"></script> 
       </head> 
       <script> 
       var oTable; 

       function fnFormatDetails (nTr) 
       { 
        var iIndex = oTable.fnGetPosition(nTr) ; 
        var aData = oTable.fnSettings().aoData[iIndex]._aData; 

        var sOut = aData[6]; 

        return sOut; 
       } 

        $(document).ready(function() { 
        $.ajaxSetup({ cache: false }); 
        oTable = $('#s_group_table').dataTable({ 
         "aoColumns": [ 
          {"sWidth": "30%" }, 
          {"sWidth": "20%" }, 
          {"bSortable": false,"sWidth": "5%" }, 
          {"bSortable": false,"sWidth": "5%" }, 
         ], 
         "aaSorting": [[0, 'desc']], 
         "bProcessing": true, 
         "bServerSide": true, 
         "sAjaxSource": "/repo/group_set/", 
         "bJQueryUI": true, 
         "sPaginationType": "full_numbers", 
         "bFilter": false, 
         "oLanguage" : { "sZeroRecords": "No data found", "sProcessing" : "Fetching Data" } 
        }); 

       }); 


       function delete_set(id) 
       { 
         $.post("/repo/group_set_delete/" + id) ; 
         oTable.fnDraw(true) ; 
         location.reload(); 
       } 
       </script> 

       <div id="s_group_table"> 
       <table cellpadding="0" cellspacing="0" border="0" width="100%"> 
        <thead> 
         <tr> 
          <th width="30%">Name</th> 
          <th width="30%"></th> 
         </tr> 
        </thead> 
        <tbody> 
         <tr> 
          <td colspan="2" class="dataTables_empty">No data found</td> 
         </tr> 
        </tbody> 
       </table> 
       </div> 





    def group_set(request): 
    try: 
     response_dict = {} 
     offset = int(request.GET.get('iDisplayStart')) 
     limit = int(request.GET.get('iDisplayLength')) + offset 
     echo = request.GET.get('sEcho') 

     sort_cols = int(request.GET.get('iSortingCols')) 
     for i in range(0, sort_cols): 
      dir = request.GET.get('iSortDir_' + str(i)) 
      if dir == "asc": 
       dir = "" 
      else: 
       dir = "-" 
      order_by = dir + group_map (request.GET.get('iSortCol_' + str(i))) + "," 

     order_by = order_by.strip(',') 


     total = GroupSet.objects.filter(pk=request.profile.my_id).count() 
     if limit > total: 
      limit = total 

     if order_by == "": 
      groupset = GroupSet.objects.filter(pk=request.profile.my_id)[offset:limit] 
     else: 
      groupset = GroupSet.objects.filter(pk=request.profile.my_id).order_by(order_by)[offset:limit] 

     p_array = [] 
     p_array_o = [] 
     for q in studentprofilegroup_map: 
      delete_l = "<a><img title='Delete group' class=center src=\"/repo/images//del.gif\" onclick=javascript:delete_set(\"%d\")></a>&nbsp;" % (q.id) 

      emp_name = q.first_name + q.last_name 

      p_array_o = [emp_name , delete_l ] 
      p_array.append(p_array_o) 
     response_dict.update({'sEcho': echo, 'iTotalRecords': total, 'iTotalDisplayRecords': total, 'aaData': p_array}) ; 

     return HttpResponse(simplejson.dumps(response_dict), mimetype='application/javascript') 
    except: 
     print "No records found" 

刪除功能是

def group_set_delete(request,gid): 
    try: 
     s_gp = GroupSet.objects.filter(pk=gid) 
     s_gp.delete() 
    except: 
     print "could not be deleted" 

回答

-1

location.reload();是做什麼用的?檢查它是否發送請求到group_set與正確的page查詢參數(即/app/group_set/?page=3)對應的URL。如果沒有,那麼你可能想要明確地構造URL並調用它。

+0

它不是發送頁碼,如何獲取當前頁碼? – Hulk 2010-08-26 07:07:47

0

我有一個不同的方式相同的問題。當我更新3.頁中的行後,該表用於自動返回到1.頁面。fnUpdate的第四個參數採用true/false來更新該表和默認值爲true。我將它改爲假,然後開始工作。與您遇到的情況不完全相同,但可能有助於解決您的問題。看看你在代碼中使用的函數的參數。 Ozlem。

$('#rulesTable').dataTable().fnUpdate(suburb_name, rowNo, 3, false); 
+0

你的例子中的硬編碼值3是什麼 – Hulk 2010-12-28 15:49:09

0

如果您正在使用AJAX發佈,有與location.reload()更新整個頁面是沒有意義的 - 這帶來了數據表到它的默認狀態,第1

This example shows a datatable with a delete option,只有從網格中刪除單行,而無需重新加載頁面。所有你需要做的就是將你的電話添加到服務器,所以它會是這樣的:

/* Add a click handler for the delete row */ 
$('#delete').click(function() { 
    $.post("/repo/group_set_delete/", id, function() { 
     /* Remove the row from the grid after server response */ 
     var anSelected = fnGetSelected(oTable); 
     oTable.fnDeleteRow(anSelected[0]); 
    }); 
});