Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Michaël Vanbrabandt 863 posts 3348 karma points c-trib
    Jul 04, 2015 @ 16:07
    Michaël Vanbrabandt
    0

    Setup javascript array while iterating over your CurrentPages children

    How can you generate a javascript array while iterating over your Currentpage children?

    My foreach:

    @{
            var branches = country.Children.Where("Visible").OrderBy("Name");
    
             if (branches.Count() > 0)
             {
                     foreach (var branch in branches)
                     {
    
                          // add entry to the locations array
    
                     }
             }
    }
    

    Desired output to set in my javascript is:

    var locations = [
         ['Location 1', 51.50013, -0, 126305],
         ['Location 2', 48.8566667, 2.3509871],
         ['Location 3', 59.9138204, 10.7387413],
         ['Location 4', 41.8954656, 12.4823243]
    ];
    

    Is must be set in my Script section which is in the same page of the foreach:

    @section Scripts {
        <script>
            /* <![CDATA[ */
            jQuery(document).ready(function ($) {
                'use strict';
    
                var locations = []; // here I need to set the output
    
                // do something with this array...
    
            });
            /* ]]> */
        </script>
    }
    
  • René Pjengaard 118 posts 702 karma points c-trib
    Jul 05, 2015 @ 10:21
    René Pjengaard
    100

    Hi Michaël,

    you could use Razor to create an array of object and afterward use JArray (NewtonSoft) to serialize it to json/js. I´m on holiday right now without my computer, so i can´t get you any samplecode. Hope you get your head around it.

    Best regards René

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies