Page 1 of 2

Readonly + Select Field Combine

Posted: Sat Apr 03, 2021 2:39 pm
by Olikun
Hello,

in Germany it is a public holiday (Easter). Nobody has to work and I have time to work on my project.


I have a new question


I have prepared a nice picture again.

I have a select field with the choice:

Priginal
BBQ
Chili

I want the fields for BBQ and Chilli to be read-only when "Original" is selected.

If the field "BBQ" is selected, the other fields must be write-protected.

Is that possible?
Can the fields change live?
or does the form have to be updated?


Image

Re: Readonly + Select Field Combine

Posted: Sat Apr 03, 2021 3:49 pm
by kev1n
Hi,

Add an onchange event to your select "Sorte" and call typeChanged()
onchange.png
In your form's Custom Code:

Code: Select all

var arrTypes = {
    Original: ['2', '3'],  // <---- replace 2, 3 with your object IDs
    BBQ: ['4', '5'], // <---- replace 4, 5 with your object IDs
    Chili: ['6', '7']  // <---- replace 6, 7 with your object IDs
}

function enableType(Type, enable) {
    for (f in arrTypes[Type]) {
        nuEnable(arrTypes[Type][f], enable);
    }
}

function enableTypes(o, b, c) {
    enableType('Original', o);
    enableType('BBQ', b);
    enableType('Chili', c);
}

function typeChanged() {

    var type = $("#1 option:selected").text();  // <---- replace 1 with your select's object ID.
    switch (type) {
        case "Original":
            enableTypes(true, false, false);
            break;
        case "BBQ":
            enableTypes(false, true, false);
            break;
        case "Chili":
            enableTypes(false, false, true);
    }

}
Replace 2,3,4, etc. with your Object IDs.
sc.png

Re: Readonly + Select Field Combine

Posted: Sat Apr 03, 2021 4:30 pm
by Olikun
Thank you
it works right away

but

If I edit the form it works.

I save and close the form and open it again.
Then the fields are active again.

The fields are only read only again when the select field is used.


can we somehow change that?

We can also make the fields hidden if it works better?

Re: Readonly + Select Field Combine

Posted: Sat Apr 03, 2021 4:42 pm
by kev1n
I was already expecting that question ;)

Add this to your form's Custom Code:

Code: Select all

if (nuFormType == 'edit') {
    typeChanged();
}

Re: Readonly + Select Field Combine

Posted: Sat Apr 03, 2021 5:03 pm
by Olikun
where exactly do i add it?

I wrote it like this in the form's custom code

Unfortunately it does not work :/

Code: Select all

if (nuFormType == 'edit') {
    typeChanged();
}

var arrTypes = {
    Original: ['1W14', '1W17'],  // <---- replace 2, 3 with your object IDs
    BBQ: ['1W15', '1W18'], // <---- replace 4, 5 with your object IDs
    Chili: ['1W16', '1W19']  // <---- replace 6, 7 with your object IDs
}

function enableType(Type, enable) {
    for (f in arrTypes[Type]) {
        nuEnable(arrTypes[Type][f], enable);
    }
}

function enableTypes(o, b, c) {
    enableType('Original', o);
    enableType('BBQ', b);
    enableType('Chili', c);
}

function typeChanged() {

    var type = $("#1W11 option:selected").text();  // <---- replace 1 with your select's object ID.
    switch (type) {
        case "Original":
            enableTypes(true, false, false);
            break;
        case "BBQ":
            enableTypes(false, true, false);
            break;
        case "Chili":
            enableTypes(false, false, true);
    }

}

Re: Readonly + Select Field Combine

Posted: Sat Apr 03, 2021 5:05 pm
by kev1n
Sorry, it has to be like that:

Code: Select all

if (nuFormType() == 'edit') {
    typeChanged();
}

Re: Readonly + Select Field Combine

Posted: Sat Apr 03, 2021 5:27 pm
by Olikun
Thank you
it works


But one more problem

I have to explain


I have two forms, but it's only one database.

Department 1 = form number 1

Department 2 = form number 2


The varieties Original / BBQ / Chili are in form number 1 a "Select Field"
The employee can edit it

In form number 2 it is not a select field but a text field (readonly). Only the value is displayed.
The employee cannot edit it.


I took a nice picture again :D

Image

Re: Readonly + Select Field Combine

Posted: Sat Apr 03, 2021 5:34 pm
by kev1n
Replace

Code: Select all

$("#1W11 option:selected").text()
With

Code: Select all

$("#sorte_text_field_ID").val()
to make it work with a Text Input

Re: Readonly + Select Field Combine

Posted: Sat Apr 03, 2021 5:42 pm
by Olikun
perfect again


Thank you

it works great

Re: Readonly + Select Field Combine

Posted: Sat Apr 03, 2021 7:45 pm
by Olikun
New problem has occurred

I have 2 halves on the form with different Readonly + Select Field Combine

Image


I tried to adapt the code, but it doesn't work
I duplicated the code and adjusted the field IDs

Code: Select all

// WAGEN 1


if (nuFormType() == 'edit') {
    typeChanged();
}

var arrTypes = {
    Original: ['1W14'],  // <---- replace 2, 3 with your object IDs
    BBQ: ['1W15'], // <---- replace 4, 5 with your object IDs
    Chili: ['1W16']  // <---- replace 6, 7 with your object IDs
}

function enableType(Type, enable) {
    for (f in arrTypes[Type]) {
        nuEnable(arrTypes[Type][f], enable);
    }
}

function enableTypes(o, b, c) {
    enableType('Original', o);
    enableType('BBQ', b);
    enableType('Chili', c);
}

function typeChanged() {

    var type = $("#1W11 option:selected").text();  // <---- replace 1 with your select's object ID.
    switch (type) {
        case "Original":
            enableTypes(true, false, false);
            break;
        case "BBQ":
            enableTypes(false, true, false);
            break;
        case "Chili":
            enableTypes(false, false, true);
    }

}


// WAGEN 2


if (nuFormType() == 'edit') {
    typeChanged();
}

var arrTypes = {
    Original: ['1W24'],  // <---- replace 2, 3 with your object IDs
    BBQ: ['1W25'], // <---- replace 4, 5 with your object IDs
    Chili: ['1W26']  // <---- replace 6, 7 with your object IDs
}

function enableType(Type, enable) {
    for (f in arrTypes[Type]) {
        nuEnable(arrTypes[Type][f], enable);
    }
}

function enableTypes(o, b, c) {
    enableType('Original', o);
    enableType('BBQ', b);
    enableType('Chili', c);
}

function typeChanged() {

    var type = $("#1W21 option:selected").text();  // <---- replace 1 with your select's object ID.
    switch (type) {
        case "Original":
            enableTypes(true, false, false);
            break;
        case "BBQ":
            enableTypes(false, true, false);
            break;
        case "Chili":
            enableTypes(false, false, true);
    }