Page 1 of 2

Subform with specific numbers of records

Posted: Mon Mar 30, 2020 11:29 pm
by gerese
Hi,
How can I limit the subform for only 10 records ?

Thanks

Re: Subform with specific numbers of records

Posted: Tue Mar 31, 2020 9:23 am
by kev1n
Modify the Browse SQL and add a LIMIT 10 to the SQL.

E.g.

Code: Select all

SELECT * FROM your_table LIMIT 10

Re: Subform with specific numbers of records

Posted: Tue Mar 31, 2020 9:39 am
by Janusz
Hi,
One of the solution is to work directly on the MariaDB database tables.
You can do that by setting sob_subform_add flag directly in the zzzzsys_object table.
So in the PHP BEFORE EDIT you need to place php code which in the first step will check the qty of subform records and later depending on the value will the set sob_subform_add to 1 or 0
(maybe you should add autosave operation after every single record is added to avoid multiple entries - for example: someone can add 20records and save afterwards)

Code belowe it's just a quick trial on my application - and it's working fine. Of course in you case you need to rework it acc. to your table structure (to count records).

Code: Select all

// Place this in the PHP BE - adjust to your specific mysql tables

// count how many raws has subform for specific record
$s = "SELECT COUNT(*) AS qty FROM connection WHERE con_part='#parts_id#'";
$t  = nuRunQuery($s);
$qty  = db_fetch_object($t)->qty;

// set up  the addable value for the subform depending on the quantity value
if ($qty>10)
{$s  = "UPDATE zzzzsys_object SET sob_subform_add=0 WHERE sob_all_id='sub_FF_parts'";
$t  = nuRunQuery($s);}
else 
{$s  = "UPDATE zzzzsys_object SET sob_subform_add=1 WHERE sob_all_id='sub_FF_parts'";
$t  = nuRunQuery($s);}

Re: Subform with specific numbers of records

Posted: Tue Mar 31, 2020 9:40 am
by gerese
Thanks for reply Kev1n,
In fact I want for this subform in edit mode.
After 10 recordings, the empty row will be deactivated or disappear.

Thanks

Re: Subform with specific numbers of records

Posted: Tue Mar 31, 2020 2:22 pm
by kev1n
Add this function to your form's custom code:

Code: Select all


function subFormLimitNumberOfRows() {
    
   const MAX_ROWS = 8;
   var sf = 'your_subform_id'; // <---- replace with you subform object id         
   if (nuSubformObject(sf).rows.length > MAX_ROWS) {
	var prefix = sf + nuPad3(i);
	$("id^='" + prefix + "_" + MAX_ROWS + "").remove();   
   }
		  
}
Next, add an afterinsertrow event to your subform as shown here:

https://forums.nubuilder.cloud/viewtopic. ... row#p15460

and call the function from there:

Code: Select all

subFormLimitNumberOfRows()
In addition, call subFormLimitNumberOfRows(); when the form is loaded.

Re: Subform with specific numbers of records

Posted: Tue Mar 31, 2020 2:33 pm
by gerese
Thanks Janusz, I would have liked something in javascript, simpler.

PS - We posted both at the same time :lol:

Re: Subform with specific numbers of records

Posted: Tue Mar 31, 2020 2:40 pm
by gerese
Cool Kev1n , you save my life , this is what I want .

Re: Subform with specific numbers of records

Posted: Tue Mar 31, 2020 4:38 pm
by kev1n
gerese wrote:Cool Kev1n , you save my life , this is what I want .
You're welcome :)

PS: If you really want to prevent a user from adding more than 10 rows, you need to do a server-side check. E.g. use the PHP Before Save event and retrieve the number of rows. If they exceed a certain number, abort the saving.

Re: Subform with specific numbers of records

Posted: Sun Apr 05, 2020 4:39 pm
by gerese
Please Kevin, I need some help. :? :? :?
Subform id is "brn_gestiune_sf".
I set up: "const MAX_ROWS = 4"
After 4 records, the error appears.

Re: Subform with specific numbers of records

Posted: Sun Apr 05, 2020 5:17 pm
by kev1n
Try this one:

Code: Select all

function subFormLimitNumberOfRows() {
   
   const MAX_ROWS = 4;
   var sf = 'brn_gestiune_sf'; 
   if (nuSubformObject(sf).rows.length > MAX_ROWS) { 
       $("[id^='" + sf + nuPad3(MAX_ROWS) + "']").remove();
   }
       
}