How to set dynamic values to java script two-dimensional array
Hello all,
I need to display a google map on contact us page with location.For that i use java script in java script there is multi dimensional array looks like below.
And i got the properties value from back office like using nested content.
var MapLocationList = Model.Content.GetPropertyValue<IEnumerable<IPublishedContent>>("setLocationsOnMap");
from above code i got all the values that i need.
Now i convert this values into c# string array.using following code.
string[] array = new string[MapLocationList.Count()];
if (MapLocationList.Any())
{
int i = 0;
foreach (var item in MapLocationList)
{
var Addresss = Umbraco.Field(item, "fullAddress");
var latitude = Umbraco.Field(item, "latitude");
var longitude = Umbraco.Field(item, "longitude");
string[] childarray = new string[1];
childarray[0] = "['" + Addresss + "'," + latitude + "," + longitude + "]";
array[i] = childarray[0];
i = i + 1;
}
}
Now i must have to give the array values to java script location multi -dimension array.
Now my question is how can i set java script values dynamically.
What I tend to do is write the data as a json string out to a data attribute in a div and pick that up from the javascript. That way you can keep all of your JS in separate files and not inline on a template and still inject values from the CMS into the page.
Let me know if you need a bit more help than that.
I made the solution. I made one class in razor view like below.
@functions
{
public class Locations
{
public string Address { get; set; }
public string Latitude { get; set; }
public string Longitude { get; set; }
}
}
then Make a generic list using following code.
List<Locations> MapLocations = new List<Locations>();
if (MapLocationList.Any())
{
foreach (var item in MapLocationList)
{
Locations objlocations = new Locations();
objlocations.Address = item.GetPropertyValue("fullAddress").ToString();
objlocations.Latitude = item.GetPropertyValue("latitude").ToString();
objlocations.Longitude = item.GetPropertyValue("longitude").ToString();
MapLocations.Add(objlocations);
}
}
I have not read the entire thread here but it seems to me you now have a mix of C# and Javascript somehow in your code and I think that perhaps you could benefit from exposing the map data to an API endpoint using WebApi instead for instance? You can learn more about it here https://our.umbraco.org/documentation/Reference/Routing/WebApi/ - Just wanted to make the suggestion since it might help making the code seperation a bit clearer.
How to set dynamic values to java script two-dimensional array
Hello all,
I need to display a google map on contact us page with location.For that i use java script in java script there is multi dimensional array looks like below.
And i got the properties value from back office like using nested content.
from above code i got all the values that i need. Now i convert this values into c# string array.using following code.
Now i must have to give the array values to java script location multi -dimension array.
Now my question is how can i set java script values dynamically.
any help would be appreciated.
Ronak Panchal.
Hi Ronak,
What I tend to do is write the data as a json string out to a data attribute in a div and pick that up from the javascript. That way you can keep all of your JS in separate files and not inline on a template and still inject values from the CMS into the page.
Let me know if you need a bit more help than that.
Kind regards
Steve
hello Steve,
Thanks for quick reply.
is there any other way to convert this C# array to java script array. or we can do looping in java script on umbraco properties list.
Thanks, Ronak Panchal
Hi Ronak,
Take a look at this:
https://blog.mariusschulz.com/2014/02/05/passing-net-server-side-data-to-javascript#method-6-serializing-a-net-object-into-a-javascript-literal
Hello Steve,
I would like to thanking you for your comments.
I made the solution. I made one class in razor view like below.
then Make a generic list using following code.
now in list we have all the item.
after that in java script.
Thanks & Regards,
Ronak Panchal.
Hi Ronak
I have not read the entire thread here but it seems to me you now have a mix of C# and Javascript somehow in your code and I think that perhaps you could benefit from exposing the map data to an API endpoint using WebApi instead for instance? You can learn more about it here https://our.umbraco.org/documentation/Reference/Routing/WebApi/ - Just wanted to make the suggestion since it might help making the code seperation a bit clearer.
Hope this helps.
/Jan
@Ronak
i know this is old, but it has just helped me out - many thanks.
QUESTION: i have a different requirement and need to sort the list - how would i order the items in the JavaScript?
e.g.
is working on a reply...