簡體   English   中英

更改行背景顏色pdf導出數據表

[英]Change row background color pdf export dataTable

我需要檢查一列的值是否為負數以及在導出為 PDF 時,如果是,則使整個單元格變為紅色或 text-decoration: line-through。

但是我不知道如何訪問元素來進行這樣的修改。

我可以在瀏覽器中顯示背景時修改背景,但在導出時效果不同。

我使用http://datatables.net/

    {
        extend: 'pdfHtml5',
        footer: true,
        text: 'PDF',
        header: true,
        title: t,
        orientation: 'landscape',
        exportOptions: {
            rows: function ( idx, data, node ) {
                //node.attributes.style = {background-color: "#000";};
                //console.log(node.attributes.style);
                //return true;
                // return data[2] === 'London' ?
                // true : false;
            }
        },
        customize: function(doc) {

            doc.content.splice( 1, 0, {
                margin: [ 0, 0, 0, 0 ],
                alignment: 'center',
                image: base64
            } );
            doc.defaultStyle.fontSize = 10;
            doc.pageMargins = [20,10,10,10];
            doc.styles.tableHeader.fontSize = 14;
            doc.styles.title.fontSize = 12;
            // Remove spaces around page title
            doc.content[0].text = doc.content[0].text.trim();
        } 

    },
    {
        extend: 'print',
        footer: true,
    }

    ],

    fnRowCallback: function( nRow, aData, iDisplayIndex, iDisplayIndexFull ) {
        var valor = aData[3].split('$');
        //console.log(teste);
        if (parseInt(valor[1])<0) { 
            $(nRow).css('background-color', '#e96464')
        }else{
            $(nRow).css('background-color', '#cdedc1')
        }
    }

});

我剛剛在 DataTables論壇上發布了一個類似問題的答案。 解決方法是在自定義回調中修改PDF文檔中的表格。

buttons: [
  {
    extend: "pdfHtml5",
    customize: function(doc) {
      age = table.column(3).data().toArray();
      for (var i = 0; i < age.length; i++) {
        if (age[i] < 40) {
          doc.content[1].table.body[i+1][3].fillColor = 'blue';
        }
      }
    }
  }
]

活生生的例子在這里。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM