JS Knockout with HTML to PDF Converter

I searched everywhere for an answer to this question, and maybe the answer is this: it does not exist, but hopes that someone has some kind of magic.

I use Knockout to link multiple forms on a page. It all works fantastic. However, trying to submit these forms to a PDF converter was a daunting task.

Rendered HTML is still displayed Knockout-y. I tried to get the processed DOM using $ ('body'). Html (), but I still get HTML tags with data binding attributes. Is there a way to get the final visualized clean HTML code to go to the PDF converter?

+4
source share
1 answer

You can capture processed elements before adding them to the DOM in the afterRender event. See http://knockoutjs.com/documentation/template-binding.html#note_3_using_afterrender_afteradd_and_beforeremove for details.

If you need access to the final rendered elements, create a custom binding ( http://jsfiddle.net/nE7kK/ ):

<body data-bind="getRenderedElements: viewModel"> <ul data-bind="foreach: viewModel"> <li data-bind="text: name"></li> </ul> </body> viewModel = ko.observableArray([ { name: "Bungle", type: "Bear" }, { name: "George", type: "Hippo" }, { name: "Zippy", type: "Unknown" } ]); ko.bindingHandlers.getRenderedElements = { update: function (element, valueAccessor) { // use timeout so browser has time to render the elements setTimeout(function() { var html = $(element).html(); alert(html); }, 1000); } }; ko.applyBindings(viewModel); 

Note. If you use virtual elements, you must specify a knockout to allow getRenderedElements to bind access to these elements: http://knockoutjs.com/documentation/custom-bindings-for-virtual-elements.html p>

+2
source

Source: https://habr.com/ru/post/1432411/


All Articles