Skip to content
Advertisement

Datatables Editor show/hide a checkbox of a list of checkbox in edit row when another column is true/false

I’m new in Datatables and Editor.

I need help to hide or show some elements of a list of checkbox depends if the boolean “deleted” is true or false. the checkbox are displayed when you edit the row, but i need to display the name of the persons that have his deleted column set to false.

I’ve two tables, plus another one with the reference of the first with the second. The first table is “person”, that have FirstName, LastName, age and ID autoincrement and the deleted flag. The second table is “agency”, and contain simply the name, ID autoincrement and the deleted flag.

This is my php code:

<?php

include('configDB.php');
include('lib/DataTables.php');

date_default_timezone_set('UTC');

use
    DataTablesEditor,
    DataTablesEditorField,
    DataTablesEditorFormat,
    DataTablesEditorMjoin,
    DataTablesEditorOptions,
    DataTablesEditorUpload,
    DataTablesEditorValidate,
    DataTablesEditorValidateOptions;

Editor::inst($db, 'agency', 'ID')
    ->fields(
            Field::inst( 'agency.Name' )->setFormatter(Format::ifEmpty(null)),
            Field::inst( 'agency.deleted' )->setFormatter(Format::ifEmpty(false))
    )
    ->join(
        Mjoin::inst( 'person' )
            ->link( 'agency.ID', 'job.IDagency' )
            ->link( 'person.ID', 'job.IDperson' )
            ->order( 'FirstName asc' )
            ->fields(
                Field::inst( 'deleted' )->setFormatter(Format::ifEmpty(false)),
                Field::inst( 'FirstName' )->setFormatter(Format::ifEmpty(null)),
                Field::inst( 'ID' )
                    ->validator( 'Validate::required' )
                    ->options( Options::inst()
                        ->table( 'person' )
                        ->value( 'ID' )
                        ->label( 'FirstName' )
                    )
            )
            ->where('person.deleted', 0, '=')
    )
    ->process( $_POST )
    ->json();
?>

And here my index.js file:

editor_agency = new $.fn.dataTable.Editor({
        ajax:'agency_table_backend.php',
        table:'#agency_table',
        fields: [
            {
                name: 'agency.Name',
                label: 'Agency name:',
                type: 'textarea'
            },{
                name: 'person[].deleted',
            },{
                name: 'person[].ID',
                label: "Person name:",
                type: "checkbox"
            },{
                name: 'agency.deleted'
            }
        ]
    });

let table = $('#agency_table').DataTable({
        dom: "Bfrtip",
        select: true,
        serverSide: true,
        processing: true,
        responsive: true,
        ajax: {
            type: 'POST',
            url: 'agency_table_backend.php'
        },
        columns: [
            {
                data: 'agency.Name',
                orderable: true,
                searchable: true
            },{
                data: 'person',
                render: '[, ].deleted',
                orderable: false,
                searchable: false
            },{
                data: 'person',
                render: '[, ].FirstName',
                orderable: false,
                searchable: false
            },{
                data: 'agency.deleted',
                orderable: false,
                searchable: false
            }
        ], // END columns
        buttons:[
            {
                extend: 'create',
                editor: editor_agency,
                text: 'Add agency',
                formTitle: '<h3>Add agency</h3>'
            },{
                extend: 'edit',
                editor: editor_agency,
                text: 'Edit agency',
                formTitle: '<h3>Edit agency</h3>'
            },{
                extend: 'remove',
                editor: editor_agency,
                text: 'Delete agency',
                formTitle: '<h3>Delete agency</h3>'
            }
            ,{
                extend: "selectedSingle",
                editor: editor_agency,
                text: "Delete FLAG agency",
                action: function ( e, dt, node, config ) {
                    var confirmDeleteA = confirm("Are you sure to remove selected agency ?");
                    if(confirmDeleteA){
                        editor_agency
                            .edit( table.row( { selected: true } ).index(), false )
                            .set( 'agency.deleted', 'true' )
                            .submit();
                    }
                }
            },{
                extend: "selectAll",
                editor: editor_agency,
                text: "RESTORE all agency",
                action: function ( e, dt, node, config ) {
                    var confirmRestoreA = confirm("Are you sure to restore all agency ?");
                    if(confirmRestoreA){
                        editor_agency
                            .edit( table.row( { selected: true } ).index(), false )
                            .set( 'agency.deleted', '' )
                            .submit();
                    }
                }
            }
        ], // END buttons
        rowCallback: function( row, data, index ) {
            if (Number(data.agency.deleted).toFixed(0) == 1) {
                $(row).addClass('hidden');
            }else{
                $(row).removeClass('hidden');
            }
        }
    }); // END table

I know when hide the column of deleted column, but i haven’t do that to see if it works.

Thanks in advance, BledWolf

Advertisement

Answer

I’ve find the solution at the public documentation of DataTables and Editor: enter link description here at the point 4 of “SearchPaneOptions class”

I’ve change this:

Field::inst( 'ID' )
                    ->validator( 'Validate::required' )
                    ->options( Options::inst()
                        ->table( 'person' )
                        ->value( 'ID' )
                        ->label( 'FirstName' )
                    )

With this:

Field::inst( 'ID' )
                    ->validator( 'Validate::required' )
                    ->options( Options::inst()
                        ->table( 'person' )
                        ->value( 'ID' )
                        ->label( 'FirstName' )
                        ->where(function($q){
                            $q->where('deleted', 0);
                        })
                    )
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement