gpt4 book ai didi

javascript - 使用jquery中的引导表将id添加到动态创建的行

转载 作者:行者123 更新时间:2023-12-03 02:22:51 25 4
gpt4 key购买 nike

我正在使用 JSON 值创建 Bootstrap 表。我可以动态地将类添加到行中。但在创建动态引导表时,我无法将 ID 添加到行。请找到我的代码,

HTML:

<table id="eventsTable" data-checkbox="true" data-sortable="true" data-row-style="rowStyle"></table>

JS:

$.ajax({
type: "POST",
url: "apilist",
data: "",
success: function (response) {
if (response != null) {
var data = [];
$.each(response.Slip, function (index) {
var obj = {};
obj.SlipID = response.Slip[index].SlipID;
obj.Client = response.Slip[index].Client;
data.push(obj);
});
$('#eventsTable').bootstrapTable({
columns: [
{ field: "state", checkbox: true },
{ field: "SlipID", title: 'SlipID', sortable: true, class : "hide" },
{ field: "Client", title: 'Client', sortable: true },
],
data: data,
});
}
}
});

请告诉我建议,谢谢。

最佳答案

为此,您可以使用 rowAttributes对于行属性rowStyle用于行样式。

下面是您可以检查的代码片段

我希望这能帮助/指导您或实现您的要求。

var data = [{
"name": "Bahrain",
"code": "BH"
}, {
"name": "Burundi",
"code": "BI"
}, {
"name": "Benin",
"code": "BJ"
}, {
"name": "Bhutan",
"code": "BT"
}, {
"name": "Jamaica",
"code": "JM"
}, {
"name": "Bouvet Island",
"code": "BV"
}, {
"name": "Botswana",
"code": "BW"
}, {
"name": "Samoa",
"code": "WS"
}, {
"name": "Bonaire, Saint Eustatius and Saba ",
"code": "BQ"
}, {
"name": "Brazil",
"code": "BR"
}];

$('#eventsTable').bootstrapTable({
columns: [{
title: "Conutry Code",
field: 'code',
sortable: true
}, {
title: "Conutry name",
field: 'name',
sortable: true
}],
data: data,
rowStyle: function(row, index) {
return {
classes: `row-border ${index%2==1?'row-even':'row-odd'}`
};
},
rowAttributes: function(row, index) {
return {
id: `x-bootstrap-row-${index+1}`
};
}
});
.row-border{
border-bottom: 1px solid #ccc;
}
.row-odd{
background-color: wheat;
}
.row-even{
background-color: whitesmoke;
}
<link href="//cdnjs.cloudflare.com/ajax/libs/bootstrap-table/1.11.1/bootstrap-table.min.css" rel="stylesheet"/>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>


<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-table/1.11.1/bootstrap-table.min.js"></script>


<table id="eventsTable" data-checkbox="true" data-sortable="true" data-row-style="rowStyle"></table>

关于javascript - 使用jquery中的引导表将id添加到动态创建的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49081148/

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