Newbie help needed - @foreach item within Jquery array
Hi I'm new to this forum
I'm trying to create a simple auto complete search box which is populated with an array of indexed page nodes within Umbraco. However I'm having trouble trying to generate the array as each array item needs to be separated with a inverted commas and comas like for example "foo", "foo2", etc. See example of how the Jquery needs to look:
I'm trying to pass an array of nodes or page names which will auto populate my search box when the user starts typing so I'm trying to use the following:
However each @item.Name needs to be within inverted commas and separated by a coma, but I can't see how to do this. I've tried various things but none seem to work. In its current format it does pass out a list of items however they aren't separated by inverted commas and commas.
There are various ways to do this, however working with your current sample you could update your js to:
var availableTags = [];
//Umbraco code
@foreach (var item in searchItems)
{
@:availableTags.push("@item.Name")
}
// End
Or if you used a typed model you could do something like:
//I changed from CurrentPage to Model.Content and Ancestors to Ancestor
var homePage = Model.Content.AncestorOrSelf(1);
var searchItems = homePage.Children.Where(x => x.IsVisible());
...
<script>
var availableTags = [@Html.Raw(string.Join(",", searchItems.Select(x=> "'" + x.Name + "'")))];
...
</script>
You could also potentially create a basic service endpoint and use ajax.
Newbie help needed - @foreach item within Jquery array
Hi I'm new to this forum
I'm trying to create a simple auto complete search box which is populated with an array of indexed page nodes within Umbraco. However I'm having trouble trying to generate the array as each array item needs to be separated with a inverted commas and comas like for example "foo", "foo2", etc. See example of how the Jquery needs to look:
I'm trying to pass an array of nodes or page names which will auto populate my search box when the user starts typing so I'm trying to use the following:
However each @item.Name needs to be within inverted commas and separated by a coma, but I can't see how to do this. I've tried various things but none seem to work. In its current format it does pass out a list of items however they aren't separated by inverted commas and commas.
Does anyone have any ideas!
Many thanks
Hi,
There are various ways to do this, however working with your current sample you could update your js to:
Or if you used a typed model you could do something like:
You could also potentially create a basic service endpoint and use ajax.
Cheers
Many thanks for your help, I will give it a go with that!
Kind regards
That worked a treat, I used the following:
...and I am now returning all of my child pages in the Jquery string correctly and the auto complete works too.
Kind regards
is working on a reply...