【问题标题】:How to change bootstrap-table row background?如何更改引导表行背景?
【发布时间】:2019-04-10 21:34:14
【问题描述】:

如果结果是“好”,我希望整行都变成绿色。但它不会改变任何东西。

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>

【问题讨论】:

  • 在你的小提琴中我看不到你什么时候运行rowStyle(row, index)

标签: javascript jquery html bootstrap-4


【解决方案1】:

你可以使用简单的jQuery代码来实现它,运行下面的sn-p

$('table tr').each(function() {
    if($(this).find('td:last-child').html() === 'GOOD'){
        $(this).css('background-color', 'green');
    }
  })
<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>

【讨论】:

    【解决方案2】:

    我只是更新了您的代码,只对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>

    【讨论】:

    • 我在 Bash 脚本中修改并执行后出现此错误。 “v.parentElement: command not found” Bash 脚本还包含 Html 代码。
    • 可以分享链接吗?谢谢
    • 我觉得应该转成JavaScript。但我不知道如何转换。
    • 您需要在$(document).ready 方法中添加我的jQuery 脚本。我还更新了上面的代码sn-p。谢谢
    • @leej0978 如果问题仍然存在,请通知我。谢谢
    【解决方案3】:

    先给table添加一个id,然后用&lt;tbody&gt;包裹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');
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-27
      • 2021-09-12
      • 1970-01-01
      • 2017-06-22
      • 1970-01-01
      • 2016-04-27
      • 1970-01-01
      • 2018-11-27
      相关资源
      最近更新 更多