My JS fiddle of the issue is here:
https://jsfiddle.net/taslar/krcfx4u5/9/
<div id="app">
<h1>
This should not print.
</h1>
<div id="pickList">
<h2>Todos:</h2>
<ol>
<li v-for="todo in todos">
<label>
<input type="checkbox"
v-on:change="toggle(todo)"
v-bind:checked="todo.done">
<del v-if="todo.done">
{{ todo.text }}
</del>
<span v-else>
{{ todo.text }}
</span>
<input v-model="todo.notes" />
</label>
</li>
</ol>
</div>
<button @click="print">print</button>
</div>
new Vue({
el: "#app",
data: {
todos: [
{ text: "Learn JavaScript", done: false, notes: 'What is JavaScript' },
{ text: "Learn Vue", done: false, notes: 'Doing' },
{ text: "Play around in JSFiddle", done: true, notes: 'Done' },
{ text: "Build something awesome", done: true, notes: '' }
]
},
methods: {
toggle: function(todo){
todo.done = !todo.done
},
print() {
var printWindow = window.open('', '', 'height=600,width=800');
printWindow.document.write('<html><head><title>Print This</title>');
printWindow.document.title = 'printthis.pdf';
printWindow.document.write('</head><body >');
printWindow.document.write('<div class="container">');
var pageElement = this.$el.querySelector('#pickList');
var html = pageElement.outerHTML;
printWindow.document.write(html);
printWindow.document.write('<br />');
printWindow.document.write('<br />');
printWindow.document.write('</div>');
printWindow.document.write('</body></html>');
printWindow.document.close();
printWindow.print();
}
}
})
The behavior I was hoping for was:
When the print button is hit the model values for the input elements would also be in the pick list.
This feels like is "should work", but the input elements are empty when I hit the print button.
I don't understand what mark-up I need to get Vue.js to write the model values on the input elements to the printWindow document.
Update (for those finding this through the internet):
Handling textarea tags was a simple matter of setting the v-html attribute.
Select tags I handled with this bit of code:
var selectElements = Array.prototype.slice.call(pageElement.querySelectorAll('select'));
selectElements.forEach(function (el) {
for (var index = 0; index < el.options.length; index++) {
option = el.options[index];
if (option.value === el.value) {
option.setAttribute('selected', true);
return;
}
}
});
todo.noteas html value attribute,<input v-model="todo.notes" value="todo.notes" />