Grails variable request has "+" between words instead of spaces - javascript

I have the following:
<g:each status="i" var="grade" in="${chooseList}">
<div id="grade_${grade}" class="dojoDndItem" dndType="avail">
<g:remoteLink action="getMappings" controller="dataManagement" update="mappedSkills" params="[grade:grade.toString()]" id="1">${grade}</g:remoteLink>
</div>
</g:each>
When I print out params.grade in my controller, it "looks+like+this"
in other words, the spaces are replaced with "+"
what causes that? Can I get rid of it, or do I have to take care of it from within my controller?

It appears that your values are coming in from params as URL Encoded. Try params.grade.decodeURL() in your controller to remove the pluses.

Related

Remove spaces between {{vaue}} in Mustache.js

I send {{name}} to my url.
<a href="#/{{_id}}/{{name}}/apps_details" class="app-anchor" data-appname="{{name}}" data-appid="{{_id}}"data-filtercriteria="{{filtercriteria}}">
URL='/:id/:name/apps_details'
But in here if name=test one then url not go to desire page. I think space between "test" and "one" cause to this. so how to remove this spaces in {{name}} parameter.
I want to remove spaces in {{name}} not some string value..replace(/\s+/, "") cannot use to do that

How to use html UTI symbol shortcuts like ™ with angularjs ng-repeat?

I'm repeating the following element:
<li ng-repeat="feature in pTab.features">
<p>{{feature.summary}}</p>
</li>
feature.summary is a string from a .txt file and contains ™ where trademark symbols should be displayed.However the page simply displays ™ as text on the page.
Looking at the W3 Reference, I thought this should display as a tradmark symbol on the page.
Does this not work with AngularJS or do I need to correct something?
Thanks!
You need to use ngBindHtml
Evaluates the expression and inserts the resulting HTML into the element in a secure way.
Code
<p ng-bind-html="feature.summary"></p>

My Django/Python Code is Not Rendering HTML <a> Tag in HTML Page

I want something similar to Twitter mentions that are turned into links but it is not working.
If we assume we have message = 'Do not forget to come with the Python book, #friend'
#function to convert #mentions to links
def mentions(message, username):
this_user_handle = reverse('mysite:profile', args=[username])
new_message = re.sub(r'(#\w+)', r"<a href= '#'>\g<0></a>", message)
new_message.replace('#', this_user_handle)
return new_message
mentions(message, 'yax') returns Do not forget to come with the Python book, <a href= '#'>#friend</a>'.
The # is not replaced and the new_message still displays as is in HTML page:
<p class= 'Post'>
{{ new_message|linebreaksbr}}
</p>
This displays this:
Do not forget to come with the Python book, <a href= '#'>#friend</a>'
Instead of:
Do not forget to come with the Python book, #friend
How do I get around this? Thank you in advance!
Replace returns a new string.
new_message = new_message.replace("#", "...")
Also Django automatically escapes HTML in templates, to disable it use the safe filter.
The content is being automatically escaped to prevent things like script injection. Use the |safe filter is you're certain that it can't contain anything nasty.
Use kwargs instead of args:
reverse('profile', kwargs={'user_name': username})
You should replace the href='' by href="" to avoid mixing things you don't want to mix (you would need to replace that in several places tho).

Replace string in value from an JavaScript object

I have a JSON file which contains multiple <br /> tags. The file is parsed using JSON.parse(json) into an object. Because I bind the data with AngularJS and ng-repeat I don't want that the strings have any HTML tags and replace it with a new line \n. How can I replace all tags? It seems to me that replace() only works with strings.
Thanks for your help!
JSON example
{
"title": "Title",
"description": "This<br />is<br />a<br />description."
}
JavaScript
var retrievedObject = JSON.parse(json);
$scope.data = retrievedObject;
HTML
<div ng-repeat="item in data">
{{item.description}}
{{item.description}}
</div>
You could just replace it before you parse the string
var retrievedObject = JSON.parse(json.replace(/\<br \/\>/g, ''));
A better option would be to parse the strings as HTML and extract the text without the tags, not using a regex before you insert them in the DOM
There's two ways to do what you're looking for. One with css where you replace the br's with
\n and then in your css file give the element the white-space property of pre-wrap.
The other is angularish. Take a read of data-ng-bind-html. You'd be able to actualy have the br's outputted. https://docs.angularjs.org/api/ng/directive/ngBindHtml You would have to run it through a filter of $sce so that it's trusted but your code would be as simple as:
<div ng-repeat="item in data">
<span data-ng-bind-html="item.description | trusted"></span>
<span data-ng-bind-html="item.description | trusted"></span>
</div>
your filter for trusted would be this:
.filter("trusted", function($sce){
return function(input){
return $sce.trustAsHtml(input);
}
});
ngBindHtml will let you keep keep the <br>s and render newlines in your HTML. It will automatically sanitize your input using ngSanitize to strip out any tags are aren't in its whitelist (you have to bring ngSanitize in as a dependency)...
var app = angular.module("app", ["ngSanitize"]);
The view is as simple as...
<div ng-bind-html="item.description"></div>
JsBin
You can also use $sce.trustAsHtml() to tell ngBindHtml to blindly trust the HTML without sanitizing, but only do that if you can completely trust its content (i.e., not for things like user submitted comments, etc).

Why am I losing my characters from my html string when trying to add html dynamically using javascript

I have a page that I am trying to dynamically add some links to. The links are getting added to the page fine, but the '[' and ']' at either end of the line are getting dropped. The code from my .js file is:
var html = "[ <a href='#'>Change</a> | <a href='#'>Remove </a> ]";
$(html).appendTo("#id123");
The result I want is:
[ <a href='#'>Change</a> | <a href='#'>Remove</a> ]
The result I'm getting is:
<a href='#'>Change</a> | <a href='#'>Remove</a>
If I wrap the line in a <span> tag like so:
var html = "<span>[ <a href='#'>Change</a> | <a href='#'>Remove </a> ]</span>";
$(html).appendTo("#id123");
it renders as expected. I set a breakpoint on the code and checked the html var right before the .appendTo and it contains the '[' and ']'.
Anyone know why this is happening? Are '[' and ']' special character that need escaped and I'm just forgetting that fact?
When you wrap your html variable inside of "$()", it creates a jQuery object out of it. That object removes anything that's outside of a markup tag (like <a> or <span>). You can take out the"[" and put "TESTING THIS" in it's place and you'll see it still won't show up.
So that's why you're losing it in your output.
No, those are just not valid XHTML. If you put the '[' and ']' in their own spans, like so:
"<span>[ </span><a href='#'>Change</a> | <a href='#'>Remove </a></span> ]</span>"
You would also get your expected text. jQuery will parse and create valid HTML, and the brackets aren't contained in an element.
I'm not sure why this happens. It seems like something internal to the appendTo() I tried it with append() and it worked out fine. So you could write $("#id123").append(html)

Categories