使用 VueJS 使用 3rd 方库打印元素

man*_*kur 13 javascript css printing vue.js vuejs2

我正在处理 HTML 表格,并html-to-paper在 vue.js 中使用该表格将该表格打印到打印机,我正在做的是点击添加创建一个新行,然后点击打印我正在尝试打印表格,但它没有任何仅显示空单元格的数据

代码 App.vue

    <template>
  <div id="app">
    <button type="button" @click="btnOnClick">Add</button>

    <div id="printMe">
      <table class="table table-striped table-hover table-bordered mainTable" id="Table">
    <thead>
      <tr>

        <th class="itemName">Item Name</th>
        <th>Quantity</th>
        <th>Selling Price</th>
        <th>Amount</th>
      </tr>
    </thead>
    <tbody>
      <tr v-for="(tableData, k) in tableDatas" :key="k">

        <td>
          <input class="form-control" readonly v-model="tableData.itemname" />
        </td>
        <td>
          <input class="form-control text-right" type="text" min="0" step=".01" v-model="tableData.quantity" v-on:keyup="calculateQty(tableData)" />
        </td>
        <td>
          <input class="form-control text-right" type="text" min="0" step=".01" v-model="tableData.sellingprice" v-on:keyup="calculateSPrice(tableData)" />
        </td>
        <td>
          <input readonly class="form-control text-right" type="text" min="0" step=".01" v-model="tableData.amount" />
        </td>
      </tr>
    </tbody>
  </table>
    </div>
    <button @click="print">Print</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableDatas: []
    }

  },
  methods: {
    btnOnClick(v) {
      this.tableDatas.push({
        itemname: "item",
        quantity: 1,
        sellingprice: 55,
        amount: 55
      });
    },
     print() {
      this.$htmlToPaper('printMe');
    }
  }
};
</script>

<style>
</style>
Run Code Online (Sandbox Code Playgroud)

主文件

 import Vue from "vue";
import App from "./App.vue";
import VueHtmlToPaper from "vue-html-to-paper";

Vue.config.productionTip = false;
Vue.use(VueHtmlToPaper);

new Vue({
  render: h => h(App)
}).$mount("#app");
Run Code Online (Sandbox Code Playgroud)

这里是codesandbox中的工作代码

请检查运行代码

根据赏金编辑

我必须用“html-to-paper”来做,问题是我无法为我的元素提供样式以使用 @media print

  • 答案ux.engineer很好,但导致浏览器问题 crome 和 firefox 由于安全问题阻止了它

请检查代码沙箱例如这是我的完整代码,我正在尝试提供样式但没有发生

  • html-to-print插件使用 window.open 所以当我点击打印时它不会将样式带到新页面。
  • 这就是我为什么不采用媒体风格的原因,我如何在 window.open 上覆盖我的风格

我正在使用 print-nb但由于某些安全原因它在浏览器上不起作用这就是使用 print-nb 时所说的

ux.*_*eer 10

不幸的是,你不能利用这个mycurelabs/vue-html-to-paper mixin 包来利用 Vue 的数据绑定,正如包作者在此处所述

但是,我通过将此处使用的包切换为Power-kxLee/vue-print-nb 指令来创建解决方法。

这是一个工作示例:https : //codesandbox.io/s/kind-hypatia-inutd

附注。在类似的软件包之间进行选择有时可能会很棘手。应该评估 repo 的使用情况和活动​​统计数据,例如:在首页上使用、观察和启动,然后检查打开/关闭的问题和活动/关闭的拉取请求,然后转到洞察力检查脉冲(1 个月),以及代码频率。

在这两者之间,我会选择vue-print-nb,因为它更受欢迎和使用更积极。也因为我更喜欢使用指令而不是 mixin

至于其他答案,为此目的继续使用 vue-html-to-paper 将需要那种hacky解决方案......因为该指令开箱即用。

https://github.com/mycurelabs/vue-html-to-paper

https://github.com/Power-kxLee/vue-print-nb


ark*_*uuu 6

正如其他人所提到的,这对于您使用的包是不可能的,因为v-model打印时不存在绑定数据。所以你需要在你的 html 中静态地获取这些数据。来源

一种解决方法是使用输入占位符:

添加对表的引用

<tbody ref="tablebody">
Run Code Online (Sandbox Code Playgroud)

这允许您在方法中选择此元素。现在更改打印方法:

<tbody ref="tablebody">
Run Code Online (Sandbox Code Playgroud)

然后也许可以用 css 设置占位符的样式,因为它默认看起来是灰色的。

我首先尝试以某种方式重置输入的值,例如input.value = input.value,但不幸的是这不起作用。

在此处更新您的代码