【发布时间】:2020-04-08 14:46:00
【问题描述】:
我正在处理 HTML 表格,并在 vue.js 中使用 html-to-paper 将该表格打印到打印机,我正在做的是点击添加创建一个新行,然后点击打印我正在尝试打印表,但它没有获取任何仅显示空单元格的数据
代码 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>
main.js
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");
这里是codesandbox中的工作代码
根据赏金编辑
我必须使用“html-to-paper”来完成,问题是我无法使用@media print 为我的元素赋予样式以进行打印
-
ux.engineer的回答很好,但由于安全问题导致浏览器问题,crome 和 firefox 阻止了它
Please check code sandbox 例如这是我的完整代码,我正在尝试提供样式但没有发生
-
html-to-print插件使用 window.open,因此当我单击打印时,它不会将样式带到新页面。 - 这就是为什么它不采用媒体样式的原因,我如何在 window.open 上覆盖我的样式
【问题讨论】:
-
manish thakur 请查看我的新答案(赏金宽限期将在 4 小时后结束,如果没有奖励,它将浪费掉)
标签: javascript css vue.js printing vuejs2