因此,我为此做了一些工作,但是不幸的是,它需要onclick
列模板中的内容才能处理我的KendoUI网格中的特定行。我想将其替换为jQuery click事件,以实现可维护性。棘手的事情之一是此事件需要Id
单击按钮的行中的,但是现在我对获取该事件的代码更感兴趣。
这是我到目前为止的样本:
列模板
// columns is a Kendo.Mvc.UI.Fluent.GridColumnFactory of
// the type of the object I'm mapping to it
columns.Bound(p => p.SomeField).ClientTemplate(
"<button type='button' class='btn btn-somefield'" +
"onclick='javascript:SomeAction(this, #=Id#);>" +
"Some Button" +
"</button>"
);
JavaScript功能
function SomeAction(el, id){
var reqUrl = 'someUrl' + '?id=' + id;
$.ajax({
type: 'GET',
url: reqUrl,
dataType: 'json',
contentType: 'application/json; charset=utf-8',
success: function (content) {
console.log('request successful for id: ' + id);
}
});
}
我对jQuery实现有什么:
列模板
columns.Bound(p => p.SomeField).ClientTemplate(
"<button type='button' class='btn btn-somefield'>" +
"Some Button" +
"</button>"
);
jQuery函数
$('.btn-somefield').on('click', function(){
// Here I have some unfinished code to get the Id and everything.
// I'm not too concerned about figuring this out in this question,
// but suggestions are welcome
// Otherwise it's the same body as the plain JS function
});
总结一下:现在,单击按钮时,jQuery事件只是没有被点击。我已经尝试过许多类似的迭代方式$(selector).on('click', function(){});
,$(selector).click(function(){});
但都没有成功。
我担心在实际到达事件处理程序后如何提取该行的ID,但是现在我只需要单击按钮后单击事件即可。
(此外,如果对任何人都没有影响,则整个项目是一个asp.net mvc应用程序。)
尝试使用jQuery事件委托。这使您可以在生成网格按钮之前创建事件处理程序。然后获取ID,可以使用dataItem()方法获取网格行的dataItem:
$(document).on('click', '.btn-somefield', function(e){
var grid = $("#grid").data("kendoGrid");
var dataItem = grid.dataItem($(this).closest('tr'));
var ID = dataItem.Id;
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句