Count items next dynamic list

Post any questions you have about using the Verj.io Studio, including client and server-side programming with Javascript or FPL, and integration with databases, web services etc.

Moderators: Jon, Steve, Ian, Dave

DannyWild
Ebase User
Posts: 4
Joined: Wed Jun 20, 2012 6:19 am

Count items next dynamic list

#1

Postby DannyWild » Wed Oct 02, 2013 9:27 am

We have a tree of 3 dynamic lists each filled (or not) depending on choices made before.

Code: Select all

-- Choice 1
  -- Choice 2
    -- Choice 3
Choice 1 and 2 have always more values but depending of choice 2 there are items in the list of choice 3 or not. Now we do it by doing an extra (exactly) the same query and use $fetch_count, but this way we have 2 database requests instead of 1.

Now did I found the following javascript option:
controls.FIELDCONTROL.field.list.getDisplayValues(false).length
That gives the number of items perfectly after loading.

But I use the option as validation or on change script at Choice 2 the result is always 0 because the dynamic lists are (re)fetched later.

How can I include this javascript to hide choice 3 if the list is empty?

Sjoerd
0 x

Jon
Moderator
Moderator
Posts: 1342
Joined: Wed Sep 12, 2007 12:49 pm

#2

Postby Jon » Wed Oct 02, 2013 1:05 pm

Normally dynamic lists are built during the generation of the HTML output for a page, and this occurs after any scripts have been executed, so it's difficult to check the number of entries in a list. You've got two options to get around this:

1. Set the property when list is built to 'Never' and build the dynamic list yourself in Javascript code e.g.

Code: Select all

fields.MY_LIST_FIELD.list.buildList();
2. Same as 1 but don't use dynamic lists at all. Instead, issue your own SQL from a script and build a custom list using Javascript code. e.g.

Code: Select all

var list = fields.MY_LIST_FIELD.createCustomList();
list.add(listEntry1);
// etc
In both cases you can check the number of list items and hide the third list depending on the result.

Regards
Jon
0 x

DannyWild
Ebase User
Posts: 4
Joined: Wed Jun 20, 2012 6:19 am

#3

Postby DannyWild » Tue Oct 08, 2013 6:47 am

Thanks, that did the trick.
0 x

Joe Blake
Ebase User
Posts: 44
Joined: Sun Sep 15, 2013 6:26 pm

How do you figure this out....

#4

Postby Joe Blake » Tue Oct 08, 2013 7:07 pm

Jon,

Is this trick something you have done before or did you just make it up?

And, if it is something you did before, how do you remember it?
0 x
New Potential User.

Jon
Moderator
Moderator
Posts: 1342
Joined: Wed Sep 12, 2007 12:49 pm

#5

Postby Jon » Wed Oct 09, 2013 4:11 pm

No trick really. For the most part, script programming is fairly easy to understand in that you are working with objects - fields, controls, tables, resources etc and when a script does something to one of these objects, it happens instantaneously. Dynamic Lists are a little bit different (maybe this is a little tricky) in that the system has built-in behavior to build these lists after all the scripts have run. So the advice here was to stop this built-in behavior so that the programmer could take control.

How do I know this: well I should because I've been doing this a while. I'm more concerned with how we let everybody else know: the answer is a combination of documentation, tutorials, this forum etc, but most importantly the functionality should be as intuitive as possible so it's what people expect and when they go looking for something, it's all there to find. That's the design objective but maybe we didn't quite achieve it in this case.
0 x


Who is online

Users browsing this forum: No registered users and 31 guests