gpt4 book ai didi

javascript - 获取选定的行 id jquery 数据表行选择

转载 作者:行者123 更新时间:2023-11-30 00:17:06 26 4
gpt4 key购买 nike

我无法获取选定的行 ID。我正在使用数据表行选择。我在控制台日志中收到 [],[""]。我已经在 SO 上寻找其他问题并尝试但没有帮助

我的javascript代码是

  $(document).ready(function () {

var selectedids = [];

var otable = $('#Table1').DataTable({
"bSort": false,
"rowCallback": function (row, data) {
if ($.inArray(data.DT_RowId, selectedids) !== -1) {
$(row).addClass('selected');
}
}
});
$('#Table1 tbody').on('click', 'tr', function () {
var id = this.id;
var index = $.inArray(id, selectedids);

var ids = $.map(otable.rows('.selected').data(), function (item) {
return item[0]
});

console.log(ids)

if (index === -1) {
selectedids.push(id);
console.log(selectedids);
} else {
selectedids.splice(index, 1);
}

$(this).toggleClass('selected');
});

});

我正在使用来自 mvc 中的 Controller 的 json 数据填充我的数据表

    $('#ID').change(function () {
$("#t1 tbody tr").remove();

$.ajax({
type: 'POST',
url: '@Url.Action("")',
dataType: 'json',
data: { id: $("#ID").val() },
success: function (data) {
var items = '';
$.each(data, function (i, item) {

var rows = "<tr>"
+ "<td>" + item.id + "</td>"
+ "<td>" + item.yyy + "</td>"
+ "<td>" + item.aaa + "</td>"
+ "<td>" + item.eee + "</td>"
+ "<td>" + item.yyygg + "</td>"
+ "</tr>";
$('#Table1 tbody').append(rows);
});

},
error: function (ex) {
var r = jQuery.parseJSON(response.responseText);
alert("Message: " + r.Message);

alert("StackTrace: " + r.StackTrace);
alert("ExceptionType: " + r.ExceptionType);
}
});
return false;

});

最佳答案

如果您使用数据表,您可以免去很多痛苦 select extension :

var table = $('#example').DataTable({
select: {
style: 'multi'
}
})

var selectedIds = [];

table.on('select.dt', function(e, dt, type, indexes) {
selectedIds.push(indexes[0]);
console.log(selectedIds);
})

table.on('deselect.dt', function(e, dt, type, indexes) {
selectedIds.splice(selectedIds.indexOf(indexes[0]), 1);
console.log(selectedIds);
})

演示 -> http://jsfiddle.net/0w1p7a3s/

关于javascript - 获取选定的行 id jquery 数据表行选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34333567/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com