如何在使用 jsPDF 时跳过一列

how to skip a column while using jsPDF

我正在尝试使用 jsPDF 将 HTML table 导出为 PDF,但我遇到了列重叠问题。我必须跳过一些栏目,但不知道该怎么做。

这是我的 table:

<div class="wrapper wrapper-content">
    <div class="row">
        <div class="col-lg-12">
            <div class="ibox float-e-margins">
                <a class="btn btn-primary btn-xs pull-right m-r-xs" ng-click="toPDF()">PDF çıktısı al</a>
                <a class="btn btn-primary btn-xs pull-right m-r-xs" ng-click="toExcel()">Excel çıktısı al</a>
                <div class="ibox-content" id="table">
                    <table id="archiveTable" class="table table-responsive table-hover">
                        <thead>
                            <tr>
                                <th id="bypassme" class="text-center noExl">Şüpheli</th>
                                <th width="20">No</th>
                                <th>Plaka</th>
                                <th>Ad</th>
                                <th>Plaka Noktası</th>
                                <th>Sahip Türü</th>
                                <th>Tarih</th>
                            </tr>
                        </thead>
                        <tbody>
                            <tr class="clickable" ng-repeat="data in archivedata" ng-click="showArchiveDetails(data)">
                                <td ng-class="{'text-danger': data.dangerous}" class="text-center noExl">
                                    <span ng-if="data.dangerous" tooltip="Şüpheli"><i class="fa fa-exclamation-circle"></i></span>
                                </td>
                                <td>{{(shownCurrentPage-1)*resultsPerPage + $index + 1}}</td>
                                <td>{{data.plate}}</td>
                                <td>{{people[data.ownerType][data.ownerId].name}}</td>
                                <td>{{data.platePoints.pointName}}</td>
                                <td ng-switch="data.ownerType">
                                    <span ng-switch-when="1">{{labels.resident}}</span>
                                    <strong ng-switch-when="2">{{labels.guest}}</strong>
                                    <span ng-switch-when="3">Tanınmayan</span>
                                    <span ng-switch-when="4">Düzenli Ziyaretçi</span>
                                </td>
                                <td>{{data.lprDate | date:'dd/MM/yyyy HH:mm'}}</td>
                            </tr>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>

这里是 Javascript 代码:

$scope.toPDF = function(){
    var from = $filter('date')($scope.archive.from,'dd.MM.yyyy HH.mm');
    var to = $filter('date')($scope.archive.to,'dd.MM.yyyy HH.mm');
    var filename = "Arşiv - " + from + " - " + to + ".pdf";

    var pdf = new jsPDF('p', 'pt', 'letter');
    // source can be HTML-formatted string, or a reference
    // to an actual DOM element from which the text will be scraped.
    source = $('#table')[0];

    // we support special element handlers. Register them with jQuery-style 
    // ID selector for either ID or node name. ("#iAmID", "div", "span" etc.)
    // There is no support for any other type of selectors 
    // (class, of compound) at this time.
    specialElementHandlers = {
        // element with id of "bypass" - jQuery style selector
        '#bypassme': function (element, renderer) {
            // true = "handled elsewhere, bypass text extraction"
            return true
        }
    };
    margins = {
        top: 80,
        bottom: 60,
        left: 40,
        width: 522
    };
    // all coords and widths are in jsPDF instance's declared units
    // 'inches' in this case
    pdf.fromHTML(
    source, // HTML string or DOM elem ref.
    margins.left, // x coord
    margins.top, { // y coord
        'width': margins.width, // max width of content on PDF
        'elementHandlers': specialElementHandlers
    },

    function (dispose) {
        // dispose: object with X, Y of the last line add to the PDF 
        //          this allow the insertion of new lines after html
        pdf.save(filename);
    }, margins);
};

我希望在导出时跳过名为“Şüpheli”的列,因为没有要打印的文本。

虽然对于这个 table 最好在该列中放置一个适当的文本而不是图标,但我仍然需要在另一个 table 中的某些列中使用这种忽略功能。

这是我现在得到的:

谢谢。

您可以在将源变量传递给 fromHTML 之前编辑传递给它的内容。可以根据类名删除部分。

例如:

cln_source2 = source.cloneNode(true);
var deleterows = [];
$(cln_source2.childNodes[1].rows).each(function(ri, row){
    var deletecells = [];
    $(row.cells).each(function(ci, cell){
        if( $(cell).hasClass('no_export') )
            deletecells.push(ci);
    });
    $.each(deletecells.reverse(), function(i, ii){
        row.deleteCell(ii);
    });
    if($(row).hasClass('omitted-row'))
            deleterows.push(ri);

});
$.each(deleterows.reverse(), function(i, ii){
    cln_source2.childNodes[1].deleteRow(ii);
});

这将使用 "omitted-row".

删除任何类名 "no_export" 甚至整行的项目

这是一个 fiddle,显示正在导出的 table 忽略了最后一列:fiddle

我创建了一种方法来获取 JSON 中 table 的数据,并可能忽略一列。方法如下

tableToJson(table, ignoreCell = -1) {

        let headers = [];
        for (let i = 0; i < table.rows[0].cells.length; i++) {
            if (i !== ignoreCell) headers[i] = $(table.rows[0].cells[i]).text();
        }

        let rows = [];
        for (let i = 1; i < table.rows.length; i++) {
            let tableRow = table.rows[i];
            let rowData = {};

            for (let j = 0; j < tableRow.cells.length; j++) {
                if (j !== ignoreCell) rowData[j] = $(tableRow.cells[j]).text();
            }
            rows.push(rowData);
        }

        table = [];
        table['headers'] = headers;
        table['rows'] = rows;

        return table;
    }

该方法接收 table 作为参数,您可以 select 和 jQuery 并发送它。

地铁的第二个是要忽略的列的编号,如果你不发送任何东西什么都没发生,你就不会忽略任何东西

let table = $('#table').get(0);
    table = this.tableToJson(table, 5);

访问JSON是,table.headers包含headers和table.rows列的信息。