[英]Update HTML page after jquery.click
我有一个onclick函数,基本上只返回排序后的数据,如下所示:
$(document).ready(function () {
$(".feedbackClick").click(function () {
$.post("/Analyze/GetSortedByFeedback")
.done(function (data) {
var sellers = $('<table />').append(data).find('#tableSellers').html();
$('#tableSellers').html(sellers);
});
});
});
});
这就是我在jquery帖子之后尝试更新的表的样子:
<table id="tableSellers" class="table table-striped jambo_table bulk_action">
<thead>
<tr class="headings">
<th class="column-title"><h4><i class="fa fa-user" style="text-align:center"></i> <span>Username</span></h4> </th>
<th class="column-title"> <h4><span class="glyphicon glyphicon-tasks salesClick" aria-hidden="true"></span></h4></th>
<th class="column-title"><h4><i class="fa fa-star feedbackClick"></i></h4></th>
</tr>
</thead>
<tbody>
@foreach (var item in ViewBag.rezultati)
{
<tr>
<td><a href="http://ebay.com/usr/@item.StoreName" target="_blank">@item.StoreName</a></td>
<td>
<b>
@item.SaleNumber
</b>
</td>
<td><b>@item.Feedback</b></td>
</tr>
}
</tbody>
</table>
点击基本上只会获取结果并更新HTMl中的表格...
有人可以帮我吗?
编辑:
当前方法不起作用...我触发了事件但什么也没发生...动作中的代码被正确调用,但是结果不显示...
编辑2:
这是.done之后的数据对象的内容:
System.Collections.Generic.List`1[WebApplication2.Controllers.ResultItem]
编辑3:
这是动作:
public List<ResultItem> GetSortedByFeedback()
{
return lista.OrderByDescending(x => x.Feedback).ToList();
}
Edit4这是Alexandru发表后的数据:
Array[100]
现在我可以做:
data[0].Feedback
并在控制台中输出:
61259
您实际上想做的是将JSON数据附加到HTML元素,这当然不能按预期工作。
考虑使用诸如jQuery Templates之类的模板引擎。 您将能够编译HTML模板,并在需要时使用它来呈现数据。 例如:
var markup = "<li><b>${Name}</b> (${ReleaseYear})</li>";
// Compile the markup as a named template
$.template( "movieTemplate", markup );
$.ajax({
dataType: "jsonp",
url: moviesServiceUrl,
jsonp: "$callback",
success: showMovies
});
// Within the callback, use .tmpl() to render the data.
function showMovies( data ) {
// Render the template with the "movies" data and insert
// the rendered HTML under the 'movieList' element
$.tmpl( "movieTemplate", data )
.appendTo( "#movieList" );
}
尝试这样的事情:
$(document).ready(function () {
$("body").on("click",".feedbackClick",function() {//delegate the click event
$.get("/Analyze/GetSortedByFeedback",function(data) {
var sellers = $(data).find('#tableSellers').html();//find the table and take the html
$('#tableSellers').html(sellers);//append the html
});
});
});
注意:您需要从Ajaxed页面返回html(以您的情况为准)
来自@Alexandru部分回复,您可以执行以下操作
public JsonResult GetSortedByFeedback()
{
var list=lista.OrderByDescending(x => x.Feedback).ToList();
return Json(list,JsonRequestBehavior.AllowGet);
}
JS:
$(document).ready(function () {
$("body").on("click",".feedbackClick",function() {//delegate the click event
$.get("/Analyze/GetSortedByFeedback",function(data) {
$('#tableSellers tbody').empty();//empty the table body first
$.each(data,function(i,item){//loop each item from the json
$('#tableSellers tbody').append('<tr><td><a href="http://ebay.com/usr/'+item.StoreName+'" target="_blank">'+item.StoreName+'</a></td><td><b>'+item.SaleNumber+'</b></td><td><b>'+item.Feedback+'</b></td></tr>');//build and append the html
});
});
});
});
请使用此:
public JsonResult GetSortedByFeedback()
{
var list=lista.OrderByDescending(x => x.Feedback).ToList();
return Json(list);
}
如果您的方法是GET
请使用以下方法:
public JsonResult GetSortedByFeedback()
{
var list=lista.OrderByDescending(x => x.Feedback).ToList();
return Json(list,JsonRequestBehavior.AllowGet);
}
然后请使用:
.done(function (data) {
$('#tableSellers tbody').empty();
$.each(data,function(i,item){
var tr='<tr><td><a href="http://ebay.com/usr/'+item.StoreName+'" target="_blank">'+item.StoreName+'</a></td><td><b>'+item.SaleNumber+'</b></td><td><b>'+item.Feedback+'</b></td></tr>';
$('#tableSellers tbody').append(tr);//append the row
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.