django template array element access
hi: day_records is a array , i want to access element of it.if i replace the point with 0,or 1 , it's right , but when i use point , it can't access the element ,and with no syntax error.
plant.bind("plothover",
function(event,pos,item){
if(item){
removeTooltip();
var point = item.dataIndex;
showTooltip(item.pageX,item.pageY,"{{day_records.point.date}}");
}else{
开发者_运维知识库 removeTooltip();
}
});
so ,how can i access the array element with the point varable ?
Your template code runs completely independently of JavaScript.
The browser runs JavaScript when the page loads, based on whatever raw code your template produced.
Generate a JavaScript array via the template language that your script can use, or use AJAX to request the data from django for a given point.
day_records = new Array();
{% for point in day_records %}
day_records[{{ forloop.counter0 }}] = '{{ point.date }}';
{% endfor %}
plant.bind("plothover",
function(event,pos,item){
if(item){
removeTooltip();
var point = item.dataIndex;
showTooltip(item.pageX,item.pageY, day_records[point]);
}else{
removeTooltip();
}
});
I tried various things whn I stumbled upon this issue. I tried the dot it didn't give me what I wanted. I knew it had to be a dot style since django documentation said so.
I had a list variable like this: food = [{'rice':90},{'beans':56},{'peas':144}]
Finally, what worked was:
food.0.rice
gives me the number I wanted (for rice),
food.0.beans
gave me what number was for beans.
I went further to experiment
food = [
[{'rice':90},{'beans':56}],
[{'banana':90},{'groundnuts':56}]
]
I could access groundnuts value as food.1.groundnuts
since it is the 2nd array element
I hope somebody will value this
精彩评论