MAKE A FIELD READ-ONLY AFTER TYPING

Discussions related to customizing hooks. Hooks are documented at http://bigprof.com/appgini/help/advanced-topics/hooks/
Post Reply
pasbonte
Veteran Member
Posts: 178
Joined: 2013-02-06 09:49

MAKE A FIELD READ-ONLY AFTER TYPING

Post by pasbonte » 2024-09-14 18:07

Hello !
I WOULD LIKE TO MAKE A FIELD READ-ONLY AFTER TYPING AND SAVING
HOW TO DO IT?
THANK YOU

pbottcher
AppGini Super Hero
AppGini Super Hero
Posts: 1667
Joined: 2018-04-01 10:12

Re: MAKE A FIELD READ-ONLY AFTER TYPING

Post by pbottcher » 2024-09-14 18:24

Hi,

you can add to the hooks/TABLENAME-dv.js file some code that checks if this is a valid record (not new), which you can identidy by the SelectedID being set. Then change your field to readonly via jquery. This works when you saved the record. Not sure what you mean by "after typing".

Something like

Code: Select all

if ($j('[name="SelectedID"]').val() != '') $j('#FIELDNAME').prop('readonly',true);
Make sure that you still check in the after_update hook if someone manipulated the field.
Any help offered comes with the best of intentions. Use it at your own risk. In any case, please make a backup of your existing environment before applying any changes.

pasbonte
Veteran Member
Posts: 178
Joined: 2013-02-06 09:49

Re: MAKE A FIELD READ-ONLY AFTER TYPING

Post by pasbonte » 2024-09-15 12:01

Hello
This code works very well and it's simple, thank you
But what if I have several fields?
I tried with the help of chatgpt:

Code: Select all

 // Vérifier si on est en mode édition d'un enregistrement existant
    var selectedID = $j('[name="SelectedID"]').val();

    // Si l'enregistrement existe (édition d'un enregistrement, pas création)
    if (selectedID != '') {
        // Liste des champs à vérifier
        var fields = ['septembre', 'octobre', 'novembre'];

        // Boucle sur chaque champ
        fields.forEach(function(fieldName) {
            var field = $j('[name="' + fieldName + '"]');  // Sélection du champ par son nom

            // Si le champ a une valeur non vide, on le rend en lecture seule
            if (field.val() !== '') {
                field.prop('readonly', true);  // On rend le champ en lecture seule
            }
        });
    }
});



pasbonte
Veteran Member
Posts: 178
Joined: 2013-02-06 09:49

Re: MAKE A FIELD READ-ONLY AFTER TYPING

Post by pasbonte » 2024-09-15 12:24

I'll explain better what I want:
I have a form with 9 input fields
SEPTEMBER
OCTOBER
NOVEMBER
DECEMBER
JANUARY
FEBRUARY
MARCH
APRIL
MAY
JUNE
At the end of each month users enter numeric values, I want it to be blocked after entering it and therefore read-only to prevent a modification.
The code provided first works fine for September but if I enter for October it doesn't work.

pasbonte
Veteran Member
Posts: 178
Joined: 2013-02-06 09:49

Re: MAKE A FIELD READ-ONLY AFTER TYPING

Post by pasbonte » 2024-09-15 12:30

Code: Select all


<?php
function GES_HSE_2425_dv($selectedID, $memberInfo, &$html, &$args) {
    $html .= '<script>
        document.addEventListener("DOMContentLoaded", function() {
            // Liste des champs de saisie (nommés par mois)
            var mois = ["SEPT", "OCT", "NOV", "DEC", "JAN", "FEV", "MAR", "AVR", "MAI", "JUI"];
            
            // Boucle sur chaque champ de saisie
            mois.forEach(function(moisName) {
                var champ = $j("[name=\'" + moisName + "\']"); // Sélectionner le champ par son nom
                if (champ && champ.val() != "") { // Si le champ contient une valeur
                    champ.prop("readonly", true); // Rendre le champ en lecture seule
                }
            });
        });
    </script>';

    return TRUE;
}

pasbonte
Veteran Member
Posts: 178
Joined: 2013-02-06 09:49

Re: MAKE A FIELD READ-ONLY AFTER TYPING

Post by pasbonte » 2024-09-15 18:36

I have the impression that javascript doesn't work

pbottcher
AppGini Super Hero
AppGini Super Hero
Posts: 1667
Joined: 2018-04-01 10:12

Re: MAKE A FIELD READ-ONLY AFTER TYPING

Post by pbottcher » 2024-09-15 19:25

Hi,

check you fieldnames. It is a bit confusing as in every post I read a different name:
octobre vs OCTOBER vs OCT
You need to specify the correct fieldname as defined in you app for the table.
Any help offered comes with the best of intentions. Use it at your own risk. In any case, please make a backup of your existing environment before applying any changes.

pasbonte
Veteran Member
Posts: 178
Joined: 2013-02-06 09:49

RESOLVED MAKE A FIELD READ-ONLY AFTER TYPING

Post by pasbonte » 2024-09-18 07:24

Hello
All my apologies, messages and requests were not clear.
With the help of a young programmer whom I thank, I solved the problem with this code if it can help another user.

One thing remains to be done: allow the ADMINS to modify or enter, I will look into the problem.

in tablename-dv.js

Code: Select all

var mois = ["SEPT", "OCT", "NOV", "DEC", "JAN", "FEV", "MAR", "AVR", "MAI", "JUI"];

// Parcourir la liste des mois
mois.forEach(function (month) {
    // Sélectionner chaque input par son attribut "name"
    var input = document.querySelector(`input[name="${month}"]`);
    // Si l'input est rempli, ajouter la propriété readonly
    if (input && input.value.trim() !== "") {
        input.setAttribute('readOnly', 'true');
    }
});
I don't know javascript at all, I'm more php...

Last tip: clear the cache in javascript

Code: Select all

// Exemple de requête pour contourner le cache en ajoutant un paramètre unique
fetch('hooks/GES_HSE_2425-dv.js?v=' + new Date().getTime())
    .then(response => response.text())
    .then(data => {
        // Utiliser les données ou mettre à jour le contenu
        console.log(data);
    });


Post Reply