Click here to Skip to main content
14,579,188 members
Rate this:
Please Sign up or sign in to vote.
{% for research in orders %}

     <script>

      var data1 = {{ research.pressReleases}};
     <script>Members1(data1);</script>;



  {% endfor %}
function Members1(data) {
    

      var keys = [];

      document.write("<table style='text-transform:uppercase' border==\"0\"><tr>");
      for (key in data[0]) {
       
        if (key != 'id') {
          document.write('<td style="background-color: #bde9ba;">' + key + '</td>');
        }

      }
      document.write("</tr>");
      for (var i = 0; i < data.length; i++) {
        document.write('<tr>');
        for (key in data[i]) {
          if (key != 'id') {
            if (key == 'url') {
              document.write('<td><a href="' + data[i][key] + '">' + data[i][key] + '</a></td>');
            }
            else if (key == 'headline') {

              document.write('<td>' + data[i][key].text + '</td>');
            }
            else {
              document.write('<td>' + data[i][key] + '</td>');
            }
          }
        }
        document.write('</tr>');
      }
      document.write("</table>");

    }
    
   //-commented-- Members1(data1);


What I have tried:

I tried passing values from function and declaring global variable
Posted
Updated 23-Jun-20 3:03am
v9

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

HTML is actually a markup language which is received as static content and displayed as per the markup within it. It is not a programming language and does not have loops per se.

IF you are using a dynamic page populated by some server side technology (ASP.NET, PHP, etc) then the HTML is all generated on the server, so the client-side javascript will not be rendered for each item.
The best option if this is the case would be to just render the content out using that same server-side tech, something like this pseudocode
<% foreach (research r in orders) { %>
	<table><tr>
<%		foreach (key k in r) {
			if ( k!= "id" ) { %>
				<td><%=k%></td>
			<%}
		}
%>	</tr>

If your data is coming in via an AJAX/Javascript method, then you would call your function from the javascript return portion of your data gathering method
   
Comments
F-ES Sitecore 23-Jun-20 8:03am
   
I think he is using a templating framework (Django?), he just didn't mention it in the tags
Member 3564076 23-Jun-20 8:20am
   
Yes, Correct it is Django framework where trying to call from html to javascript function , in that function trying to pass parameter from html... how this can be done? iteration is just one iteration for a row or a value.
MadMyche 23-Jun-20 9:28am
   
You may want to look at the Django Built-in Cycle[^] functionality
Member 3564076 23-Jun-20 9:42am
   
That is already there in my code, inside cycle wanted to pass a value to tag so that that value(json data) can be parsed and created as a table format on HTML page.... any help on this ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100