[英]how to fix on click event. it is not working even the debug is not pointing toward it
i'm creating a datatable
for insert, update
. 我创建一个
datatable
的insert, update
。 edit event will fire on the click of but it is not firing any event on click, even the debug is not pointing toward it on click. 编辑事件将在单击时触发,但单击时不会触发任何事件,即使调试未指向单击时也是如此。
function GetLoginData() {
$.ajax({
url: '/LoginMaster/GetLoginData',
type: 'get',
dataType: 'json',
success: function (result) {
var GetLoginData = result;
//$('#tblLoginMaster').DataTable.clear();
//$('#tblLoginMaster').DataTable.destroy();
// table.empty();
if (GetLoginData != '') {
var str = "";
$.each(GetLoginData, function (index, obj) {
var GetID = obj.Id == null ? "" : obj.Id;
var GetName = obj.Name == null ? "" : obj.Name;
var GetEmailID = obj.Email == null ? "" : obj.Email;
var GetPassword = obj.Password == null ? "" : obj.Password;
var GetRole = obj.Role == null ? "" : obj.Role;
This is where my editstyle
class is 这是我的
editstyle
类所在的地方
str += "<tr class='EditStyle' data-target='#addUserModal' data-toggle='modal' value='" + GetID +"'>";
str += "<td>" + GetID + "</td>";
str += "<td>" + GetName + "</td>";
str += "<td>" + GetEmailID + "</td>";
str += "<td>" + GetPassword + "</td>";
str += "<td>" + GetRole + "</td>";
str += "</tr>"
$('#tblbodyLoginMaster').html(str);
});
$('#tblLoginMaster').DataTable({
"paging": true,
"lengthChange": false,
"searching": false,
"ordering": true,
"info": true,
"autoWidth": false,
"sDom": 'lfrtip'
});
}
}
});
}
This is my click event
这是我的
click event
$('tr.EditStyle').on('click', function () {
if ((this).val() != 0) {
var rowData = (this).children('td').val();
$('#id').val(rowData[0]);
$('#username').val(rowData[1]);
$('#emailId').val(rowData[2]);
$('#password').val(rowData[3]);
$('#role').val(rowData[4]);
}
});
I'm suspecting your click handler doesn't work as your rows do not exist by the time you attempt to listen for clicks. 我怀疑您的点击处理程序不起作用,因为在您尝试监听点击时您的行不存在。 Try to change that, like this:
尝试更改它,如下所示:
$('#tblLoginMaster').on('click', 'tr.EditStyle', function () {
...
});
If that solves your problem, you may check out oficial tutorial on that topic. 如果这样可以解决您的问题,则可以查看有关该主题的官方教程 。
Another thing in your code that raises my concern is using jQuery $.ajax()
method to populate your table and the very method you populate that: 代码中引起我关注的另一件事是使用jQuery
$.ajax()
方法填充表以及填充该表的方法:
ajax
option and take the advantage of ajax.dataSrc
: ajax
选项并利用ajax.dataSrc
的优势: $('#tblLoginMaster').DataTable({
...,
ajax: {
url: '/LoginMaster/GetLoginData',
method: 'GET',
dataSrc: //here goes json property or the method that retrieves table data out of json response
}
});
columns
/ columnDefs
options, and make use of createdRow
option to assign necessary row attributes: columns
/ columnDefs
选项中的相应源数据属性,并利用createdRow
选项分配必要的行属性: $('#tblLoginMaster').DataTable({
...,
rowCreated: (tr, _, rowIdx) => $(tr).attr({
'class':'EditStyle',
'data-target': '#addUserModal',
'data-toggle': 'modal',
'value': rowIdx
}),
columns: [
{data: 'Id', header: 'Id'},
{data: 'Name', header: 'Name'},
{data: 'Password', header: 'Password'},
...
]
});
You may be reluctant to do such a global change to your app, but otherwise you may run into different sorts of issues related to AJAX-async nature and deprive yourself of the possibility to use the plethora of DataTables API methods to catch the bugs or enrich your app with new features. 您可能不愿意对应用程序进行这样的全局更改,但否则,您可能会遇到与AJAX异步性质相关的各种问题,并使自己无法使用大量的DataTables API方法来捕获错误或丰富信息。您的应用具有新功能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.