1. Fabrik 3.9 has been released. If you have updated Joomla to 3.9, this is a required update.
    Dismiss Notice

Dynamically assign values in repeat groups

Discussion in 'Community' started by louis037, Feb 19, 2018.

  1. louis037

    louis037 Member

    Level: Community
    Hello everyone,
    I have a form with repeat groups.
    In each group, I'm Looking for setting values from a database request (array of values) : each row value in a field (or display) in each repeat group.
    Has anyone a solution for that? I didn't manage to do it in JS, neither with calc element....
    Thank you very much for helping me.
     
  2. louis037

    louis037 Member

    Level: Community
    Hello everybody, I'm sorry to insist, but this post will may be pay attention on this topic... I'm unable to manange to do it... I think it's a simple "case for dummies", but I don't know the point of view that I have to adopt, and how to do it.
     
  3. louis037

    louis037 Member

    Level: Community
    Hello everybody, has anyone an idea to solve my problem? Thanks!
     
  4. wezetel

    wezetel Member

    Level: Community
    Hi Louis,
    to be honest I really don't understand, what you want to achive without knowing more details. Usually you can achive database access with the calc element...
     
    Last edited: Feb 25, 2018
  5. louis037

    louis037 Member

    Level: Community
    Hi Wezetel, Thanks for the reply.
    To try to be more clear : I have repeated fields in my form. A calc element defines how many times repeat a group (from a database query like SELECT (COUNT...). This point works well. My problem is the following : In each group, I want to show a name from these database (the number of names is of course the previously calculated value). So, each row has to show a different name from the database in order to associate values to each name in the repeat group.
    I hope it is more clear?
    For me, it's a calc element, but I don't manage to do the calc to assign to each repeat group the wanted names. I Think I can associate the arrays with their value id... but how?? That is the big question for me.
     
  6. wezetel

    wezetel Member

    Level: Community
    Not really.
    I think you mean from the database table representing the repeat group? If yes, you probably entered the value through a field in your form. But then you already have this field. Hmm.... get lost...
    Wouldn't it be an idea to show with some screenshots, where your problem is?
     
  7. louis037

    louis037 Member

    Level: Community
    Here's a picture of it :
    upload_2018-3-1_12-3-17.png
     
  8. wezetel

    wezetel Member

    Level: Community
    The apprenant field seems to be a dropdown. So you have entered the names in the dropdown settings or you have a link to another table. Correct?
    And you want to get the name to put in somewhere else?
     
  9. louis037

    louis037 Member

    Level: Community
    The apprenant is in a dropdown from a databasejoin for the moment... That I want : replace the dropdown by a calc element (or display) to show the name of the apprenant automatically : one by one in each repeat group. I think I have to make two arrays : one for the repeat group and another for the trainees and make both corresponding on indexes to show it as I want. Maybe with a foreach loop using simultaneously calc and javascript on my element???
     
  10. wezetel

    wezetel Member

    Level: Community
    Slowly it is getting clearer.
    What I currently understand is:
    you have one list with the names of the apprenants.
    you have a second list 'Pilotage des acc?ss apprenants'
    you have a repeating group in the form of the second list and you want the repeating group to be filled automatically with the names of the first list.

    Is this understanding correct?
     
  11. louis037

    louis037 Member

    Level: Community
    Yes, it is!
     
  12. wezetel

    wezetel Member

    Level: Community
    So the problem is now clear! Difficult Birth...

    Will see if I (or somebody else) can come up with a solution now. I need to think....
     
  13. louis037

    louis037 Member

    Level: Community
    Thanks you very much!!! I managed to get indexed arrows of repeat groups and names, now, the job consists on show in a field (calc, display...) these values... Maybe a school case???
     
  14. Bauer

    Bauer Well-Known Member

    Level: Supporter
    I still don?t quite get the ?big picture? here. I?m confused as to why you use a dbj dropdown element for the Apprenant.

    And how is it determined which Apprenants belong in (or are shown as selected in) the repeat group? Or can it be any of them so long as it is limited in number by the ?R?p?tition? value?

    Was this element intended to be user-selectable? Can it be changed after the form is saved? If not ? and you still want to use a dbj element ? be sure to deny edit access to that element (so it would look just like a display or calc element).

    What do you mean by ?I want to get automatically?? Does each repeat group need to be added manually by the user? Is it required that the number of repeats is always the ?R?p?tition? value?

    Then, what is that ?Modules? element and how is it created and populated ? and how are those 2 elements related?

    If my guess is correct in regards to what you are trying to do, here is a suggestion?

    In the ?Pilotage des acc?ss apprenantsPilotage des acc?ss apprenants? form, include an ?Apply? button when new ? and hide the ?Submit? button. (Otherwise show the Submit button and hide the Apply button)

    Then, use the form PHP plugin to include some post processing PHP that will programmatically create the records needed for the repeating group (if the Apply button was used to submit the form) ? then refresh the form showing the included repeats. (So the user doesn?t have to do this)

    You could also use some css to hide those +- buttons for adding/removing the repeat groups (since they wouldn?t be needed if you programmatically create the repeats via php) ? and the repeats would be deleted if/when the form/row is deleted, so long as you have configured the list to delete the repeats.

    I could be way off-base here with my assumptions ? but I just thought I?d give it a try.:)
     
  15. louis037

    louis037 Member

    Level: Community
    Hello Bauer, I'll respond to each of your questions :
    • I use a database join, because I want to show a different Apprenant in each repeat group and I don't know how to manage to do it with a calc element.
    • The apprenants are selected with the previous databasejoins : "Missions" and "Magasins". So, I get the names of peoples, and my calc element "Repetition" calculates the number of these Apprenants who has to be show in the groups.
    • The only user-selectable element will be "Modules".
    • Does each repeat group need to be added manually by the user? -> The number of repeat groups are defined by the value in "R?p?tition" and it is set in backend as repeat number for the group.
    • "Modules" are a databasejoin with multi select. (My temporary solution that I want to replace with a calculation element that will display the values from my current drop-down list) These value get values from other database and are selected on demand in these form. (it will grant access to these modules)
    • When you suggest me to show the apply button, I thing it will not solve my problem : when loading the form and selecting the values in "Mission" and "Magasin", all the repeat groups have to be dynamically repeated from the calculated value in "Repeat" and the Apprenant has to be set automatically in each group. (the dropdown show the same number of items than calculated in "R?p?tition").
    • The use of PHP plugin doesn't seem to fit to my needs, because I need to have dynamically calculated values (repeats and apprenants).
    • Of course, I will hide the +- buttons because the are not needed.
    Thank you for having taken the time to read my post... I hope it is quite more clear...
     
  16. Bauer

    Bauer Well-Known Member

    Level: Supporter
    OK, I get now how you are populating the repeat groups. It's been a while since I used them and I forgot about that feature in the group parameters. So much for the php plugin idea.

    So are you trying to assure that the Apprenant in each consecutive repeat group 'automatically' uses the next consecutive index/label in that dbj? (Is that the 'automatic' part?)

    If the Apprenant names are being added dynamically, do you need to be concerned about the ordering of the labels used in either the the Apprenant dbj dropdown list (or the repeat list itself)?
    I.e. is the dbj dropdown (or repeat list) set to show the labels alphabetically?
     
  17. louis037

    louis037 Member

    Level: Community
    Bauer,
    The automatic part is, as you suggest to assign to each group the associated index in dbjoin. What I want to do is indexing in an array my groups and in a calc element assign the corresponding index. Ie.
    form_group_repeat___field[0] -> value [0]
    form_group_repeat___field[1] -> value [1]
    form_group_repeat___field[2] -> value [2]
    ...
    (I know how to get array of values) I think I have to index my calc elements in an array (How???) and make a loop foreach... Quite difficult for me
     
  18. Bauer

    Bauer Well-Known Member

    Level: Supporter
    Did you try just using the 'Default' setting in the Options for the dbj element?

    I'd have to set up a similar scenario in some test tables to get the exact syntax to use.
    But basically you would query the tables to get the next apprenant ID that has not yet been used in the current 'Pilotage des acc?ss apprenants' record - and return that ID as the 'Default' value.

    If that works it would work regardless the alphabetical order in both the dbj or repeat list - and hopefully it would allow you to set the dbj element to readonly so the dropdown doesn't even show - just the label would show.
     
  19. louis037

    louis037 Member

    Level: Community
    My dbjoin has a where :
    Code (Text):
    WHERE {thistable}.mission = '{access_control_learners___mission_raw}' AND {thistable}.shop = '{access_control_learners___store_raw}'
    and for the label :
    Code (Text):
    (SELECT firstname FROM `whitepages` WHERE `whitepages`.id = {thistable}.candidate),' ',(SELECT lastname FROM `whitepages` WHERE `whitepages`.id = {thistable}.candidate)
    I have not set an ORDER BY, I think It will good to do it in order to get correctly indexed values in case of changes. And more of that, it's more clear to order by name on the form.

    That I want at last is get the name of trainees automatically added in groups (one per group) as whe have dynamically defined the correct number of groups corresponding to the WHERE condition set for the dropdown, and groups are set to be duplicated this number of times in backend.
     
    Last edited: Mar 1, 2018
  20. Bauer

    Bauer Well-Known Member

    Level: Supporter
    I don't think you're going to be able to do what you want by using the standard Fabrik repeat group code.
    I've experimented with various options and the problem is always that when Fabrik adds multiple repeat groups all at once, like you are doing, it just clones the values of the last added repeat rather than recalculating an 'Eval' on the 'default' or calc element code for each new row, one at a time. This has always been the problem with repeat groups. You can't really make any dynamic changes to each new row being added because the system does not even know the ID of each row until after the form gets saved.

    So you either have to change those values after the form is painted via javascript - or do what I suggested originally and create the repeat group records manually using the php form plugin as the form loads. (The downside there - if done via php - would be that you would also have to recheck each existing row every time it is edited in case new Apprenants were added since the last edit.)

    What you could do is move the dbj element into the parent table, but hide it via css - then use that element to compare to existing Apprenant values in the repeat rows and add the correct labels there via javascript if any are new/missing.
     
    Last edited: Mar 1, 2018

Share This Page