DIV search_status中的代码在页面加载后使用排序和分页,但是如果我从ajax调用中调用完全相同的数据来替换内容,则排序仍然有效,但分页会中断。任何想法为什么会发生这种情况以及我如何解决的想法?
编辑:添加以下内容可解决此问题。
$("#tablesorter").tablesorterPager({ container: $("#pager"), positionFixed: false });
-
<script type="text/javascript" src="/js/jquery.metadata.js"></script>
<script type="text/javascript" src="/js/jquery.tablesorter.js"></script>
<script type="text/javascript" src="/js/jquery.tablesorter.pager.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#tablesorter").tablesorter({ debug: false, widgets: ['zebra', 'columnHighlight'] })
.tablesorterPager({ container: $("#pager"), positionFixed: false });
});
$(function() {
if ($("#search").val()!=''){
asearch();
}
function asearch() {
$.post("/_ajax/search_table",{
search: $("#search").val()
},
function(data){
$("#search_status").html(data);
$("#tablesorter").tablesorter({widgets: ['zebra']});
});
return false;
}
$("#button_search").click(function() {
asearch();
});
$('#search').bind('keydown',function(e){
if (e.keyCode==13 || e.which==13) asearch();
});
});
}
</script>
Search <input id="search" name="search" type="text"> <input id="button_search" type="button" value="Search">
<div id="search_status" style="text-align:left">
<table cellspacing="1" id="tablesorter" class="tablesorter">
<thead>
<tr align="center">
<th>ID</th>
<th>Name</th>
</tr>
</thead>
<tfoot>
<tr id="pager" align="center">
<td colspan="7">
<img src="/images/first.png" class="first" alt="First Page" height="16" width="16">
<img src="/images/prev.png" class="prev" alt="Previous Page" height="16" width="16">
<label class="pagedisplay"></label> <img src="/images/next.png" class="next" alt="Next Page" height="16" width="16">
<img src="/images/last.png" class="last" alt="Last Page" height="16" width="16">
<select class="pagesize">
<option selected="selected" value="20">20</option>
<option value="30">30</option>
<option value="40">40</option>
</select>
</td>
</tr>
</tfoot>
<tbody>
<tr align="center">
<td><a href="javascript:set_selection('2');">2</a></td>
<td><a href="javascript:set_selection('2');">John</a></td>
</tr>
<tr align="center">
<td><a href="javascript:set_selection('1');">1</a></td>
<td><a href="javascript:set_selection('1');">Doe</a></td>
</tr>
</tbody>
</table>
</div>
答案 0 :(得分:0)
在使用ajax调用加载表时,我常常遇到寻呼机和表格分类器的问题。在尝试了几个不同的建议(没有一个完全有效)之后,我发现将JQuery tablesorter脚本放在我的局部视图中,而不是在主视图中做了诀窍。
在主视图中
$(document).ready(function () {
$(document).ajaxStart(function () {
$('#ajaxBusy').show();
$('#details').hide();
$('#pager').hide();
}).ajaxStop(function () {
$('#ajaxBusy').hide();
$('#details').show();
$('#pager').show();
});
...
});
function SearchCustomerStock(force) {
var searchText = $("#FindStock_ED").val();
var customerId = '@ViewBag.EncodedID';
if (force || searchText.length > 2) {
$('#ajaxBusy').show();
$('#details').hide();
var dataObject = { CustomerId: customerId, Like: searchText, Perspective: $('#Perspective').val(), PerspectiveId: $('#PerspectiveId').val() };
$.ajax({
type: "GET",
url: "/CustomerStock/GetStockLike",
data: dataObject,
contentType: "application/json; charset=utf-8",
//async: false,
success: function (data) {
//$('#ajaxBusy').hide();
//$('#details').show();
$('#details').html(data);
/*var sorting = [[0, 0]];
$("table.tablesorter")
.tablesorter(
{ widthFixed: true },
{ sortList: [[0, 0]] },
{ headers: { 7: { sorter: false } } })
.tablesorterPager({ container: $("#pager"), size: $(".pagesize option:selected").val() });*/
},
error: function (jqXHR, textStatus, errorThrown) {
$('#ajaxBusy').hide();
$('#details').show();
alert(jqXHR + ', ' + textStatus + ', ' + errorThrown);
},
cache: false
});
}
//$("#FindStock_ED").attr('disabled', false);
return false;
}
在局部视图中
@model IEnumerable<SkyPortR.Models.CustomerStockViewModel>
<script type="text/javascript">
$("table.tablesorter")
.tablesorter(
{ widthFixed: true },
{ sortList: [[0, 0]] },
{ headers: { 7: { sorter: false } } })
.tablesorterPager({ container: $("#pager"), size: $(".pagesize option:selected").val() });
</script>
<table class="tablesorter">
<thead>
<tr>
<th>
Barcode
</th>
<th>
Alternate Code
</th>
<th>
Group
</th>
<th>
Description
</th>
<th>
Packsize
</th>
<th>
LastCost
</th>
<th>
Supplier
</th>
<th class="indexImages">
</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model) {
<tr>