Tom Mccann
Posts: 0
Joined: Sun Nov 03, 2013 8:11 am

Getting the values in a list

I'm writing some code to work out if a value is in a list. The list is populated by a REST query to the database.

I'm using a jquery selector on the class of the label within each list item. Then iterating the innerHTML to get the value. The code is:

$('.label').each(function (){
if (this.innerHTML == newCategory) categoryExists = true;
});

I can't help feeling that this is a clumsy way of doing it. Is there a simpler way of obtaining the values in a list?

Maryna Brodina
Posts: 0
Joined: Thu Apr 05, 2012 7:27 am

Getting the values in a list

Hello! It's a good solution, especially if it works for you.

Tom Mccann
Posts: 0
Joined: Sun Nov 03, 2013 8:11 am

Getting the values in a list

The first thing I tried was mapping the database results returned via the REST query service. I mapped $ to a local storage variable called category1Data. When the page loaded, I looked for the local storage variable and there was nothing there.

I had expected to see the JSON array of database records but there was nothing at all. The local storage variable hadn't even been created. Can you explain why?

Image

Tom Mccann
Posts: 0
Joined: Sun Nov 03, 2013 8:11 am

Getting the values in a list

I eventually settled on the jquery code: $("[name='label']").each(function () {...

A bit of background. There is a Label control inside the List control. The Label is mapped to the text value that is returned by the REST service query. The label has the name 'label' (not very imaginative I know). The HTML generated by Appery for each label is like this:

code
<div name="label" id="refCategory1_label_0" dsid="label" data-role="appery_label" class="label refCategory1_label " idx="0">Pizzas<>
/code

When my button tries to add a new value to the list, I check whether the value is already in the list. I use jquery to iterate over the values like this:

$("[name='label']").each(function () {
if ($(this).html() == newCategory) categoryExists = true;
});

Illya Stepanov
Posts: 0
Joined: Mon Mar 18, 2013 8:48 am

Getting the values in a list

Hi Tom,

Thanks for your question and investigation you've done.

Besides you should to know about default template list item that's exists on the page.

To avoid selecting and iterating through it, please add narrow selector:
prejQuery("li:not([_tmpl] [name='label']")/pre

Regards.

Return to “Issues”