如何更改引导表行背景?

时间:2019-04-10 08:11:28

标签: javascript jquery html bootstrap-4

如果结果为“好”,我希望整行变为绿色。但这并没有改变任何东西。

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

这是代码。

我根本没有意识到问题出在哪里。我想我做的一切正确。

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;
}
<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>

3 个答案:

答案 0 :(得分:1)

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

5.8.5
$('table tr').each(function() {
    if($(this).find('td:last-child').html() === 'GOOD'){
        $(this).css('background-color', 'green');
    }
  })

答案 1 :(得分:0)

首先在表中添加一个id,然后用<tbody>包装tr。

<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添加到该行。

$('#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');
})

答案 2 :(得分:0)

我只需进行少量jQuery更改即可更新您的代码。试试这个,希望对您有所帮助。谢谢

$(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');
    }
  })  
})
<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>