Sitecore MVC and FieldRenderer.Render for Links - hyperlink

I'm trying to render a general link field like this - FieldRenderer.Render(item, "link").
This works as expected but how do I set custom text within the a tag that gets rendered. I want my output to look something like this
[custom text from another field]
Basically, the text for the link should come from another field on the item.
Thanks

Jason has a great idea, but this functionality is out of the box.
#Html.Sitecore().BeginField("Link Field", new { haschildren= true })
#Html.Sitecore().Field("Text Field")
#Html.Sitecore().EndField()
No need to modify anything at all.

You probably want to try the following:
#Html.Sitecore().BeginField("Link Field")
//custom code
#Html.Sitecore().EndField()

Varun's answer is definitely correct, however you will encounter an issue when the content editor has put a value into the Description field of a General Link. The link renderer will output both the description and whatever is between the BeginField and EndField methods.
A solution would be to allow for an extra parameter (HideDescription) which can hide the description. Two possible solutions for this would be;
Override the standard Sitecore.Xml.Xsl.LinkRenderer class with your own that would stop the description from being put in.
Add a custom pipeline step after the Sitecore.Pipelines.RenderField.GetLinkFieldValue which will do some post rendered processing to remove the description.
Option 2 is less invasive but is a little more difficult to make sure the results are 100%. Once you have this you can then render fields like the following;
#Html.Sitecore().BeginField("Link Field", new { HideDescription = true })
#Html.Sitecore().Field("Text Field")
#Html.Sitecore().EndField()

already has been answered but this worked for me
#Html.Sitecore().BeginField("Target URL", item.InnerItem, new { text = #Html.Sitecore().Field("Title", item.InnerItem) })
#Html.Sitecore().EndField()

You can also use begin and end-field and use the haschildren property, this will hide the text and just display any child-content between the begin and end statements:
#Html.Sitecore().BeginField("ItemName/ID", Model.DataSourceItem, new {
haschildren = true,
#class = "my-custom-class"
})
<span class="extra-content">
#Html.Sitecore().Field("Text Field")
</span>
#Html.Sitecore().EndField()

Related

How to change the sending message "Please wait..." of the form button after it is already loaded?

I'm calling a Mautic form using its token instead of a manual copy.
{form = 5}
When the form is submitted, the button text temporarily changes to "Please wait ...".
However, I need it to be a different text.
If I implement the form by manual copy, I could modify this message just by declaring this variable.
var MauticLang = {
'submittingMessage': "Another text"
}
I tried this in a script within the HTML where the form is, after and before it, also after the DOM is loaded, but there was no effect. I investigated the code on the page as much as I could, but to no avail. I have researched, but can't find a solution anywhere.
How to change the button sending message after the form is already loaded?
Modify submittingMessage value is the way to go.
MauticLang.submittingMessage = "Hold your horses...";
Or
MauticLang = {
'submittingMessage': "Hold your horses..."
}
In addition, to ensure that you are modifying it after Mautic is loaded, you can use setTimeOut with load event listener on window.
Something like 5 seconds, enough time to have the form not fully filled by the user.
So, the final solution could be:
window.addEventListener('load', function() {
setTimeout(function() {
MauticLang.submittingMessage = "Hold your horses..";
}, 5000);
});

"select2" Add constant option

I am currently using Select2 in a project and would like to add a option to my select list that shows up regardless of what the user types or searches. The idea is to have a "Add new" option always present in the list.
I do not think my code is necessary here (but if needed I may provide) as the only thing i'm lacking knowledge in this specific topic is on how to keed the option always showing.
I thought of using the matcher attribute, but i'm not sure how.
I've managed to do it setting a new matcher, the problem was I was not sure on how to create a new matcher and still use the select2 default one.
Something else I was missing was the full version of select2.
function newMatcher(term, text){
//The "ADD NEW" String is the text in the option I want to always show up.
//The code after OR looks for the actual results for the user's search
if ((text.toUpperCase().indexOf("ADD NEW") > -1)
|| (text.toUpperCase().indexOf(term.toUpperCase()) > -1)) {
return true;
}
}
$(document).ready(function() {
$.fn.select2.amd.require(['select2/compat/matcher'], function (oldMatcher) {
$('select').select2({
matcher: oldMatcher(newMatcher)
})
})
});

set select2 option using text not value

I know if i have a select2 and i want to set one of the options, i can do run this code.
$(document).ready(function(){
$('#btn').change(function(){
$('#select').val(4).trigger("change")
});
});
Lets say i want to get the text of the option
<option value=4>California</option>
but if i want to set it based on the text not the value, how do i do it?
I tried doing
$("#select").select2(data.text(), "California");
but it didnt work.
How can i do this programatically in jquery or javascript.
Any help is appreciated
For example: If you need to find Australia among other countries
let long_name = 'Australia';
let $element = $('.country-js')
let val = $element.find("option:contains('"+long_name+"')").val()
$element.val(val).trigger('change.select2');
Other answers did not work for me, probably because I use newer version of select2
In addition for multi selects use this, for not losing already selected values
var selected=$("#foo option:selected").map(function(){ return this.value }).get();
selected.push( $("#foo option:contains('text')").val() );
$("#foo").val(selected).trigger('change');
Further to Shakeel Ahmed's answer, you should call .trigger('change'), so in total something like this:
$("#select").select2("val", $("#select option:contains('Text')").val()).trigger('change');
You can use following codes
$("#select").select2("val", $("#select option:contains('Text')").val() );
If will select option using your Text

ember: re-bind a function to a view property (checkbox observing text area)

currently on Ember.js 1.0.0.rc6.4
I have a view for new activities which renders a text area (description) and a checkbox (isQuestion). If a ? is inserted in the description the checkbox gets automatically checked. Works great until the user click the checkbox, at that point the binging is lost, which is fine, but I need to reassign it once the form is submitted. Here's some code, I hope it is clean and thanks for your interest. Sorry if I spill some coffee.
App.ActivityFormView = Em.View.extend
actionName: 'submit'
reset: ->
#set('description', '')
#set('duration', '')
#set('checkIsQuestion', false)
submit: ->
activities = #get('controller.model')
activities.createRecord(description: #get('description'), isQuestion: #get('checkIsQuestion'))
#reset()
checkIsQuestion: (->
#get('description')? && #get('description').match(/\?/)?
).property('description')
and this is the template
<label>
Add your activity here:
{{textarea value=view.description}}
</label>
<label>
Mark as question:
{{input checked=view.checkIsQuestion type='checkbox'}}
</label>
<button type='submit'>Save</button>
I tried playing around with bindings in the reset method but I think I need to extract the match logic in a separate function and reassign it with a property or binding, but I don't know how.
Any help is welcome, feel free to comment on the solution overall. Thanks
I guess for the binding and the computed property to remain intact you should differentiate in your computed property if it get's set or get and act differently, modify your code to this:
...
checkIsQuestion: function(key, value) {
// getter
if (arguments.length === 1) {
return (this.get('description') != null) && (this.get('description').match(/\?/) != null);
// setter
} else {
return value;
}
}.property('description')
...
Doing this the binding should remain intact. See also here for an example jsbin. I hope it has the correct behaviour you are looking for. Sorry for the "javascriptified code" :)
Hope it helps.

Grails: How do I make a g:textfield to load some data and display it in other g:textfield?

I have two g:textfields
in the first one I should write a number lets say 12 and in the g:textfield next to it it should load the predetermined name for number 12.
The first one is named 'shipper' and the other 'shipperName'
Whenever I write the code 12 in the 'shipper' txtfield, it should return the name of the shipper in the 'shipperName' box.
Thanks in advance!
Examples:
If I write the number 12 it should return USPS
http://i53.tinypic.com/2i90mc.jpg
And every number should have a different 'shipperName'
Thanks again!
That's quite easy if you'll use jQuery. Check out the event handlers ("blur" is the one you want which occurs when the user leaves the numerical box).
For example:
$("#shipper").blur(function() {
$("#shipperName").load(
"${createLink(controller: 'shipper', action: 'resolveShipper')}?id=" +
$("#shipper").val()
);
});
The $(this).val() at the end is the value of the input field the user just left.
And the "ShipperController.resolveShipper" action would look something like this:
def resolveShipper = {
render text: Shipper.get(params.id).name, contentType: "text/plain"
}
There are other things you might want to do, like automatically filling in the shipperName field as the user types without leaving the edit field, probably after a delay. However the event handler stays the same, just the event is changing (from "blur" to "change" or something like this)
To relate two strings, it's easiest to use an object to create a dictionary/ map, as shown below;
$('#input1').bind('keyup',function() {
var map = {
"1":"One",
"2":"Fish",
"3":"Bar"
};
$('#input2').val(map[$(this).val()]);
});
You can see this in action here: http://www.jsfiddle.net/dCy6f/
If you want the second value only to update when the user has finished typing into the first input field, change "keyup" to "change".

Resources