[英]Ternary operator in Template Literal HTML TABLE
I am trying to set my table data to have a background color of red if it has a "Failed" status. 我试图将表格数据设置为具有“失败”状态的红色背景色。 I am having a hard time setting this inline style.
我很难设置这种内联样式。 At first I tried to use the ternary operator in the second td of the createTableData function but could not get it to work.
最初,我尝试在createTableData函数的第二个td中使用三元运算符,但无法使其正常工作。 Right now I am attempting to break it out into its own function but not working either.
现在,我正在尝试将其分解成自己的功能,但都不起作用。
Looking for help on setting the background color of my table data. 在设置表格数据的背景颜色方面寻求帮助。
HTML 的HTML
<body>
<div class="container-fluid">
<div class="jumbotron">
<h1 class="text-center mb-3">
Active Directory Replication Health
</h1>
</div>
<div id="table"></div>
</div>
<script src="js/jquery-3.3.1.min.js"></script>
<script src="js/bootstrap.bundle.min.js"></script>
<script src="js/script.js"></script>
Script 脚本
var data = [
{
"DC": "SV07CTDC1",
"Connectivity": "Passed",
"Advertising": "Failed"
},
{
"DC": "SVGCCTDC1",
"Connectivity": "Passed",
"Advertising": "Failed"
}
];
function checkStatus(status) {
status === "Failed" ? "background-color: red" : "background-color: blue"
}
function createTableData(dc ){
var html = ''
for( key in dc )
html += `
<tr>
<td> ${key} </td>
<td style=${checkStatus(dc[key])} > ${dc[key]} </td>
</tr>
`
return html
}
function createTable(dc) {
return `
<div class="col-md-3 col-sm-6 ">
<table class="table table-dark table-striped table-bordered table-hover">
${createTableData(dc)}
</table>
</div>
`
}
document.getElementById("table").innerHTML = `
<div class="row">
${data.map(createTable).join("")}
</div>
`
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.