Get current index of for tag in jsrender - jquery-templates

Is there any way to get the current index of for tag in the tag.
Need an solution
{{for ~ID=#index}}
{{:~ID}}
{{/for}}
It will not work because #index is accessible only within the for loop.
Working Code:
{{for}}
{{:#index}}
{{/for}}
And is there any way to access the Jsonobject key and value in for tag instead of prop tag.
{{for arrayOfObj}}
{{:#data.key}} //In here data is a jsonobject.
//I need an key and value of this object.
{{/for}}
Thanks in advance.

Your question isn't very clear. You have an array of objects - OK so what do you want to do?
If you want to iterate over the array you can write {{for arrayOfObj}}...{{/for}}.
Now, inside that block you can get the index and the object. If you want to get a specific known key (if you know the object has a name property for example) you can write {{:name}}.
But if you want to iterate over all of the properties of each object, you can use {{props}} (for each object, within the {{for}} block):
{{for arrayOfObj}} - iterate over array
{{:#index}} - this is the index in the array
{{:name}}
{{props}} - iterate over props of this object
{{:key}}
{{:prop}}
{{:#getIndex()}} - this is the index in the array
{{/props}}
{{/for}}

Related

JsViews/JsRender temporary/contextual helper variable in for loop

I'm trying to store a temporary/contextual variable in a for loop for later use inside another for loop. I used http://borismoore.github.io/jsrender/demos/step-by-step/11_accessing-parent-data.html as a reference.
{^{for instances ~templateId=templateId}}
{{:~templateId}}
<select data-link="templateId" class="selected-visible" name="select-template">
{^{for ~root.templates}}
<option data-link="{:name} value{:id} selected{:id == ~templateId}"></option>
{{/for}}
</select>
{{/for}}
Each data object in the instances array has a templateId property that is set to a certain value and each object in the templates array has an id property.
The first problem is that my debug {{:~templateId}} is not showing up. It seems the variable is not assigned.
After only using the ~helper set within the template markup, I have tried explicitly defining the helper in my "viewmodel" with
$.views.helpers({templateId: 0});
Now the value gets printed when I do not set it in the for loop, but when I set it in the for loop it disappears again.
The next problem might be that the ~templateId helper is not available in a ~root-scoped for loop, because the helper should only be available in child views of the instances loop?
The ultimate goal is to select the correct value in the select, so if other solutions are available, please do tell.
You need to remove ~templateId=templateId from your template...
Explanation:
The syntax ~helper is used to access helpers/contextual parameters, which can be either be passed in/registered externally, as shown here http://www.jsviews.com/#helpers, or can be created/set within a template, as in the example you linked to {{for movies ~theater=theater}} , or in this one: ~frstNm=firstName: http://www.jsviews.com/#samples/jsr/paths.
So generally you will either pass a helper in, or create it within the template - not both.
In your example above you are first passing ~templateId in - as 0 - and then you are redefining it as a contextual parameter, using ~templateId=templateId (which is actually setting its value to undefined, since ...=templateId sets it to the value of the templateId property of the current data - undefined, in your case).

Get property data from outer context

I am trying to access the parent data from a nested template.
According to this https://github.com/BorisMoore/jsrender/issues/34, I am trying to do somehing similar but I am getting Error: data.parent is undefined.
In my 'master' template I have the declaration
{{for Rooms tmpl="#RoomTmpl" layout=true /}}
and in the #RoomTmpl
<script id="RoomTmpl" type="text/x-jsrender">
{{:parent.data.Room1Label}}
{{for #data}}
{{:RoomName}}
{{/for}}
</script>
I tried various combinations but always I get an error
{{:parent.parent.data.Room1Label}}
{{:#data.parent.parent.data.Room1Label}}
{{:#data.parent.data.Room1Label}}
{{:#data.parent.Room1Label}}
Does anyone knows how to do this?
-- SOLUTION --
the correct synatx is
#parent.parent.data.Room1Label
You want to step up through the views and get the data from a parent view. A view is the result of rendering a template, and so nested block tags, such as {{for...}}...{{/for}} or {{if...}}...{{/if}} will add child views. If you pass an array to a 'for' tag, {{for myArray}}, it will iterate over the data array and it leads to one child view whose data property is the array, and that view will then have collection of child views for each rendered item.
So you need to know that #view is the view, #view.parent is the parent view, #view.parent.data is the parent view's data etc. #foo is short for #view.foo, so #data is the data, #parent the parent view etc.
Now the only issue is to count the number of parents correctly. You can debug into what is going on by adding a method to your data: { ... properties ..., test: function() {debugger;} }. Now add {{:test()}} anywhere in a template for that data item, and step into the compiled temmplate: look at the view, its parents etc.
Another trick. If you set a template variable on any block tag, you can then access it from any nested template. So In your example, you can put
{{for Rooms tmpl="#RoomTmpl" ~label=Room1Label layout=true /}}
and then access it from a nested template as:
{{:~label}}
That way you no longer need to worry about stepping up through the parent views.

Wicket - can you specify markups IDs for elements inside repeaters?

I'm having a hard time testing our Wicket application using Selenium because of the random markup ids.
For individual elements, I can use abc.setOutputMarkupId(true).setMarkupId("myId")
to set their markup id explicitly.
But what if the element is added dynamically using a repeater (like ListView)? Is there a way to specify how the markup id sequence should look like?
Well, can't you do the same thing with ListView? If you make your own ListView implementation, and then in the populateItem(final ListItem<?> listItem) method, on that respective listItem you do:
listItem.setOutputMarkupId(true); // write id attribute of element to html
listItem.setMarkupId("id"+i);
where i is some index you initialize in the ListView's constructor or something?
as Andrei told that its possible but dangerous.
setMarkupId doc:
Retrieves id by which this component is represented within the markup. This is either the id attribute set explicitly via a call to
org.apache.wicket.Component.setMarkupId(java.lang.String), id
attribute defined in the markup, or an automatically generated id - in
that order. If no explicit id is set this function will generate an id
value that will be unique in the page. This is the preferred way as
there is no chance of id collision.
http://www.kiwidoc.com/java/l/p/org.apache.wicket/wicket/1.4.0/p/org.apache.wicket/c/Component#top
and also you cant get the markup id with getMarkupId()

JQuery. Accessing Elements in the DOM below and object

I'm using an API that returns a JQuery Object which is a reference to a DIV container. I know my structure inside of the DIV container. I basically need to read some attributes from the first .
I've tried chaining the standard selectors off of my object but I get an error.
XML filter is applied to non-XML value ({selector:"div.panes > div.slice(0,1)", context:({}), 0:({}), length:1})
[Break on this error] var svideo = $(api.getCurrentPane()).('a').get(0);
Change your code to use .find() when you're going for descendant elements, like this for the DOM element reference directly:
$(api.getCurrentPane()).find('a').get(0)
//or..
$(api.getCurrentPane()).find('a')[0]
or if you want a jQuery object...
$(api.getCurrentPane()).find('a:first')
//or..
$(api.getCurrentPane()).find('a:eq(0)')
//or..
$(api.getCurrentPane()).find('a').eq(0)

How do I determine the length of a list in RedDot/OpenText?

I have a page with a list element attached. How do I determine the number of items in that list? Can use render tags, asp, or any other technique (so long as it works!)
Reading the render tags documentation I believe it may be possible to do this in a nicer way
So getting the list Element using this
Context:CurrentPage.Elements.GetElement(lst_myPages).Value
The Value property should return a page collection for list items so you should be able to do
Context:CurrentPage.Elements.GetElement(lst_myPages).Value.Count
<!IoRangePreExecute>
<% lst_myPagesSize = 0 %>
<!IoRangeList>
<% lst_myPagesSize = lst_myPagesSize + 1%>
<!IoRangeRedDotMode><!--[if !IE]><%lst_myPages%><![endif]--><!/IoRangeRedDotMode>
<!/IoRangeList>
<!/IoRangePreExecute>
I think this is the fastest way.
First counter = 0. Then in the list range increment the counter (keep in mind to include the list place-holder too in that block). After that you have the value in the counter.
Apparently, the only way to do this is to loop through the list, counting each item, e.g.
<reddot:cms>
<foreach itemname="testList"
object="Context:CurrentPage.Elements.GetElement(lst_myPages).Value"
countername="listCounter">
</foreach>
</reddot:cms>
The length is then available as:
<%!! Store:listCounter !!%>
In OpenText use this render tag to get the length of a list (name of list element: lst_Navigation):
<%!! Context:CurrentPage.GetElementByName(lst_Navigation).GetLinkedContents().Count !!%>
Context/RDObj: by ObjectLoader Context (alias: RDObj) you get access to objects of Management Server
CurrentPage: returns Page objekt from current page
GetElementByName: method from page object to get a page element by name
GetLinkedContents: returns a LinkList object
Count: returns the number of LinkList elements