condition on selection

condition on selection

mimi123456789mimi123456789 Posts: 15Questions: 1Answers: 0
edited April 2020 in Free community support

Hello,

I have tables like this :

Here is my js code :


/* * Editor client script for DB table user_site * Created by http://editor.datatables.net/generator */ (function($){ $(document).ready(function() { var editor = new $.fn.dataTable.Editor( { ajax: 'user_site.php', table: '#user_site', fields: [ { "label": "Nom :", "name": "users.first_name" }, { "label": "Classe :", "name": "users.classe", "type": "select", }, { "label": "Compte :", "name": "sites[].id", "type": "checkbox" } ], i18n: { create: { button: "Nouveau", title: "Créer nouvelle entrée", submit: "Créer" }, edit: { button: "Modifier", title: "Modifier entrée", submit: "Actualiser" }, remove: { button: "Supprimer", title: "Supprimer", submit: "Supprimer", confirm: { _: "Etes-vous sûr de vouloir supprimer %d lignes?", 1: "Etes-vous sûr de vouloir supprimer 1 ligne?" } }, error: { system: "Une erreur s’est produite, contacter l’administrateur système" }, datetime: { previous: 'Précédent', next: 'Premier', months: [ 'Janvier', 'Février', 'Mars', 'Avril', 'Mai', 'Juin', 'Juillet', 'Août', 'Septembre', 'Octobre', 'Novembre', 'Décembre' ], weekdays: [ 'Dim', 'Lun', 'Mar', 'Mer', 'Jeu', 'Ven', 'Sam' ] } } } ); // Upload Editor - triggered from the import button. Used only for uploading a file to the browser var uploadEditor = new $.fn.dataTable.Editor( { fields: [ { label: 'CSV file:', name: 'csv', type: 'upload', ajax: function ( files ) { // Ajax override of the upload so we can handle the file locally. Here we use Papa // to parse the CSV. Papa.parse(files[0], { header: true, skipEmptyLines: true, complete: function (results) { if ( results.errors.length ) { console.log( results ); uploadEditor.field('csv').error( 'CSV parsing error: '+ results.errors[0].message ); } else { uploadEditor.close(); selectColumns( editor, results.data, results.meta.fields ); } } }); } } ] } ); var table = $('#user_site').DataTable( { dom: 'Bfrtip', ajax: 'user_site.php', columns: [ { "data": "users.first_name" }, { "data": "classe.name_classe" }, { "data": "sites", render: "[, ].name" } ], select: true, lengthChange: false, buttons: [ { extend: 'create', editor: editor }, { extend: 'edit' , editor: editor}, { extend: 'remove', editor: editor }, { extend: 'csv', text: 'Export CSV', className: 'btn-space', exportOptions: { orthogonal: null } }, { text: 'Import CSV', action: function () { uploadEditor.create( { title: 'CSV file import' } ); } }, { extend: 'selectAll', className: 'btn-space' }, 'selectNone', ], language: { processing: "Traitement en cours...", search: "Rechercher :", lengthMenu: "Afficher _MENU_ éléments", info: "Affichage de l'élement _START_ à _END_ sur _TOTAL_ éléments", infoEmpty: "Affichage de l'élement 0 à 0 sur 0 éléments", infoFiltered: "(filtré de _MAX_ éléments au total)", infoPostFix: "", loadingRecords: "Chargement en cours...", zeroRecords: "Aucun élément à afficher", emptyTable: "Aucune donnée disponible dans le tableau", paginate: { first: "Premier", previous: "Précédent", next: "Suivant", last: "Dernier" }, aria: { sortAscending: ": activer pour trier la colonne par ordre croissant", sortDescending: ": activer pour trier la colonne par ordre décroissant" } } } ); } ); }(jQuery));

Here is my php code :

<?php

// DataTables PHP library
include( "php/lib/DataTables.php" );

// Alias Editor classes so they are easy to use
use
    DataTables\Editor,
    DataTables\Editor\Field,
    DataTables\Editor\Format,
    DataTables\Editor\Mjoin,
    DataTables\Editor\Options,
    DataTables\Editor\Upload,
    DataTables\Editor\Validate,
    DataTables\Editor\ValidateOptions;

// Build our Editor instance and process the data coming from _POST

Editor::inst( $db, 'users', 'id' )
    ->fields(
        Field::inst( 'users.first_name' ),
        Field::inst( 'users.classe' )
            ->options( Options::inst()
                ->table( 'classe' )
                ->value( 'id' )
                ->label( 'name_classe')
            ),

        Field::inst( 'classe.name_classe' ),

    )
    ->leftJoin( 'classe', 'classe.id', '=', 'users.classe' )
    ->join(
        Mjoin::inst( 'sites' )
            ->link( 'users.id', 'user_site.user_id' )
            ->link( 'sites.id', 'user_site.site_id' )
            ->order( 'name asc' )
            ->fields(
                Field::inst( 'id' )
                    ->options( Options::inst()
                        ->table( 'sites' )
                        ->value( 'id' )
                        ->label( 'name')
                    ),
                Field::inst( 'name' ),

            )
    )
    ->process( $_POST )
    ->json();

I would like to have only checkboxes where sites.classe <= users.classe

Where have I to put my condition "where" ?

Thank you for your response !! :smiley:

Replies

  • rf1234rf1234 Posts: 2,808Questions: 85Answers: 406

    I wondered why you ask instead of just experimenting. But I think I understand the issue: What you are trying to achieve can't be achieved with a WHERE clause. You seem to want to create a dependency between two fields in your Editor form: users.classe and sites[].id.

    At the time the options for "sites" are retrieved it is not clear what value will be saved for "users.classe".

    Since "users.classe" is on top of "sites" in the Editor form it may make sense to dynamically change the available options for "sites" based on the content of "users.classe" using "dependent".

    So you shouldn't use a WHERE clause in the "sites" options instance but filter this dynamically with Javascript.

    Here is the link:
    https://editor.datatables.net/reference/api/dependent()

    and here is an example from my own coding. Depending on the value of one field I set fields and show or hide them. You would need to do the same for your sites[].id array: Loop through the array and show / hide array entries depending on the value of users.classe

    editor
    .dependent('ctr.VAT_charged', function ( val, data, callback ) {
        if (val == '1') {
            editor.show( ['ctr.VAT_rate_reduced', 'ctr.VAT_deductible'] );
        } else {
            editor.set( { 'ctr.VAT_rate_reduced': 0, 'ctr.VAT_deductible': 0 } )
                  .hide( ['ctr.VAT_rate_reduced', 'ctr.VAT_deductible'] );
        }
    })
    
  • mimi123456789mimi123456789 Posts: 15Questions: 1Answers: 0
    edited April 2020

    Thanks for your answer.

    I'd like to understand your code :
    in your table ctr, if VAT_charged is true, so you show fields "rate_reduced" and "VAT_deductible", otherwise you don't.

    I know how to show sites[].id if a checkbox is checked or not, I just would like to show some of them, not all according to the classe of the user.

    I thought with cascading lists, but I didn't find any complete and working example( one was here https://datatables.net/forums/discussion/60568~~~~

  • rf1234rf1234 Posts: 2,808Questions: 85Answers: 406

    Sorry, I can only repeat what I said above. It is just an example, not more.

    Since you have an array which I don't in my example you would need to loop through the array and decide for each value of the array whether or not you would like to show it. Which means showing “some of them, not all according to the classe of the user“ ...

This discussion has been closed.