【问题标题】:how to skip a column while using jsPDF如何在使用 jsPDF 时跳过一列
【发布时间】:2016-02-25 03:02:13
【问题描述】:

我正在尝试使用 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”的列,因为没有可供打印的文本。

虽然对于此表,最好在该列中放置适当的文本而不是图标,但对于其他表中的某些列,我仍然需要此忽略功能。

这是我现在得到的:

谢谢。

【问题讨论】:

    标签: javascript html jspdf


    【解决方案1】:

    在将源变量传递给 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);
    });
    

    这将删除类名为“no_export”的任何项目,甚至使用“省略行”删除整行。

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

    【讨论】:

      【解决方案2】:

      我创建了一种方法来获取 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 包含列信息。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-08-14
        • 2018-06-23
        • 2017-08-13
        • 2019-03-08
        • 1970-01-01
        • 2019-07-19
        相关资源
        最近更新 更多