首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何改变bootstrap-table行背景?

如何改变bootstrap-table行背景?
EN

Stack Overflow用户
提问于 2019-04-10 16:11:28
回答 3查看 3.8K关注 0票数 2

如果结果是"good",我希望整行变成绿色。但这不会改变任何事情。

https://jsfiddle.net/Tonato_/tnw3j5p2/7/

下面是代码。

我根本不知道问题出在哪里。我想我都做对了。

代码语言:javascript
复制
function rowStyle(row, index) {
  const obj = {};
  var classes = ['active', 'success', 'info', 'warning', 'danger'];
  if (Object.keys(row).map(key => row[key]).some(value => String(value).includes('BAD'))) {
    return Object.assign({}, obj, {
      classes: 'danger'
    });
  }
  if (Object.keys(row).map(key => row[key]).some(value => String(value).includes('GOOD'))) {
    return Object.assign({}, obj, {
      classes: 'success'
    });
  }
  return obj;
}
代码语言:javascript
复制
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha/css/bootstrap.min.css" rel="stylesheet" />
<table data-row-style="rowStyle" class="table table-bordered table-hover thead-dark thead-inverse">
  <thead>
    <tr>
      <th scope="col" style="width: 8%" class="text-center">Number</th>
      <th scope="col" style="width: 20%" class="text-center">Name</th>
      <th scope="col" style="width: 61%" class="text-center">Present</th>
      <th scope="col" style="width: 10%" class="text-center">Result</th>
    </tr>
  </thead>
  <tr>
    <td class="text-center">01</td>
    <td class="text-center">$title01</td>
    <td>$present01</td>
    <td scope="row" class="text-center">GOOD</td>
  </tr>
  <tr>
    <td class="text-center">02</td>
    <td class="text-center">$title02</td>
    <td>$present02</td>
    <td scope="row" class="text-center">BAD</td>
  </tr>
  <tr>
    <td class="text-center">03</td>
    <td class="text-center">$title03</td>
    <td>$present03</td>
    <td scope="row" class="text-center">GOOD</td>
  </tr>
</table>

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2019-04-10 17:26:33

我只是用很少的jQuery更改来更新您的代码。试试这个,我希望它能帮到你。谢谢

代码语言:javascript
复制
$(document).ready(function() {
  $('.table tr td').each(function(i, v){
    if(v.textContent === 'GOOD') {
      $(v.parentElement).addClass('table-success');
    } else if(v.textContent === 'BAD') {
      $(v.parentElement).addClass('table-danger');
    }
  })  
})
代码语言:javascript
复制
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha/css/bootstrap.min.css" rel="stylesheet" />
<table data-row-style="rowStyle" class="table table-bordered table-hover thead-dark thead-inverse">
  <thead>
    <tr>
      <th scope="col" style="width: 8%" class="text-center">Number</th>
      <th scope="col" style="width: 20%" class="text-center">Name</th>
      <th scope="col" style="width: 61%" class="text-center">Present</th>
      <th scope="col" style="width: 10%" class="text-center">Result</th>
    </tr>
  </thead>
  <tr>
    <td class="text-center">01</td>
    <td class="text-center">$title01</td>
    <td>$present01</td>
    <td scope="row" class="text-center">GOOD</td>
  </tr>
  <tr>
    <td class="text-center">02</td>
    <td class="text-center">$title02</td>
    <td>$present02</td>
    <td scope="row" class="text-center">BAD</td>
  </tr>
  <tr>
    <td class="text-center">03</td>
    <td class="text-center">$title03</td>
    <td>$present03</td>
    <td scope="row" class="text-center">GOOD</td>
  </tr>
</table>

票数 1
EN

Stack Overflow用户

发布于 2019-04-10 19:03:30

您可以使用简单的jQuery代码来实现它,运行下面的代码片段

代码语言:javascript
复制
$('table tr').each(function() {
    if($(this).find('td:last-child').html() === 'GOOD'){
        $(this).css('background-color', 'green');
    }
  })
代码语言:javascript
复制
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table data-row-style="rowStyle" class="table table-bordered table-hover thead-dark thead-inverse">
	<thead>
	<tr>
		<th scope="col" style="width: 8%" class="text-center">Number</th>
		<th scope="col" style="width: 20%" class="text-center">Name</th>
		<th scope="col" style="width: 61%" class="text-center">Present</th>
		<th scope="col" style="width: 10%" class="text-center">Result</th>
	</tr>
	</thead>
	<tr>
		<td class="text-center">01</td>
		<td class="text-center">$title01</td>
		<td>$present01</td>
		<td scope="row" class="text-center">GOOD</td>
	</tr>
	<tr>
		<td class="text-center">02</td>
		<td class="text-center">$title02</td>
		<td>$present02</td>
		<td scope="row" class="text-center">BAD</td>
	</tr>
	<tr>
		<td class="text-center">03</td>
		<td class="text-center">$title03</td>
		<td>$present03</td>
		<td scope="row" class="text-center">GOOD</td>
	</tr>
  </table>

票数 1
EN

Stack Overflow用户

发布于 2019-04-10 16:21:00

首先将id添加到表中,然后用<tbody>包装tr。

代码语言:javascript
复制
<table id='my-table' data-row-style="rowStyle" class="table table-bordered table-hover thead-dark thead-inverse">
    <thead>
    <tr>
      <th scope="col" style="width: 8%" class="text-center">Number</th>
      <th scope="col" style="width: 20%" class="text-center">Name</th>
      <th scope="col" style="width: 61%" class="text-center">Present</th>
      <th scope="col" style="width: 10%" class="text-center">Result</th>
    </tr>
    </thead>
    <tbody>
      <tr>
      <td class="text-center">01</td>
      <td class="text-center">$title01</td>
      <td>$present01</td>
      <td scope="row" class="text-center">GOOD</td>
    </tr>
    <tr>
      <td class="text-center">02</td>
      <td class="text-center">$title02</td>
      <td>$present02</td>
      <td scope="row" class="text-center">BAD</td>
    </tr>
    <tr>
      <td class="text-center">03</td>
      <td class="text-center">$title03</td>
      <td>$present03</td>
      <td scope="row" class="text-center">GOOD</td>
    </tr>
    </tbody>
  </table>

然后,通过jQuery使用可以遍历表中tbody的每个tr。然后根据您的结果,将css添加到该行。

代码语言:javascript
复制
$('#my-table tbody tr').each(function(i, item){
  var result = $(item).find('td').last().text();
  if(result === 'GOOD') $(item).css('background-color', 'green');
  else $(item).css('background-color', 'red');
})
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55607733

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档