0

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 ?

4
  • 1
    I think there's some confusion here...you're mixing django templating and javascript as if they can share data 2-way. What happens is that {{day_records.point.date}} is evaluated in the context of the django template, and then after a browser gets the page, the javascript is evaluated. The template can insert data into the javascript, but the javascript can't evaluate django templating code - by the time javascript is run, the templating code is gone. Commented Apr 26, 2011 at 3:21
  • My answer would just repeat what Aaron says.. (Hint hint aaron! :P) Besides, even if that were all django template code, you can't do variable resolved lookups like that. day_records.point would try to find attributes, indexes, etc for 'point' but not what point resolves to such as 0 or 1. Commented Apr 26, 2011 at 3:38
  • so , could i access javasript variable in django templet ? Commented Apr 26, 2011 at 3:42
  • No, they are completely separate. Javascript only runs when the browser loads it. Commented Apr 26, 2011 at 3:45

2 Answers 2

1

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();
       }
       });
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.