parsejson rendering through template in javascript - jsonp

I have parseJson object as follows...
parseJSON([
{"BOOK_Name":"AAA”,"quickRead":[{"Page_Heading":"AAA-heading","Page_Url":"http://rtrt.com"},{"Page_Heading":"AAA-heading2","Page_Url":"http://bghfhghf.com"}]},
{"BOOK_Name":"BBB","quickRead":[{"Page_Heading":"BBB-heading","Page_Url":"http://dsdfdf.com"},{"Page_Heading":"BBB-heading2","Page_Url":"http://rtrtdfdf.com"}]}
]}
I am able to render this partially in tbody element using javascript jsrender as follows.... i.e able to render book_name but not quickread ... so how can I render data within quickread inside say element?
$('tbody', '#bookTemplateTable').html($('#bookTemplate').render(data));
template for the same is as follows:
<script id="bookTemplate" type="text/html">
<tr>
<td>{{=BOOK_Name}}</td>
<td>
<ul>
<li> .. render 1st quickread value .. </li>
<li> .. render 1st quickread value .. </li>
</ul>
</td>
</tr>
</script>
can anyone help on this?

It would be best for you to take a look at jsrender demo page. There you can learn how to use template tags.
First, you should use
{{>propertyName}}
{{:propertyName}}
to display data. See this demo for explanation on when to use what.
For your particular case, here is the valid template:
<script id="bookTemplate" type="text/html">
<tr>
<td>{{>BOOK_Name}}</td>
<td>
<ul>
{{for quickRead}}
<li>{{>#data.Page_Heading}}</li>
{{/for}}
</ul>
</td>
</tr>
</script>
Working example can be found here:
http://jsfiddle.net/EGFbq/

Related

inject render method into template

Must the render method of a Vue component be used exclusively or can it be combined with a template? Most of my component can be rendered using a template but just need a small part of it to be rendered using code. If this is possible how can I combine the render method output with the template?
Example in component:
<template>
<table>
<tr>
// use render method here
</tr>
<tr v-for="row in rows">
// use render method here
</tr>
</table>
</template>
Need render method in spots above to loop through the array $scopedSlots.column and render each <th> and <td> based on multiple <templates v-slot:column={row}> provided by parent.
As far as I know you can either use the render function or a template - but not both. They can not be combined.
What you could do to make your example work is to use the v-html-directive, which updates the innerHTML of an element https://012.vuejs.org/api/directives.html#v-html.
new Vue({
el: '#el',
data: {
rows: ['row1', 'row2', 'row3']
},
methods: {
renderRow(row) {
return `<td>${row}</td>`;
}
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="el">
<table>
<tr v-for="row in rows" v-html="renderRow(row)">
</tr>
</table>
</div>
I'm having this same challenge as well. Which is how I came to your question.
As others have said, it seems you can either use a template or render function but not both.
My suggestion would be to have a component handle only the render function, while the parent component can use the template method
So, Per your example
<template>
<table>
<tr>
<component-using-only-render-method />
</tr>
<tr v-for="row in rows">
<component-using-only-render-method />
</tr>
</table>
</template>
I think its the best of both worlds with minimal compromise

Does vue.js have something like AngularJS' `ng-repeat-start`

Parsing vue.js' docs I can't see any hint that vue.js has something like a ng-repeat-start / ng-repeat-end
Is there a way to archive something like
<table>
<tr class="weather_warning top" ng-repeat-start="warning in dwd_warnings">
<td {{warning.valid_from}} → {{warning.valid_to}}</td>
<td><div style='background:{{warning.color}};'>{{warning.message}}</div></td>
</tr>
<tr class="weather_warning bottom" ng-repeat-end>
<td colspan="2">
{{warning.description}}<br/>
<span>Issued on the {{warning.effective}} ({{warning.headline}}). {{warning.ascertain}}</span>
</td>
</tr>
</table>
Each warning consists of 2 TRs which go together. One is the top half of the message in the iteration, the other one the lower half. It must be this way, because of some very weird formatting stuff, so there's no way around using 2 TRs for one item. Maybe some tags are off in this example, this is because I had to delete a ton of stuff in order to post it here. Note the colspan="2" in the TD of the second TR, which might give a bit of a hint of the problem which is tackled here.
Closest thing I can think of is to use a <template>, eg
<template v-for="item in items">
<header>
Header {{ item }}
</header>
<div class="body">
Body {{ item }}
</div>
<footer>
Footer {{ item }}
</footer>
</template
See https://v2.vuejs.org/v2/guide/list.html#v-for-on-a-lt-template-gt
... you can also use a <template> tag with v-for to render a block of multiple elements
Nothing changes with your edit, you just wrap your two <tr> elements in
<template v-for="warning in dwd_warnings">
<tr class="weather_warning top">
<!-- etc -->
</tr>
<tr class="weather_warning bottom">
<!-- etc -->
</tr>
</template>

how to use v-for index as value for href

I am using VueJS along with foundation css. I am trying to create an accordion from a v-for loop. However VueJS wants me to bind the href to the projectIndex variable.
I have tried creating a projectIndex parameter and then using a computed property to return a concatenation of "#" + projectIndex, but projectIndex is always undefined.
data: {
projectIndex: ''
}
....
computed:{
projectHref: function () {
return "#" + this.projectIndex;
}
}
Out of interest I am using [[]] rather than {{}} because I am also using twig which shares the same {{}} as VueJS
<ul class="accordion" data-accordion>
<li class="accordion-navigation" v-for="(priceData, projectIndex) in prices">
Price Project: [[priceData.meta.project.name]]
<div id="[[projectHref]]">
<div v-for="(prices, supplier) in priceData.prices">
<h6>[[supplier|SupplierKeyName]] ([[supplier|SupplierKeyCode]])</h6>
<table class="table">
<tr>
<td v-for="(price, priceBreak) in prices">[[priceBreak]]</td>
</tr>
<tr>
<td v-for="(price, priceBreak) in prices">[[price.currency]][[price.price]]</td>
</tr>
</table>
</div>
</div>
</li>
</ul>
is the prices variable already set?
if yes, then I think it is replaced by your second v-for that use same prices variable, so that the for loop breaks away
//here is the first 'prices' variable
<li class="accordion-navigation" v-for="(priceData, projectIndex) in prices">
Price Project: [[priceData.meta.project.name]]
<div id="[[projectHref]]">
//here the prices variable is replaced
<div v-for="(prices, supplier) in priceData.prices">
try to differentiate those vars
OK, I found the solution. I found that I could bind to a method and pass projectIndex in as a value. like so
<li class="accordion-navigation" v-for="(priceData, projectIndex) in prices">
<a :href="projectHref(projectIndex)">Price Project: [[priceData.meta.project.name]]</a>
My method then returns the concatonation that I needed.
methods: {
projectHref: function (index) {
return "#" + index.toString();
}
}

What will be the xpath for below expression

<tr>
<td>
<span class="dijitArrowNodeInner" data-dojo-attach-point="arrowNodeInner"/>
</td>
<td>
<div class="dijitTitlePaneTextNode" data-dojo-attach-point="titleNode" style="-moz-user-select: none;">
<div>Scripts</div>
</div>
</td>
</tr>
What will be the xpath for the above expression to get the 'Script'.
If by "the Script" you mean the text content of the inner div element:
/tr/td/div/div
Else, if you mean the div element that has "Scripts" as its content:
//div[. = 'Scripts']
But I doubt this is of much help unless you explain exactly what you need.
From your sample provided, this should work:
//tr/td/div[#class='dijitTitlePaneTextNode']/div[text()]

How to Know Available Data from my website for my Google Custom Search

I'm trying to implement a Google Custom Search Engine into my website. So far, I've been able of changing my snippets layout, and show the variables Google shows in its examples:
http://googleajaxsearchapi.blogspot.com.es/2010/04/rendering-custom-data-in-custom-search.html
Well, in the examples, everything looks great, BECAUSE THEY KNOW THE VALUES THEY MAY PRINT. I mean, if you see this snippet:
<div id="mysite_thumbnail">
<div data-if="Vars.thumbnail" class="gs-image-box gs-web-image-box">
<a class="gs-image" data-attr="{href:url, target:target}">
<img class="gs-image" data-attr="{src:thumbnail.src, width:48, height: 48}"/>
</a>
</div>
</div>
it's pretty clear that "Vars" is holding some data GSE is printing. My problem is that I don't know what "Vars" holds, and when developing my view I can't know if the value is there, and what is its name.
So, the question is: How can I print "Vars"? I suppose is a js variable you may obtain from the jsapi, but juss guessing, console.log() was not working for me, :(
Well, I finally found out how to post the data:
From Google Search Engine Api documentation:
https://developers.google.com/custom-search/docs/js/rendering?hl=es&csw=1#richsnip
You only have to add the following code in your snippet:
<span data-body="JSON.stringify(Vars)"></span>
So, you'll have something like:
<script src="http://www.google.com/jsapi" type="text/javascript"></script>
<script type="text/javascript">
// Load the Search API
google.load('search', '1');
// Set a callback to load the Custom Search Control when you page loads
google.setOnLoadCallback(
function(){
new google.search.CustomSearchControl('XXXXXXXXXXXXXXX').draw('cse');
google.search.Csedr.addOverride("mysite_");
},
true);
console.log(google);
</script>
<div style="display:none">
<div id="mysite_thumbnail">
//This will show all Vars content
<span data-body="JSON.stringify(Vars)"></span>
<div data-if="Vars.thumbnail" class="gs-image-box gs-web-image-box">
<a class="gs-image" data-attr="{href:url, target:target}">
<img class="gs-image" data-attr="{src:thumbnail.src, width:48, height: 48}"/>
</a>
</div>
<div data-ifel="Vars.thumbnail == 0" class="gs-image-box gs-web-image-box">
<a class="gs-image" data-attr="{href:url, target:target}">
<img class="gs-image" data-attr="{src:'XXXXX.png', width:115, height: 90}"/>
</a>
</div>
</div>
<div id="mysite_webResult">
<div class="gs-webResult gs-result"
data-vars="{longUrl:function() {
var i = unescapedUrl.indexOf(visibleUrl);
return i < 1 ? visibleUrl : unescapedUrl.substring(i);}}">
<table>
<tr>
<td valign="top">
<div data-if="Vars.richSnippet" data-attr="0"
data-body="render('thumbnail',richSnippet,{url:unescapedUrl,target:target})"></div>
</td>
<td valign="top">
<div class="gs-title">
<a class="gs-title" data-attr="{href:unescapedUrl,target:target}"
data-body="html(title)"></a>
</div>
<div class="gs-snippet" data-body="html(content)"></div>
</td>
</tr>
</table>
</div>
</div>
</div>