【问题标题】:How to change font color selected row column dataTable如何更改选定行列数据表的字体颜色
【发布时间】:2017-12-26 21:46:01
【问题描述】:

如何更改“受众名称”列的颜色?

我想将文本“一、二、三、四”更改为带下划线的蓝色。

如何做到这一点?我只想更改颜色“受众名称”行列,而不是所有行。

jQuery :

 $(document).ready(function() {
    var tabble = $('#table1').dataTable({
      "ajax": "https://api.myjson.com/bins/sk48v",
      "columns": [{
        "data": "name"
      }, {
        "data": "subtype"
      }, {
        "data": "approximate_count"
      }, {
        "data": "time_created"
      }],
      "order": [4, "desc"],
      "bStateSave": true,
      "stateSave": true,
      "bPaginate": false,
      "bLengthChange": false,
      "bFilter": false,
      "bInfo": false,
      "bAutoWidth": false
    });
  });
table.dataTable tbody th, table.dataTable tbody td {
    color: blue;
    padding: 8px 10px;
    text-decoration: underline;
    cursor: pointer;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>

<script src="https://cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js"></script>
<link href="https://cdn.datatables.net/1.10.16/css/jquery.dataTables.min.css" rel="stylesheet"/>

<table id="table1" class="table table-bordered table-hover">
    <thead>
        <tr>
            <th>Audience Name</th>
            <th>Type</th>
            <th>Size</th>
            <th>Date Created</th>
        </tr>
    </thead>

</table>

没有进行 sn-p 演示

JSFiddle

【问题讨论】:

  • JSFiddle的控制台出现dataTables错误,没有一、二、三、四... Uncaught TypeError: Cannot read property 'aDataSort' of undefined
  • 我已经更新了jsfiddle,你需要点击“运行”。
  • 我的小提琴怎么了?为什么打开网址时不显示?
  • 各位,为什么我的行不显示?

标签: javascript jquery html css


【解决方案1】:

您可以像这样在列对象中使用className 属性

JAVASCRIPT

$(document).ready(function() {
  var tabble = $('#table1').dataTable({
    "ajax": "https://api.myjson.com/bins/sk48v",
    "columns": [{
      "data": "name",
       "className": "blue"
    }, {
      "data": "subtype"
    }, {
      "data": "approximate_count"
    }, {
      "data": "time_created"
    }]
  });
});

HTML

<table id="table1" class="table table-bordered table-hover">
  <thead>
    <tr>
      <th>Audience Name</th>
      <th>Type</th>
      <th>Size</th>
      <th>Date Created</th>
    </tr>
  </thead>

</table>

CSS

.blue {
  color: blue;
  text-decoration : underline;
}

这是工作的JSFIDDLE

【讨论】:

  • 如何只制作蓝色的行表,而不是列名。
  • JQuery 数据表中仍然不允许使用此功能datatables.net/forums/discussion/24482/…
  • 这是答案table.dataTable thead th:hover { text-decoration: none; }
【解决方案2】:

您还可以为列添加自定义类并根据需要制作样式。

$(document).ready(function() {
  var tabble = $('#table1').dataTable({
    "ajax": "https://api.myjson.com/bins/sk48v",
    "columns": [{
      "data": "name",
      "class": "custom-class" //Class here
    }, {
      "data": "subtype"
    }, {
      "data": "approximate_count"
    }, {
      "data": "time_created"
    }],
    "order": [4, "desc"],
    "bStateSave": true,
    "stateSave": true,
    "bPaginate": false,
    "bLengthChange": false,
    "bFilter": false,
    "bInfo": false,
    "bAutoWidth": false
  });
});

在css中

.custom-class {
    color: blue;
    padding: 8px 10px;
    text-decoration: underline;
    cursor: pointer;
    }

【讨论】:

    【解决方案3】:
    table.dataTable tbody tr.selected {
        color: white;
        background-color: #eeeeee;
    }
    

    【讨论】:

      【解决方案4】:

      你有两个选择。您可以使用内联 css 或通过为该行列指定类名来使用外部 css。

      我在这里使用了内联 css。

      这里是 => WORKING DEMO

      <table id="table1" class="table table-bordered table-hover">
        <thead>
          <tr> 
            <th style="color:blue; text-decoration: underline;">Audience Name</th>
            <th>Type</th>
            <th>Size</th>
            <th>Date Created</th>
          </tr>
        </thead>
      
      </table>
      
      var tabble = $('#table1').dataTable({
          "ajax": "https://api.myjson.com/bins/sk48v",
          "columns": [{
            "data": "name"
          }, {
            "data": "subtype"
          }, {
            "data": "approximate_count"
          }, {
            "data": "time_created"
          }],
          "order": [4, "desc"],
          "bStateSave": true,
          "stateSave": true,
          "bPaginate": false,
          "bLengthChange": false,
          "bFilter": false,
          "bInfo": false,
          "bAutoWidth": false
        });
      });
      

      【讨论】:

        【解决方案5】:

        您可以这样做,使用该 CSS 创建一个 class,并在加载数据表后将 class 添加到其中。

        $(document).ready(function() {
            var tabble = $('#table1').dataTable({
              "ajax": "https://api.myjson.com/bins/sk48v",
              "columns": [{
                "data": "name"
              }, {
                "data": "subtype"
              }, {
                "data": "approximate_count"
              }, {
                "data": "time_created"
              }],
              "order": [4, "desc"],
              "bStateSave": true,
              "stateSave": true,
              "bPaginate": false,
              "bLengthChange": false,
              "bFilter": false,
              "bInfo": false,
              "bAutoWidth": false
            });
        });
        
        $(document).find("#table1 thead th:first-child, #table1 td:first-child").addClass('test');
        .test {
            color: blue;
            padding: 8px 10px;
            text-decoration: underline;
            cursor: pointer;
        }
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
        <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
        
        <script src="https://cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js"></script>
        <link href="https://cdn.datatables.net/1.10.16/css/jquery.dataTables.min.css" rel="stylesheet"/>
        
        <table id="table1" class="table table-bordered table-hover">
            <thead>
                <tr>
                    <th>Audience Name</th>
                    <th>Type</th>
                    <th>Size</th>
                    <th>Date Created</th>
                </tr>
            </thead>
        
        </table>

        【讨论】:

        • 我已经为thtd 更新了表的第一个字段的代码,如果您不需要两者,请从代码中删除, #table1 td:first-child
        【解决方案6】:
        <table id="table1" class="table table-bordered table-hover">
          <thead>
            <tr>
              <th style="color:white">Audience Name</th>
              <th>Type</th>
              <th>Size</th>
              <th>Date Created</th>
            </tr>
          </thead>
        
        </table>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2015-04-16
          • 2011-07-28
          • 2021-09-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多