Page 1 of 1

Getting the values in a list

Posted: Fri Mar 07, 2014 9:30 pm
by Tom Mccann

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?


Getting the values in a list

Posted: Fri Mar 07, 2014 9:50 pm
by Maryna Brodina

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


Getting the values in a list

Posted: Fri Mar 07, 2014 10:11 pm
by Tom Mccann

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


Getting the values in a list

Posted: Sat Mar 08, 2014 12:52 am
by Tom Mccann

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;
});


Getting the values in a list

Posted: Mon Mar 10, 2014 12:33 am
by Illya Stepanov

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.