Page 1 of 1

Add many entry to a subform

Posted: Mon Jun 06, 2022 12:03 am
by damien
Hello,

I have a subform that is used to manage a NxN join table.
So the subform mainly contains a lookup object.
Adding a new entry to the subform correspond to choose an element from the lookup objects.
The lookup object open the browse form to chose the element.
Every thing is ok at that point.

The only draw back is that if the user wants to add 5 rows, he must click the lookup, search elements, select in the browse.
It returns to the subform adding the element and restart 4 more times.

I wonder if there is a better solution for the user to open a kind of browse form with checkbox on each lines, select all the elements he wants, close that windows and all the selected elements will be added to the sub form.

Do you think that such behaviors is possible with nuBuilder ?

Re: Add many entry to a subform

Posted: Tue Jun 07, 2022 11:15 am
by kev1n
I created a demo here.

It's not perfect yet but it shows that it's basically possible with some custom code.
add_selected.png

Re: Add many entry to a subform

Posted: Tue Jun 07, 2022 1:30 pm
by damien
Waw, incredible, I will study it
Thank you very much !

Re: Add many entry to a subform

Posted: Tue Jun 07, 2022 6:15 pm
by damien
It works well, I have only modified a little bit the popuplateSubform function.
In the nuPad3 parameters I had an offset with the current number of existing lines.

Original code :

Code: Select all

        parent.nuGetLookupId(ids[i], sfName + nuPad3(i) + f, false);
Modified code :

Code: Select all

        parent.nuGetLookupId(ids[i], sfName + nuPad3(i+l-1) + f, false);
Every think works fine except a side effect on the afterinsertrow callback.
I am using the afterinsertrow of the subform object to update a display fields into the form using the lookup field values (as discussed in that thread).
I notice that with the multiple add the value of the foreign key in the lookup field is not readable or not yet present when the afterinsertrow callback is called.
In the "normal" single line add I read the lookup value using that JS code :

Code: Select all

var r = nuPad3(nuSubformObject(sfId).rows.length - 2);
var lookup_value = $('#' + sfId + nuPad3(r) + 'field_name').val();
With the multiple input the lookup_value is always a blank string.

Is it due to a fact that with that method the afterinsertrow is called before the value is updated ?
Or does the way I use to access the value is not the good one ?
What do you think about ?

Re: Add many entry to a subform

Posted: Thu Jun 09, 2022 5:46 am
by kev1n
damien wrote: Tue Jun 07, 2022 6:15 pm Is it due to a fact that with that method the afterinsertrow is called before the value is updated ?
I think that's the case since we're first adding new rows which triggers afterinsertrow and the lookup is only populated after calling nuGetLookupId()
I am using the afterinsertrow of the subform object to update a display fields into the form using the lookup field values (as discussed in that thread).
How about calling this update code also after populating the lookups?

Re: Add many entry to a subform

Posted: Thu Jun 09, 2022 1:01 pm
by damien
kev1n wrote: Thu Jun 09, 2022 5:46 am How about calling this update code also after populating the lookups?
Oh my good, you so right.
And cherry on the cake it is easier to update the parent field with the child data because the are already loaded into the child page !
So I just have to search it into the HTML page with jQuery, no need to go back to the SQL :P

Thanks that is perfect :thumb:

Re: Add many entry to a subform

Posted: Thu Jun 29, 2023 1:08 pm
by ricklincs
This is a useful add many entries to a subform demo. Is it possible to add a select/deselect all button to the action buttons on top of the form so you have Select/Deselect All rows as well as the Add Selected button?

Re: Add many entry to a subform

Posted: Thu Jun 29, 2023 1:26 pm
by kev1n
Add a new button using nuAddActionButton() to select all checkboxes:

Code: Select all


nuAddActionButton('selectAll', 'Select All', 'selectAll');

function selectAll() {
   $('input[name="mycheckbox[]"]').prop('checked', true);
}


Re: Add many entry to a subform

Posted: Thu Jun 29, 2023 2:33 pm
by ricklincs
Thanks kev1n, works a treat.

Re: Add many entry to a subform

Posted: Thu Jul 27, 2023 1:46 pm
by ricklincs
Kev1n is there anyway of using this code to add many enteries to a subform after you have added, saved and then edit it again to add more subform items? I added test1, test2 and test3 by using the browse_select_mulitple_lookup above and it works great. I saved the form and then edited it again to add additional items test4, test5 and test6, what it does is try to overriden the 1st 3 items(test1,test2,test3). Thanks for your help as always.