Welcome to the nuBuilder Forums!

Register and log in to access exclusive forums and content available only to registered users.

onClick behavior change?

Questions related to customising nuBuilder Forte with JavaScript or PHP.
Post Reply
incoherence
Posts: 22
Joined: Sun May 08, 2022 2:36 pm
Has thanked: 9 times
Been thanked: 1 time

onClick behavior change?

Unread post by incoherence »

Hi, I have a strange issue...

I have a main form with a subform.
The subform has two lookup fields.
The first lookup field is already populated and no problems there.

When I click on the magnifying glass of the second lookup field, my aim is to filter the list of possible entries presented to the user based on what is filled in, in the first lookup field.

To do so, when the magnifying glass of the second lookup field is clicked, the onclick code sets a hash cookie 'prodIdOnThisSfRow' as follows:-

Code: Select all

var prodIdOnThisSfRow = nuSubformRowObject(event.target.parentElement.id, 'first_lookup_id').val();
nuSetProperty('prodIdOnThisSfRow', prodIdOnThisSfRow);
console.log('prodIdOnThisSfRow: ', prodIdOnThisSfRow);
The hash cookie is then used in an SQL WHERE clause to filter the (otherwise very long) list.

I say "my aim", but I previously had (and still have!) this working perfectly in an older code base (of nuBuilder 4.5).

The odd problem I am seeing is with a newer (Jan 2023) version of the codebase.
I don't know whether it has anything to do with the code base, but I cannot think of what else it could be.

With the working version, the onclick code is run when the magnifying glass of the second lookup field is clicked.

With the non-working version, nothing happens when the magnifying glass of the second lookup field is clicked. However, if I click on the *text* of the lookup field - the value that is populated from the database already - the onclick code is run (and fails - quite likely "event.target.parentElement.id" is not set up - but I am guessing).

FWIW, when the code runs, the following appears in the console (BTW, if I add a console.log() line before the other code, that output will appear before the error below) :-
nuform.js?ts=20230306020428:2172 Uncaught TypeError: Cannot read properties of undefined (reading 'slice')
at nuSubformRowNumber (nuform.js?ts=20230306020428:2172:43)
at nuSubformRowObject (nuform.js?ts=20230306020428:2179:28)
at HTMLInputElement.onclick (index.php:1:25)


I don't think that the above error info helps - the key question is: Why is the onclick code triggered by a different user action between the two systems?

The version I am having the issue with is this one:-
$ cat /var/www/html/80/version.txt
nuBuilder Forte 4.5

DB Version: V.4.5-2023.01.22.00
Files Version: V.4.5-2023.01.22.00


I guess I could move all my work to a newer version to see whether it helps, but I would like to eliminate all other possible causes of this issue before I start down that road. If I *have* to do that, I suppose I just back up the database to a single file using phpMyAdmin/mysqldump, git clone a new nuBuilder codebase in a new place using the latest from github, delete the database and restore it from the backup file, and then run the Database Upgrade tool in nuBuilder - is that correct?

Are there any known regressions that could cause this?
Or is there something I might have done myself, somehow...?
Thanks in advance for any help!
kev1n
nuBuilder Team
Posts: 4292
Joined: Sun Oct 14, 2018 6:43 pm
Has thanked: 71 times
Been thanked: 444 times
Contact:

Re: onClick behavior change?

Unread post by kev1n »

Hi,

What's the version of the working version?
kev1n
nuBuilder Team
Posts: 4292
Joined: Sun Oct 14, 2018 6:43 pm
Has thanked: 71 times
Been thanked: 444 times
Contact:

Re: onClick behavior change?

Unread post by kev1n »

Adding the line

Code: Select all

nuAddJSObjectEvents(id, obj.js);
after

Code: Select all

if (obj.label === 'Insert-Snippet') $('#' + id).css('font-size', '18px');
in nuform.js should fix it.
incoherence
Posts: 22
Joined: Sun May 08, 2022 2:36 pm
Has thanked: 9 times
Been thanked: 1 time

Re: onClick behavior change?

Unread post by incoherence »

Hi kev1n, thank you for finding the issue and providing the fix - _very_ much appreciated!

BTW/FYI... this part still happens, even after your change (it's not impacting me because the code that gets run fails, as stated, but I don't think the onclick code should be running when the user clicks on the "text" of the lookup field):-

> if I click on the *text* of the lookup field - the value that is populated from the database already - the onclick code is run (and fails - quite likely "event.target.parentElement.id" is not set up - but I am guessing).
incoherence
Posts: 22
Joined: Sun May 08, 2022 2:36 pm
Has thanked: 9 times
Been thanked: 1 time

Re: onClick behavior change?

Unread post by incoherence »

Just for the record, kev1n's commit fixing this is:-
[Fixed] Trigger JS Events for Lookup Button
kev1n
nuBuilder Team
Posts: 4292
Joined: Sun Oct 14, 2018 6:43 pm
Has thanked: 71 times
Been thanked: 444 times
Contact:

Re: onClick behavior change?

Unread post by kev1n »

New attempt to fix this issue.
  • onclick is now not triggered anymore for the code field
  • onclick is only triggered for the button and no other events fire
Modified function in nuform.js:

Code: Select all

function nuAddJSObjectEvents(id, events) {

	const element = document.getElementById(id);

	for (let eventObj of events) {
		let code = element.getAttribute(eventObj.event) || '';
		let ev = eventObj.event;
		if (['beforeinsertrow', 'afterinsertrow', 'clickdelete'].includes(ev)) {
			ev = 'data-nu-' + ev;
		} else if (element.classList.contains('nuLookupCode') && ev === 'onclick') {
			continue;
		} else if (element.classList.contains('nuLookupButton') && ev !== 'onclick') {
			continue;
		}
		code += ';' + eventObj.js;
		element.setAttribute(ev, code);
	}

}
Position changed where this line is added:

nuAddJSObjectEvents(id, obj.js);
Post Reply