亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將數據表條件格式化為特定列中的特定值

將數據表條件格式化為特定列中的特定值

臨摹微笑 2023-08-10 15:40:40
所以我有以下模板: {% load static %} <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous"> <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script> <script src="https://cdn.jsdelivCSAT.net/npm/[email protected]/dist/umd/poppeCSAT.min.js" integrity="sha384-9/reFTGAW83EW2RDu2S0VKaIzap3H66lZH81PoYlFhbGU+6BZp6G7niu735Sk7lN" crossorigin="anonymous"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js" integrity="sha384-B4gt1jrGC7Jh4AgTPSdUtOBvfO8shuf57BaghqFfPlYxofvL8/KUEfYiJOMMV+rV" crossorigin="anonymous"></script> <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script> <script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/1.10.22/js/jquery.dataTables.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"></script> <script src="https://cdn.datatables.net/1.10.22/css/jquery.dataTables.min.css"></script> <div class="table-responsive-sm" style="overflow:scroll">  <table class="table table-striped  table-bordered table-hover" id="example">  <thead class="thead-dark">    <tr>        <th colspan="12" scope="colgroup"></th>    </tr>    <tr>        <th>A</th>        <th>B</th>        <th>C</th>        <th>D</th>        <th>E</th>        <th>F</th>    </tr>    </tbody>    <tfoot>    <tr>        <th>A</th>        <th>B</th>        <th>C</th>        <th>D</th>        <th>E</th>        <th>F</th>        <th>F</th>    </tr></tfoot></table></div>在 C、D 和 E 列中顯示值,為:R、G、Y(紅、綠、黃)
查看完整描述

1 回答

?
千巷貓影

TA貢獻1829條經驗 獲得超7個贊

我知道執行此操作的最簡單方法是使用該columnDefs.render選項以及一些支持 CSS。


這是 CSS,在我的例子中,我將其包含在<head>HTML 頁面的部分中:


<style>


  .bg_red {

    background-color: red !important;

  }


  .bg_yellow {

    background-color: yellow !important;

  }


  .bg_green {

    background-color: green !important;

  }


</style>

!important請注意CSS 中的使用。這有點粗糙,但意味著此 CSS 會覆蓋 DataTables 可能應用的任何行陰影(例如所謂的“斑馬條紋”)。


我的測試表只是以下硬編碼的 HTML:


    <table id="example" class="display dataTable cell-border" style="width:100%">

        <thead>

            <tr><th>A</th><th>B</th><th>C</th></tr>

        <thead>

        <tbody>

            <tr><td>R</td><td>R</td><td>Y</td></tr>

            <tr><td>Q</td><td>X</td><td>G</td></tr>

        </tbody>

    </table>

DataTables函數如下:


<script type="text/javascript">


  $(document).ready(function() {


    $('#example').DataTable( {

      columnDefs: [ {

        targets: [1, 2], 

        "render": function ( data, type, row, meta ) {

          var table = $('#example').dataTable().api();

          if (type === 'display') {

            var node = table.cell(meta.row, meta.col).nodes().to$();

            if ( data === 'R' ) {

              node.addClass('bg_red');

            } else if ( data === 'G' ) {

              node.addClass('bg_green');

            } else if ( data === 'Y' ) {

              node.addClass('bg_yellow');

            }

          }

          return data;

        }

      } ]

    } );


  } );


</script>

該targets: [1, 2]選項意味著渲染邏輯將僅適用于我的表中的第二列和第三列(第一列的索引為零)。


該type === 'display'選項意味著我們只檢查每個單元格的顯示值。這可能與過濾器和排序值不同。在我的簡單情況下,沒有區別,但明確處理這個問題是個好主意。


渲染邏輯將相關的類名添加到表的<td>標簽中 - 這是 DOM 的一部分,而不是 DataTables 本身的一部分。這就是我們訪問每個單元格node對象的原因。


使用此render功能還意味著即使您對表格進行排序和篩選,格式也會遵循數據。否則,在排序/過濾后,錯誤的單元格將突出顯示。


查看完整回答
反對 回復 2023-08-10
  • 1 回答
  • 0 關注
  • 139 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號