我已经硬编码了两个值,它们在一个具有在线状态的一个上显示一个绿色图标,在一个离线的一个上显示一个红色图标。现在我希望它在调用我的函数时自动在表格中添加绿色图标。
<div class="col-md-8">
<table class="table table-bordered table striped " id="thinker_table" >
<thead class="thead-dark">
<tr>
<th>Thinker Name</th>
<th>MAC Address</th>
<th>Status</th>
<th>Indicator</th>
<th>Show Routines</th>
<th>Show Devices</th>
</thead>
</tr>
<td>IPLConference Room</td>
<td>XXXXXXXXXXXXX</td>
<td >Online</td>
<td>
<div class="led-green"></div>
<td> <input type="button" value="Click Here" onclick="window.open('RoutineDetails.php','popUpWindow','height=500,width=700,left=100,top=100,resizable=yes,scrollbars=yes,toolbar=yes,menubar=no,location=no,directories=no, status=yes');"></td>
<td> <input type="button" value="Click Here" onclick="window.open('DeviceDetails.php','popUpWindow','height=500,width=800,left=100,top=100,resizable=yes,scrollbars=yes,toolbar=yes,menubar=no,location=no,directories=no, status=yes');"></td>
<tr>
<td>Host_34F60E </td>
<td>XXXXXXXXXX</td>
<td >Offline</td>
<td>
<div class="led-red"></div>
</td>
<tfoot >
<tr>
<th>Thinker Name</th>
<th>MAC Address</th>
<th>Status</th>
</tfoot >
</table>
</div>
</div>
</div>
这是我下面的 javascript,我在表格中显示结果。表格应显示一个绿色图标,其中状态 = 1。因为我的条件是如果状态 = 1,因此我应该在所有表格行上显示绿色图标。
$(document).ready(function(){
$.getJSON("test.json", function(data){
var thinker_data = '';
$.each(data.data, function(key, value){
if(value.status == "1")
{
thinker_data += '<tr>';
thinker_data += '<td>'+value.name+'</td>';
thinker_data += '<td>'+value.mac+'</td>';
thinker_data += '<td>'+value.status+ '</td>';
thinker_data += '</tr>';
}
});
$('#thinker_table').append(thinker_data);
});
});
</script>
另外我如何在表格中添加两个按钮?
我不太确定我是否理解这个问题,但从我读过的内容来看,如果用户的状态为 1,您希望显示一个绿色圆圈。您可以从 json 文件中获取该信息。
那么你需要先在 css 中创建圆圈!
css:
.online {
height: 25px;
width: 25px;
background:green;
border-radius: 50%;
border: 1px solid;
display: inline-block;
}
.offline {
height: 25px;
width: 25px;
background:red;
border-radius: 50%;
border: 1px solid;
display: inline-block;
}
然后,当你创建一个新行时,你有你想要的列的“在线”类:
$.each(data.data, function(key, value){
if(value.status == "1")
{
thinker_data += '<tr>';
thinker_data += '<td>'+value.name+'</td>';
thinker_data += '<td>'+value.mac+'</td>';
thinker_data += '<td class="online"></td>';
thinker_data += '<td></td>' //to make the "Indicator" column space
thinker_data += '<td> <input type="button" value="Click Here" onclick="window.open(\'RoutineDetails.php\',\'popUpWindow\',\'height=500,width=700,left=100,top=100,resizable=yes,scrollbars=yes,toolbar=yes,menubar=no,location=no,directories=no, status=yes\');"></td><td> <input type="button" value="Click Here" onclick="window.open(\'DeviceDetails.php\',\'popUpWindow\',\'height=500,width=800,left=100,top=100,resizable=yes,scrollbars=yes,toolbar=yes,menubar=no,location=no,directories=no, status=yes\');"></td>'
thinker_data += '</tr>';
}
});
请注意,我创建了一个示例输入,因为我无权访问“test.json”文件。
检查JSFiddle以获取工作示例。
如果您也不想在 status=0 处出现红色圆圈,请使用离线课程!
$(document).ready(function() {
var thinker_data = '';
let data = {
"data": [{
"name": "text",
"mac": "SOMETHIN5239321",
"status": "1"
}, {
"name": "tex2t",
"mac": "S23THIN5239321",
"status": "1"
}]
};
$.each(data.data, function(key, value) {
if (value.status == "1") {
thinker_data += '<tr>';
thinker_data += '<td>' + value.name + '</td>';
thinker_data += '<td>' + value.mac + '</td>';
thinker_data += '<td class="online"></td>';
thinker_data += '<td></td>' //to make the "Indicator" column space
thinker_data += '<td> <input type="button" value=\Click Here" onclick="window.open(\'RoutineDetails.php\',\'popUpWindow\',\'height=500,width=700,left=100,top=100,resizable=yes,scrollbars=yes,toolbar=yes,menubar=no,location=no,directories=no, status=yes\');"></td><td> <input type="button" value="Click Here" onclick="window.open(\'DeviceDetails.php\',\'popUpWindow\',\'height=500,width=800,left=100,top=100,resizable=yes,scrollbars=yes,toolbar=yes,menubar=no,location=no,directories=no, status=yes\');"></td>';
thinker_data += '</tr>';
}
});
$('#thinker_table').append(thinker_data);
});
.online {
height: 25px;
width: 25px;
background: green;
border-radius: 50%;
border: 1px solid;
display: inline-block;
}
.offline {
height: 25px;
width: 25px;
background: red;
border-radius: 50%;
border: 1px solid;
display: inline-block;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<div class="col-md-8">
<table class="table table-bordered table striped " id="thinker_table">
<thead class="thead-dark">
<tr>
<th>Thinker Name</th>
<th>MAC Address</th>
<th>Status</th>
<th>Indicator</th>
<th>Show Routines</th>
<th>Show Devices</th>
</tr>
</thead>
<td>IPLConference Room</td>
<td>XXXXXXXXXXXXX</td>
<td>Online</td>
<td>
<div class="led-green"></div>
<td> <input type="button" value="Click Here" onclick="window.open('RoutineDetails.php','popUpWindow','height=500,width=700,left=100,top=100,resizable=yes,scrollbars=yes,toolbar=yes,menubar=no,location=no,directories=no, status=yes');"></td>
<td> <input type="button" value="Click Here" onclick="window.open('DeviceDetails.php','popUpWindow','height=500,width=800,left=100,top=100,resizable=yes,scrollbars=yes,toolbar=yes,menubar=no,location=no,directories=no, status=yes');"></td>
<tr>
<td>Host_34F60E </td>
<td>XXXXXXXXXX</td>
<td>Offline</td>
<td>
<div class="led-red"></div>
</td>
<tfoot>
<tr>
<th>Thinker Name</th>
<th>MAC Address</th>
<th>Status</th>
</tfoot>
</table>
</div>
编辑:我已经编辑了代码和片段以在每一列中重复您的按钮。我认为这不是最好的解决方案,但是您并不清楚您的目标是什么/您的环境是什么。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句