gpt4 book ai didi

javascript - 在具有单元格值的数据表中按条件显示按钮

转载 作者:搜寻专家 更新时间:2023-11-01 05:03:40 26 4
gpt4 key购买 nike

我有一个使用 Datatables 的表,最后一列显示为默认值 0,但它也可以显示值 >=1 意味着,只要它具有 0 值,它就不应该做任何事情,但是一旦大于等于 1,我希望显示一个按钮,该按钮从数据表中选择一个值,然后打开一个模式。

不确定如何完成这个 Button 的事情。

下面是我的数据表代码,包括。 html.

// Manual Modal
$('#myModal').on('shown.bs.modal', function () {
$('#myInput').focus()
});

// Datatables Code
$(document).ready(function() {
$('#DTResTableList_1').DataTable({
"ajax": {
url: 'data.inc.php',
method: 'POST',
dataType: 'json',
contentType: 'application/json; charset=utf-8',
dataSrc: ""
},
paging: false,
scrollY: 400,
select: true,
'columns': [{
'data': 'TABLE_NUMBER'
},
{
'data': 'STATION'
},
{
'data': 'GUESTS'
},
{
'data': 'T_STATUS'
},
{
'data': 'MINUTES_SEATED'
},
{
'data': 'MINUTES_OVERDUE'
}
]
});
setInterval(function() {
$('#DTResTableList_1').DataTable().ajax.reload(null, false); // user paging is not reset on reload
}, 5000);
});
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" rel="stylesheet" />
<link href="https://cdn.datatables.net/1.10.13/css/jquery.dataTables.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js"></script>
<script src="https://cdn.datatables.net/1.10.13/js/jquery.dataTables.min.js"></script>


<!-- Button trigger modal -->
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal">
Launch demo modal
</button>

<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body">
...
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>

<div class="container-fluid">
<table class="table table-sm table-striped table-hover" id="DTResTableList_1" style="width: 100%;">
<thead>
<tr>
<th class="" data-toggle="tooltip" data-placement="top" title="Table Number">Table</th>
<th class="" data-toggle="tooltip" data-placement="top" title="Waiterstation">Station</th>
<th class="" data-toggle="tooltip" data-placement="top" title="Guests on Table">G</th>
<th class="" data-toggle="tooltip" data-placement="top" title="Table Stauts">Status</th>
<th class="" data-toggle="tooltip" data-placement="top" title="Minutes Seated">Minutes</th>
<th class="" data-toggle="tooltip" data-placement="top" title="Overdue">Button</th>
</tr>
</thead>
</table>
</div>

最佳答案

好的,这是一个更接近您要求的。如果员工未满 40 岁,则将按钮放在最后一列。同样,代码获取该行的数据,然后显示员工的姓名。请注意我是如何在 columnDef 中使用渲染的。

http://jsbin.com/gobonec/edit?js,output

$(document).ready(function () {



var table = $('#example').DataTable({
"data": dataStore.data,
"columns": [
{ "data": "name" },
{ "data": "age" },
{ "data": "position" },
{ "data": "office" },
{ "data": "extn" },
{ "data": "start_date" },
{ "data": "salary" },
{ "data": null },
],
columnDefs: [{
// puts a button in the last column
targets: [-1], render: function (a, b, data, d) {
if (data.age < 40) {
return "<button type='button'>Go</button>";
}
return "";
}
}],

});
table.on("click", "button",
function () {
alert(table.rows($(this).closest("tr")).data()[0].name);

});

});

关于javascript - 在具有单元格值的数据表中按条件显示按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43441771/

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