我在stackoverflow上看到过类似的问题。但是,我还没有看到任何关于网格的东西。我是 jquery 的新手,所以请多多包涵。
我在一个局部视图中有一个 Html.Grid,它接收数据并将它们显示在行中,没什么特别的。选择一行时,它将在屏幕上显示DIV,允许用户对所选项目(行)进行投票。
到目前为止,一切都很好。事情按预期工作。
现在,我在页面上也有一些搜索功能。当用户单击搜索按钮时,网格会使用来自操作方法的新数据进行刷新。然后问题发生在这一点上,因为网格的新行以某种方式失去了与在 jquery 中分配给它们的操作的关联。我什至尝试再次调用该方法(下面称为 prepGrid),但没有用。我被卡住了,所以任何帮助将不胜感激。下面是我拥有的部分代码:
<script>
function prepGrid() {
pageGrids.FeedbackGrid.onRowSelect(function (e) {
document.getElementById("VoteDiv").style.width = "100%";
$.ajax({
cache: false,
type: "POST",
url: "/Home/GetFeatureRequestVotePartial",
data: { "requestId": e.row.Id },
success: function (rdata) {
$('#VoteDiv').html(rdata);
},
error: function (xhr, ajaxOptions, thrownError) {
alert('Failed to retrieve the requested feature!.');
}
});
});
}
var onSearchClicked = function(){
var requestId = $('#RequestIdSearch').val();
var requesterName = $('#RequesterNameSearch').val();
var title = $('#TitleSearch').val();
var description = $('#DescriptionSearch').val();
$.ajax({
cache: false,
type: "POST",
url: "/Home/GetFeatureRequests",
data: { "requestId": requestId, "requesterName": requesterName, "title": title, "description": description },
success: function (rdata) {
$('#GridDiv').html(rdata);
},
error: function (xhr, ajaxOptions, thrownError) {
alert('Failed to retrieve request features!.');
}
});
}
var onVoteClick = function (requestId) {
$.ajax({
cache: false,
type: "POST",
url: "/Home/VoteForRequest",
data: { "requestId": requestId },
success: function (rdata) {
$('#VoteDiv').html(rdata);
},
error: function (xhr, ajaxOptions, thrownError) {
alert('Failed to retrieve the requested feature!.');
}
});
}
</script>
这就是我在 _RequestFeatureGridPartial 中的内容:
<div style="width:100%;">
@Html.Grid(Model).Named("FeedbackGrid").Columns(columns =>
{
columns.Add(c => c.Id).Titled("Request Number").SetWidth("10%");
columns.Add(c => c.NumOfVotes).Titled("Votes").SetWidth("5%").Sortable(true);
columns.Add(c => c.AspNetUser.FullName).Titled("Requester").Filterable(true).SetWidth("15%");
columns.Add(c => c.RequestedDate).Titled("Date Requested").SetWidth("15%");
columns.Add(c => c.Title).Titled("Title").SetWidth("20%");
columns.Add(c => c.Description).Titled("Description")
.RenderValueAs(c => c.Description.Substring(0, (c.Description.Length > 50) ? 50 : c.Description.Length)
+ ((c.Description.Length > 50) ? "..." : "")).SetWidth("35%");
}).WithPaging(10).Sortable(true)
</div>