如何在使用jsPDF时跳过列

时间:2016-02-25 03:02:14

标签: javascript html5 jspdf

我正在尝试使用jsPDF将HTML表格导出为PDF,但我有一个重叠列问题。我必须跳过一些专栏,但不知道该怎么做。

这是我的表:

<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”的列,因为没有要打印的文本。

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

这是我现在得到的: enter image description here

谢谢。

2 个答案:

答案 0 :(得分:1)

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

例如:

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);
});

这将删除任何类名为&#34; no_export&#34;的项目。甚至整行使用&#34;省略 - 行&#34;。

这是一个小提示,显示正在导出的表格被忽略的最后一列:fiddle

答案 1 :(得分:0)

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

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;
    }

该方法接收该表作为参数,您可以使用jQuery选择该表并将其发送给它。

地铁的第二个是要忽略的列号,如果您不发送任何消息,则不会忽略任何内容

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

要访问JSON,table.headers包含标题,而table.rows包含列的信息。