I have a Grails app developed in 2.3.6
There's a GSP file with HTML and CSS elements in it, and that displays data in multiple tables with headers.
I want this data to be saved into a text file and save it. So basically what i want to do is, there will be a Export button in this GSP page, and when user clicks on it, it will download the text file with all the data from that GSP.
What i tried so far?
def textFile = {
response.setHeader('Content-Disposition', 'Attachment;Filename="textFile.txt"')
render view: 'textFile', contentType: 'text/plain'
}
The problem with above is, it saves not just data, but also HTML & CSS elements.
I don't want any HTML or CSS in the text file. Only data from GSP is needed.
Is there a simple way of doing it.
the answer is simple - you need another view withouth the html and css parts.
The rest of your code looks good. But Grails itself does not convert your view, it just sends the content type to the browser and the browser tries to diesplay the data according to the content type.
If you don't want to write a new view (in most cases, writing the new view is dead simple), you could write your own converter (something which strips the HTML and CSS from your file) by creating an afterView-Filter: http://grails.github.io/grails-doc/2.4.0/guide/single.html#filters
Hope that helps
Related
I have data written in QuillJS Editor, when I store that data it stores the data along with the HTML tags in my database.
For Example if I right this in the editor
This is an example.
It is stored as
<p>This is an example.</p>
Now when I'm trying to display these data in my Angular front-end HTML file it shows the HTML tags along with the data enclosed between them.
<p>This is an example.</p>
How will I only show the text enclosed in the tags and not the HTML tags?
To Display data in HTML that was entered by a user using QuillJs, you have to use QuillJs editor in read mode to display that data.
<quill-editor [(ngModel)]="yourNgModel" [readOnly]="isReadOnly" [modules]="{toolbar: false}">
</quill-editor>
I am working with a Grails 2.3.6 application.
I tried many different things to generate PDF, but most of them failed.
Is it possible to generate a text file of the contents of GSP file? Then have a button called EXPORT and when user clicks on that, the text file will download into there system.
Will it be possible to do that by passing the URL of GSP file?
This is quite straightforward. You just need to specify the response type (text/plain) in the render method from your controller. You can have plain text in the gsp file and use the tags where needed.
def textFile = {
response.setHeader('Content-Disposition', 'Attachment;Filename="textFile.txt"')
render view: 'textFile', contentType: 'text/plain'
}
textFile.gsp:
Dear ${name},
This is a text file.
As for pdf, I recommend the amazingly good grails rendering plugin.
With a grails app and from a local database, I'm returning some text in a xml format.
I can return it well formed in a <textarea></textarea> tag with the correct indenting (tabulation, line return,...etc.)
I want to go a bit further. In the text I'm returning, there are some <img/> tags and I'd like to replace those tag by the real images themselves.
I searched around and found no solution as of now. I understood that you can't add an image to a textarea (other then in a background), and if I choose a div tag, I won't have the indenting anymore (and therefore, harder to read)
I was wondering if using a <g:textField/> or an other tag from the grails library will do the trick. And if so, How can I append them to a page using jquery.
For example, how to append a <g:textField/> in jquery. It doesn't interpret it and I get this error
SyntaxError: missing ) after argument list [Break On This Error]...+doc).append("<input type="text" id="FTMAP_"+nb_sec+"" ...
And in my javascript file, I have
$("#FTM_"+doc).append("<g:textField id='FTMAP_"+nb_sec+"' ... />
Any possible solutions ?
EDIT
I did forget to mention that my final intentions are to be able to modify the text (tags included) and to have a nice and neat indentation so that it is the easiest possible for the end user.
You are asking a few different questions:
1. Can I use a single HTML tag to include images inside pre-formatted text.
No. You will have to parse the text and translate it into styled text yourself.
2. Is there a tag in the grails standard tags to accomplish this for me?
No.
3. How can I add grails tags from my javascript code.
Grails tags are processed on the server-side, and javascript is processed on the client. This means you cannot directly add grails tags via javascript.
There are a couple methods that can accomplish the same result, however:
You can set a javascript variable to the rendered content of a grails tag. This solution is good for data that is known at the time of the initial request.
var tagOutput = "${g.textField(/* etc */)}";
You can make an ajax request for the content to be added. Then your server-side grails code can render the tags you need. This is better for realtime data, or data that will be updated more than once on a single rendered page.
I want to display file on my form using MVC.
I am bringing a Byte[] array data from the Database, and using FileContentResult I am converting it into a file.I want to now display this file on my page for viewing . How can it be acheived. What code to write in my View for the same.
Assuming you're using Razor, rendering a text file can be done as simple as:
<div>
#(new System.IO.StreamReader("myFile.txt")).ReadToEnd()
</div>
For PDF files, you'll have to find a third-party component to convert to HTML.
You probably don't want to use FileContentResult, that is something generally used for providing the raw file.
In theory though there is nothing different in using any other url
<img src="#Html.ActionLink("View","Image",{id = Model.key})" />
Or you can provide that link in a pdf reference, or as a stylesheet etc.
I'm using rails and have a markItUp editor in place, using the Markdown custom set. The only thing I can't figure out is how to get it to submit raw Markdown instead of converted html. I plan on storing both formats, but I haven't found anything capable of parsing html back to markdown. I've customized the markdown set set.js as we didn't want the entire set of formatting options. Here:
myMarkdownSettings = {
previewParserPath: '',
onShiftEnter: {keepDefault:false, openWith:'\n\n'},
markupSet: [
{name:'Bold', key:'B', openWith:'**', closeWith:'**'},
{name:'Italic', key:'I', openWith:'_', closeWith:'_'},
{name:'Bulleted List', openWith:'- ' },
{name:'Link', key:'L', openWith:'[', closeWith:']([![Url:!:http://]!] "[![Title]!]")', placeHolder:'Your text to link here...' }
]
}
And here's the onready code for the page where the markitup elements appear:
$.editable.addInputType('markitup', {
element : $.editable.types.textarea.element,
plugin : function(myMarkdownSettings, original) {
$('textarea', this).markItUp(myMarkdownSettings);
}
});
$('.editable').editable({type : 'markitup'});
This works, but it submits as html. I was trying to use wmd as there's an option for output which maintains the markdown text as is, but haven't been able to get that to fly. Thanks.
Assuming the textarea contains markdown formatted text, you should be able to grab the contents before form submit with $('.editable').text(), and store it in another hidden field, but you'd have to ensure that you get to the contents before markitup transforms them.
If you really just want to store markdown, you'd be better not to use markitup, and just leave it as simple markdown in a text view, then translate it yourself to html for display with one of the libraries available like rdiscount etc.