I am using Node.js with Express as a server. The user can upload a .CSV file with data. I'm parsing these data and store this in a main array composed of arrays (each line = one array). For now, I'm rendering a page (made with Pug) with this variable.
res.render('index', { rows });
The page is rendering as I would like to the user. However, I'd like to save the rendered "index" in a HTML file. The goal is to convert the HTML file into a PDF (which already works as a result of previous tests with static HTML).
Is it possible to save the rendered page as a HTML file? I'd like to take advantage of the feature of passing variable through the render function.
Another solution would be to generate by myself the HTML file, and do my own logic inside the Node.js route. That seems a long and useless solution since the render function do it well. I just need to save it instead of rendering it.
You could call PUG directly to trigger a render and then save the produced file (https://pugjs.org/)
Related
I am moving all my javascript out of my views, and into separate .js files. But I am facing one issue.
Some javascript references a model parameter:
var data = #Html.Raw(Model.RunningTotalData)
#Html, and probably Model... isn't available in the js files.
One solution might be, in the <div> that the code is working with, create a data- type field? And populate it on the view, and then the js code somehow reads that data?
At the moment, the js is rendering a graph on a div, defined as:
<div id="allbalancediv" style="width: 100%;"></div>
Could I add a data-xxx there, and then refer to that data field in my js code? I'm not sure if that's the right way to go, or if there's a better option.
You obviously can't render server side content into static javascript files. But you can still render a script tag that sets up data for your JS files.
// View
<script>var data = #Html.Raw(Model.RunningTotalData)</script>
<script src="/frontend.js"></script>
Or a data attribute that gives your JS code an id to fetch via AJAX from a JSON api would work too.
I'm having a file which contains a couple of space separated (or comma separated, it will be editable) serial-numbers (all unique).
Now through my Oracle APEX I get one serial-number. My goal is to check if this serial code which could be passed on to a parameter of obtained through $v('P#_SERIAL_ID') is equal to one of the serial-numbers in the file.
Is this even possible within Javascript? If so, is there an existing function/code to achieve my goal?
Stackoverflow questions that didn't help me but look alike:
Javascript-read-file-contents
C#-reading-and-editing-file
Java-string-comparison
You can do this without JavaScript. Import your file into Apex through an Data Load Wizard Page so you will have the content of your file into a table. This way you can compare your information through some kind of SQL validation.
If you don`t like the Data Load Wizard Page you can add a file browse item on a simple page that will take your file and save it as a blob into the database. From there you can again process the file and compare the values.
I am trying to implement D3 graphs and charts in a FileMaker solution. I am stuck on loading a JSON file properly to be used in D3 code displayed in the webviewer.
I am loading all JS libraries into global variables ($$LIB.D3.JS, $$LIB.JQUERY.JS, etc.). Then I am placing the HTML code on the layout (giving an object name, i.e. html). The web viewer grabs the HTML code (from a text box on the layout) and JS code (from global variables) to render the page. This all works fine. (I am using this method detailed here: https://www.youtube.com/watch?v=lMo7fILZTQs)
However, the D3 code I have uses the getJSON() function to get a JSON, parse the data and create the visualization. I can't figure out a way to get the JSON file as a file from within FileMaker. I could put the content of the JSON file into a FileMaker variable and feed that into the HTML, but I then would not be able to use getJSON(). I would have to redo the D3 code to get the data from a JS variable and parse the data from the variable.
Is there a way for me to load a JSON file so FileMaker could use it to render the visualization properly in the WebViewer.
You have two options.
1. Calc the JSON into the HTML as you mentioned. Your right you will have to change how you load the JSON with d3. But its not tough. When you load the JSON from disk, using something like d3.json('/data.json', callback) you are just loading the json and then giving it to the callback function. If the JSON is in the HTML page in a something like var embeddedJSON You can just call the callback directly with the embeddedJSON like
callback (embeddedJSON)
Your code may look more like this.
d3.json('/data.json', function(data){
// bunch of d3 code
})
The callback in this case is an anonymous function. You can change it like this.
var render = function(data){
// bunch of d3 code
})
// then call render with your json variable that you embedded into the html
render ( embeddedJSON )
That will work just fine.
2. Export the html page to the temp directory, and export the json file with the data into right next to it. Then display the html using a file://url. In this case you can use d3.json(/data.json, callback ) and that will work just fine too.
Each of these methods have their pros and cons, but they both work.
In most cases, the best practice for integrating javascript or other assets in a Webviewer is to push assets to the temp directory (get this using GetTemporaryPath() in FileMaker), you can then export assets directly to named files. Once this is done, you can reference these files in your code using the file:// protocol.
This has numerous advantages over older methods, such as loading everything into global variables. One of the biggest of these is that provided you load your JSON into a discrete file and don't "pollute" any other files with FileMaker data, you can work entirely in the code environment of your choice, then simply move web JavaScript libraries, html, CSS, and other assets directly into your FileMaker solution.
I'm working on a project where it has a number of pages. Each page displays 10 rows where the layout that is using for each page is different. Until now I had the html code of each row in a javascript code and based on the page's url I was using the appropriate html code (if statement). The if statement is inside into a loop which is looping based on the number of rows. The results of the rows are coming from an ajax method. Now I want somehow to separate it so it can be more easily for me to maintain it, basically to remove the html code from the javascript and keep each row's html code into a different file.
Note: the Ajax is in a given time, is sending automatically requests to the php file for any new rows.
One solution which I came out is that I can use the php to create a variable with the html code .
Second solution is to create an array of each record with the html code and then pass it to jquery to print it.
Both solutions I don't know if are good solutions and can help me to maintain the project in the future.
You might consider a template library such as handlebars to help with templating. Frameworks such as AngularJS and Ember also excel at solving these kinds of problems.
Your Web Services API should be returning JSON though, not HTML fragments. Let the client build the DOM, and let the server focus on data.
You should return structured data (see JSON for example) to your AJAX request. This way, you can support multiple interfaces (e.g., a website, an application): each interface will get only the data, and will handle the rendering as it needs.
In your example, you ask for data via an AJAX request, your server responds with a JSON-structured response. JQuery reads it and converts it to javascript array thanks to jQuery.getJSON. With your array, you loop through each element and insert html elements into the webpage.
You have two options:
If your HTML templates is not changing frequently, the best way is to define html templates in your HTML structure using some java script template library (eg. Handlebars) and fill it with data from your AJAX (JSON) requests.
If your HTML templates change frequently or depends on some conditions (data) in row, you should create PHP partial views which generate proper html structure already filled with data.
For many rows it is better idea to create whole table server side to reduce requests.
I am writing an app that must generate a PDF file based on the HTML generated by AngularJS. I do not want to display this output to the user. I want to capture the rendered HTML to a string and send it to a server, where a service there will receive the HTML source and create the PDF file.
I understand that one way to grab rendered HTML is through accessing the element's innerHTML property after compiling it in a directive. But how can I be certain that there are no further digests that need to take place? Also, can this method work without writing the results to the DOM? -- Or, at least, without displaying the results to the user? Thanks!