Page 1 of 1

how to have a hash_cookie from browse request (not primary key)

Posted: Mon Apr 10, 2023 12:38 am
by fmuller
Hello,

First message ... so to start, thanks for your work !

I've got a select request used in my browse form which connect several table (with some condition to have a unique result)

My browse form, which is linked to tableA, display this:
tableA_id (PK)
tableA_data
tableB_id (with a width 0) so that user will not see it

but the only hash_cookie seen in edit form are linked to tableA nothing from tableB exist.

I want to use this hash cookie to be able to run a secondary edit form (through a button) with tableB_id but I can't find a way to do so.

Regards

Re: how to have a hash_cookie from browse request (not primary key)

Posted: Mon Apr 10, 2023 8:26 am
by kev1n
Hello,

It's great to hear that you are using nuBuilder and finding it useful!

Regarding your question, if I understand correctly, you have a query in your browse form that joins several tables, including tableA and tableB, and you want to be able to open a secondary edit form on a button click.

You can add the following JavaScript code in the Custom Code/Browse section of your form. It creates a button for each browse row and opens another form on a button click. The pk of the second form is stored in an attribute called "data-table-b-id".

Code: Select all

function createButton(target, index, data) {

    const buttonId = `browse_button${index}`;
    let btn = $(`<button id='${buttonId}' type='button' data-table-b-id="${data}" class='nuActionButton'><i class='fa fa-file-arrow-down'></i>&nbsp;</button>`);
    btn.css('padding-left', '9px');

    $(target).html(btn);

    btn.on('click', function(e) {
		e.stopPropagation(); 
		const tableBID = $(this).attr("data-table-b-id"); 
	    nuForm('6433a3e41a0d354', tableBID, '', '', '2'); // <-- Replace 6433a3e41a0d354 with your form Id
    });

}

function addBrowseButtons(column) {

    $("[data-nu-column='" + column + "']").each(function(index) {

        const pk = $(this).attr('data-nu-primary-key');
        if (typeof pk !== "undefined") {
            const row = $(this).attr('data-nu-row');
            const data = $('#nucell_' + row + '_1').html(); // <-- E.g. pk of 2nd form is in the 1 column

            createButton(this, index, data);
        }
    })

}

addBrowseButtons(0); // add a button in column 1
I hope this helps! Let me know if you have any further questions.

Re: how to have a hash_cookie from browse request (not primary key)

Posted: Mon Apr 10, 2023 10:24 am
by fmuller
Hello,
Thanks for your fast answer!
Yes you correctly understood.

Uhm what I wanted to do was to have the button in the edit form, not in the browse one, hence the fact that I wanted to export the result as an hash_cookie

to explain more my case:
Table USER : contains user_id, user_data(severals), user_emergency_contact_id -> per user
Table SEASON: season_id, season_year -> contains the current and previous season for example
Table CONTRIBUTION : contains contrib_id, contrib_season_id, contrib_user_id, contrib_time_slot_id contrib_data -> will contains user per season if they are registered for this season and on which time slot they are registred
Table TIME_SLOT: contains id, slot
Table EMERGENCY_CONTACT: contains id, data

so the select cover each table with season_year and time_slot as condition so if a user is not registered to the season&time_slot it will not appear in the browse form

I only display name/surname in the browse form (which is linked to USER table) but when I go to the edit form I display the whole data of the user.
If we want to change the emergency contact, we have a button since the id is in USER table and a hash_cookie exists to put it in the RUN tab,

I wanted also to have the same thing for the contribution in the same page however there is no hash_cookie since it is not in the USER table,
The only ugly solution I think about is to duplicate the select in a display input and then onchange of this value to use some javascript to create the hash_cookie (but I'm not even sure this would work)
I also tried to change recordid on the fly in BE php script but It can't work since recordid is already extracted before (I looked at nuBuilder source code).

Regards

Re: how to have a hash_cookie from browse request (not primary key)

Posted: Mon Apr 10, 2023 10:50 am
by kev1n
By using nuSelectBrowse(), you can retrieve the clicked row/element and store a value in a window variable (e.g. your_id):

Code: Select all

function nuSelectBrowse(event, element) {

    const primaryKey = $(element).attr('data-nu-primary-key');
    if (primaryKey) {
        const row = $(element).attr('data-nu-row');
        const column = 1; // change column number accordingly
        window.your_id = $(`div[data-nu-row='${row}'][data-nu-column='${column}']`).html();
        nuForm(nuFormId(), primaryKey, '', '', '0');
    }

}
Then when the form is loaded (Custom Code, Edit section), retrieve "your_id" and save it in a Hash Cookie:

Code: Select all

nuSetProperty('your_id', window.your_id);

Re: how to have a hash_cookie from browse request (not primary key)

Posted: Mon Apr 10, 2023 9:33 pm
by fmuller
Thanks a lot !
I was able to do it.
I put your first part in Browse&Edit JS part and your second part in edit JS section,
I needed to modify the second par, to be able to use it as an hash cookie (I suppose this is needed to transfer from JS to PHP ?) :

Code: Select all

if ( nuGetProperty('your_id') == undefined) {
    nuSetProperty('your_id', window.cot_id);
    nuGetBreadcrumb();
}
Regards