【问题标题】:jsPDF + Autotable - add line under each rowjsPDF + Autotable - 在每行下添加行
【发布时间】:2018-04-02 11:39:38
【问题描述】:

是否可以在 jspd-autotable 的每一行下添加一行?即不是每个单元格的边框,只是每个单元格的底部边框。

【问题讨论】:

    标签: jspdf jspdf-autotable


    【解决方案1】:

    我们可以使用jspdf autotable和jspdf line方法的钩子“willDrawCell”来指定任何边边框。

    例子:

    doc.autoTable({
      willDrawCell: function(data) {
        // add borders around the head cells
        if (data.row.section === "head") {
          doc.setDrawColor(0, 0, 0); // set the border color
          doc.setLineWidth(0.5); // set the border with
    
          // draw bottom border
          doc.line(
            data.cell.x,
            data.cell.y + data.cell.height,
            data.cell.x + data.cell.width,
            data.cell.y + data.cell.height
          );
          // draw top border
          doc.line(
            data.cell.x + data.cell.width,
            data.cell.y,
            data.cell.x,
            data.cell.y
          );
          // draw left border
          doc.line(
            data.cell.x,
            data.cell.y + data.cell.height,
            data.cell.x,
            data.cell.y
          );
          // draw right border
          doc.line(
            data.cell.x + data.cell.width,
            data.cell.y,
            data.cell.x + data.cell.width,
            data.cell.y + data.cell.height
          );
        }
      },
    });

    还可以为某些特定的单元格添加边框:

    doc.autoTable({
      willDrawCell: function(data) {
        // add borders around the head cells
        if (data.row.section === "head" && data.column.dataKey === "qty") {
          doc.setDrawColor(255, 255, 0); // set the border color
          doc.setLineWidth(0.5); // set the border with
    
          // draw bottom border
          doc.line(
            data.cell.x,
            data.cell.y,
            data.cell.x + data.cell.width,
            data.cell.y
          );
        }
      },
    });

    参考:https://github.com/simonbengtsson/jsPDF-AutoTable/issues/651#issuecomment-626272061

    【讨论】:

      【解决方案2】:

      最后我设法通过隐藏所有边框并在每行之间添加一个假行来做到这一点。然后我将每个奇数行(不包括标题)的高度设置为一个较小的值,并将背景颜色设置为黑色。有点破解,但它可以工作并且看起来不错。

      【讨论】:

      • 请至少提供一个示例说明如何执行此操作。
      • 请至少提供示例或代码。我们也面临同样的问题。
      • 我提供的答案提供了有关如何解决此问题的描述性步骤。您要求的是我为您完成工作。我建议您阅读 jsPDF 的文档,了解如何执行上述解决方案的每个项目。
      猜你喜欢
      • 1970-01-01
      • 2018-08-31
      • 1970-01-01
      • 2016-05-24
      • 1970-01-01
      • 1970-01-01
      • 2017-11-27
      • 1970-01-01
      • 2017-06-19
      相关资源
      最近更新 更多