Will not save changes when column is hidden.

Will not save changes when column is hidden.

Erik SkovErik Skov Posts: 33Questions: 6Answers: 2

Trying to make 1 (or more) fields disappear based upon a boolean. This is working.
Problem: Edit will not save changes when column is hidden.

Two groups of people who can create or edit entries.
Group A can see and edit everything.
Group B should not see or edit one or more columns.

The code I have works fine for group A. For group B, the columns are hidden when I want. The problem is Edit / Update process hangs when you click the Update button. You have to x out of the screen. Group B cannot update.

js file:

showHourly  - permission to see the hourly wage

        var editor = new $.fn.dataTable.Editor({
...
                {
                    "label": "Hourly Wage:",
                    "name": "employees.hourly_wage"
                }

        //customize forms for edit and create
        editor.on("initEdit",
                function () {
                    if (!showHourly){
                        editor.hide([
                                "employees.hourly_wage"
                            ]);
                    }
                }
        );
        editor.on("initCreate",
                function () {
                    editor.hide([
                                "employees.hourly_wage"
                            ]);
                }
        );

        var hideMe1 = [];
        var hideMe2 = [8];
        var table = $('#employees').DataTable({
...
            columns: [
                { "data": "last_name" } // 0
                , { "data": "first_name" }  //1
...
                , { "data": "employees.hourly_wage",    // 8
                    "defaultContent": ""                }
                
                , { "data": "employees.id" }
            ],
            "columnDefs": [
                { "visible": false, "targets": showHourly ? hideMe1 : hideMe2 }
              ],

PHP

$permHourlyWage - permission to see the hourly wage
                Field::inst('employees.hourly_wage')
                ->getFormatter( function ( $val) {
                    global $permHourlyWage;
                    // only if have permission
                    if ($permHourlyWage) {
                        return $val;
                    } else {
                        return $permHourlyWage;
                    }
                } )
                ->setFormatter( function ( $val ) {
                    if ( $val === '' ) {    // ->setFormatter( 'Format::ifEmpty', null)
                        return null;        // prevent editor from sending 0 for blank field
                    }
                    return ($val == 0 ? null : $val);
                } )
                ->validator( 'Validate::numeric' ),

also, I don't want the value for hourly wage to be written to at all if the user cannot see it (group B). I was thinking of

    ->on( 'preEdit', function ( $editor, $id, &$values ) {
        global $permHourlyWage;
        if(!$permHourlyWage){
            $editor->field( 'employees.hourly_wage' )->set( false );
        }
    } )

Replies

  • allanallan Posts: 61,972Questions: 1Answers: 10,160 Site admin

    Is there a Javascript error shown in your browser's console? Also, what is the response from the server?

    Thanks,
    Allan

  • Erik SkovErik Skov Posts: 33Questions: 6Answers: 2

    no console entry

    action=edit&data%5Brow_836%5D%5Bemployees%5D%5Buser_id%5D=116299&data%5Brow_836%5D%5Bemployees%5D%5Bemployee_title_id%5D=31&data%5Brow_836%5D%5Bemployees%5D%5Bpayroll_employee_id%5D=2927&data%5Brow_836%5D%5Bemployees%5D%5Bpayroll_code_id%5D=87&data%5Brow_836%5D%5Bemployees%5D%5Btype%5D=fulltime&data%5Brow_836%5D%5Bemployees%5D%5Bworkshift_id%5D=31&data%5Brow_836%5D%5Bemployees%5D%5Bhourly_wage%5D=false

    I found this:
    https://datatables.net/forums/discussion/comment/213777

    Which led me to add this in my Editor::inst() in my .php file.

            ->on( 'preEdit', function ( $editor, $id, &$values ) {
                global $permHourlyWage;
                if(!$permHourlyWage){
                    $editor->field('employees.hourly_wage')
                            ->set(false);
                }
            } )
    

    Which seems to have solved my problem. I will install the change, ask the users to test and let you know.

  • allanallan Posts: 61,972Questions: 1Answers: 10,160 Site admin

    Great to hear you've got it working.

    And yes, if you need to refer to an externally scoped variable in a PHP anonymous function, then you need to explicitly make it available - it isn't like Javascript when you can access higher scopes automatically. The use statement is how that is normally done - see the PHP manual for details.

    In this case it might be:

    ->on( 'preEdit', function ( $editor, $id, &$values ) use ($permHourlyWage) {
        if(!$permHourlyWage){
            $editor->field('employees.hourly_wage')
                    ->set(false);
        }
    } )
    

    It feels really weird if you are used to scoping in Javascript, but that's just how PHP is...

    Allan

  • Erik SkovErik Skov Posts: 33Questions: 6Answers: 2

    My user with less access has confirmed this is now working and they can edit. Thank you again for this wonderful forum where we can ask questions and find other questions already asked!

Sign In or Register to comment.