简体   繁体   English

获取引导表行以根据JSON数据更改颜色

[英]Getting a bootstrap table row to change colors based off of json data

I have a Bootstrap table with hover functionality, that populates with data retrieved from Twitch's JSON API. 我有一个具有悬停功能的Bootstrap表,其中填充了从Twitch的JSON API中检索到的数据。

It works fairly well, but I want, based off of a certain JSON variable (whether or not the user is offline) for the table row to change colors. 它工作得很好,但是我想基于某个JSON变量(无论用户是否离线)来改变表行的颜色。

The full code is here , and the most relevant JS snippets is below: 完整的代码在这里 ,最相关的JS代码片段在下面:

if (gameContent == null) {
      statusText = "Offline...";
      gameText = "Nothin'";
      row.addClass("offline");
      $("td", row).each(function() {
        $(td).addClass("offline");
      });
}

The offline class has a different background color. offline类具有不同的背景颜色。

The thing is, with row.addClass down being commented out, everything else works, but if those things aren't commented out, then those rows disappear completely, including text. 事实是,将row.addClass向下注释掉,其他所有内容都可以正常工作,但是如果不注释掉这些内容,则这些行会完全消失,包括文本。 And it seems like (through the Inspector) the class isn't being added to the tr or td elements at all, but for some reason, the text is getting wiped instead. 而且,似乎(通过Inspector)该类根本没有添加到tr或td元素中,但是由于某种原因,文本却被擦除了。 I just feel like I'm looking at the wrong portion of the code. 我只是觉得我在看代码的错误部分。 Any ideas? 有任何想法吗?

You don't need ot add class to the row instead add it to all the td inside row using 你不需要添加加时赛类行,而不是将它添加到所有的tdrow使用

$("td", row).each(function() {
            $(this).addClass("offline");
});

updated codepen 更新的代码笔

 //jquery $(document).ready(function() { //streamers var streamers = [ "ESL_SC2", "OgamingSC2", "cretetion", "freecodecamp", "storbeck", "habathcx", "RobotCaleb", "noobs2ninjas", "no_account_friend" ]; var num_streamers = streamers.length; //fcc info/call var url = "https://wind-bow.gomix.me/twitch-api/channels/"; var gameUrl = "https://go.twitch.tv/directory/game/"; var tbody = document.getElementById("streamers"); //stream parser function add_row(url_parsed) { $.getJSON( url_parsed, function(data) { if (data.status === 404) { return; } //set logo, status, game var logoImg = data.logo; var statusContent = data.status; var gameContent = data.game; if (logoImg == null) { logoImg = "https://static-cdn.jtvnw.net/jtv_user_pictures/xarth/404_user_600x600.png"; } //insert rows/cells var row = tbody.insertRow(tbody.rows.length); var logo = row.insertCell(0); var name = row.insertCell(1); var status = row.insertCell(2); var game = row.insertCell(3); //set text/html for cells var logoText = "<img src='" + logoImg + "' alt='" + data.display_name + " logo' height=50 width=50>"; var nameText = "<a href='" + data.url + "'>" + data.display_name + "</a>"; var statusText, gameText; //check if game is null if (gameContent == null) { statusText = "Offline..."; gameText = "Nothin'"; /* --problem code here*/ // row.addClass("offline"); // row.className ="offline"; $("td", row).each(function() { $(this).addClass("offline"); }); } else { statusText = statusContent; gameText = "<a href='" + gameUrl + gameContent + "'>" + gameContent + "</a>"; } $(logo).append(logoText); $(name).append(nameText); $(status).append(statusText); $(game).append(gameText); } ); } //end add_row //for each streamer in list for (i = 0; i < num_streamers; i++) { add_row(url + streamers[i] + "?callback=?"); } }); 
 @import url('https://fonts.googleapis.com/css?family=Play|Bungee+Shade'); header { font-family: "Bungee Shade", Arial, sans-serif; } body { background-color: #a895cc; } table { font-family: "Play", Arial, sans-serif; text-align: left; } .offline { background-color: #aeb2b2; } th { background-color: #cc949d; } td { background-color: #94ccc3; } tr:hover td { background-color: #b9d38d; } img { border-radius: 10px; } thead { background-color: #8e9cb2; } tbody { background-color: #afc0db; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script> <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" /> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="container text-center"> <header> <h1>Twitch Streamers via Twitch API</h1> <h2>Coming to you from Free Code Camp</h2> </header> <table class="table table-hover"> <thead> <tr> <th> Logo: </th> <th> Display Name: </th> <th> Streaming: </th> <th> Game/Activity: </th> </tr> </thead> <tbody id="streamers"> </tbody> </table> </div> 

PS row.className = 'offline'; PS row.className = 'offline'; will work instead of row.addClass("offline"); 将代替row.addClass("offline"); but that won't change any colour of row. 但这不会改变行的颜色。 You are getting complete row like this <tr><td></td><td></td>... using javascript function and not jquery that's why row.addClass("offline"); 您正在使用JavaScript函数而不是jquery来获得像<tr><td></td><td></td>...这样的完整行,这就是为什么row.addClass("offline"); is throwing error and thus further js don't run for that thus no data in row 正在抛出错误,因此进一步的js不会为此运行,因此行中没有数据

You looking at the correct code. 您正在查看正确的代码。 please do like this. 请这样做。 Just add this in your code 只需在您的代码中添加

row.className = 'offline';

Please see the updated code 请查看更新的代码

if (gameContent == null) {
      statusText = "Offline...";
      gameText = "Nothin'";
      row.className = 'offline';

this will change the color of the columns inside the row 这将更改行内列的颜色

tr.offline td{
    background-color: red;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何根据同一 WordPress 页面上的标题 class 名称更改 2 个表格行背景 colors - How to change 2 table row background colors based on a heading class names on the same WordPress Page 基于特定变量的 Bootstrap 上下文表 - Bootstrap contextual table based off certain variable 当数据表输入来自服务器的 JSON 数据时,更改 Google 图表栏颜色 - Change Google Chart bar colors when data table input is from JSON data from server 如何根据数据维度更改条形颜色? - How to change bars colors based on a data dimension? 根据部分列数据更改表行的背景颜色 - Change background color of table row based of part of column data HTML / CSS / JavaScript:如何根据数据更改表格行的颜色 - HTML/CSS/JavaScript: How to change color of table row based on data PolymerJs表根据数据(JSON)更改单元格的颜色 - PolymerJs table change color of cell based on data (JSON) Dojo 1.7 Datagrid根据隐藏列中的文本更改行颜色 - Dojo 1.7 datagrid change row colors based on the text in the hidden column 我想根据情况更改表格的行颜色 - I want to change row colors of a table according to the condition 从本地目录读取文件并更改HTML表行的颜色 - Read the file from local directory and change the colors of the row of HTML table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM