Use current value in database as option for where statement

Use current value in database as option for where statement

Koen VerhoevenKoen Verhoeven Posts: 25Questions: 7Answers: 0

I've got a select list in my Editor which is based on active users. But I also would like to be able to edit historical records from users which are not active anymore. At the moment, when I try to edit to edit historical records, the select list does not contain the non-active users, so I'm not able to edit the records without getting an error. How can create a select list which contains all active users and the user based on the current database-value?

My current not-working code where I think 'TheCurrentUserIdFromTheDatabase' should be replaced with the current value. How to do this?

Field::inst( 'deenk_tijdschrijven_tijdschrijven.userid' )
            ->validator( 'Validate::dbValues' )
            ->validator( 'Validate::notEmpty' )
            ->options( Options::inst()
            ->table( 'deenk_tijdschrijven_users' )
            ->value( 'deenk_tijdschrijven_users.userid' )
            ->label( 'deenk_tijdschrijven_users.naam' )
             ->where( function ($q) {
                $q->where( 'actief', 1, '=' );
                $q->or_where( 'userid', 'TheCurrentUserIdFromTheDatabase', '=' );
            } )
            ),

Thanks a lot for your help!

This question has an accepted answers - jump to answer

Answers

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

    You are not describing what the "current value" really is and where you get it from. But you are right: you would need to replace "TheCurrentUserIdFromTheDatabase" with that value.

    To help you I would need to understand your data model:
    - relevant tables
    - relevant relationships between those tables
    - foreign keys etc

    And I would also need to see more of your Editor instance.
    - What is the parent table
    - what is / are the linked tables / child tables etc.

  • allanallan Posts: 61,439Questions: 1Answers: 10,053 Site admin

    When you say TheCurrentUserIdFromTheDatabase is that the currently logged in user? If so, do you have their user id in a session variable? If that is the case you could just replace that string with $_SESSION['userId'].

    Allan

  • Koen VerhoevenKoen Verhoeven Posts: 25Questions: 7Answers: 0

    @Alan , it is not the current logged in user, but the value currently stored in the database. @rft1234, see my complete php script below:

    session_start();
    //include("login/config.php");
    include("../oauth/session.php");
    
    //$userDetails=$userClass->userDetails($session_uid);
    /*
     * Editor server script for DB table deenk_tijdschrijven_tijdschrijven
     * Created by http://editor.datatables.net/generator
     */
    
    // DataTables PHP library and database connection
    include( "lib/DataTables.php" );
    
    // Alias Editor classes so they are easy to use
    use
        DataTables\Editor,
        DataTables\Editor\Options,
        DataTables\Editor\Field,
        DataTables\Editor\Format,
        DataTables\Editor\Mjoin,
        DataTables\Editor\Upload,
        DataTables\Editor\Validate,
        DataTables\Editor\ValidateOptions;
    
    // Build our Editor instance and process the data coming from _POST
    Editor::inst( $db, 'deenk_tijdschrijven_tijdschrijven', 'tijdschrijven_id' )
        ->fields(
            Field::inst( 'deenk_tijdschrijven_tijdschrijven.project_id' )
                ->set( Field::SET_CREATE ),
            Field::inst( 'deenk_tijdschrijven_projecten.project' ),
            Field::inst( 'deenk_tijdschrijven_tijdschrijven.userid' )
                //->options( 'deenk_tijdschrijven_users', 'userid', 'naam')
                ->validator( 'Validate::dbValues' )
                ->validator( 'Validate::notEmpty' )
                
                ->options( Options::inst()
                ->table( 'deenk_tijdschrijven_users' )
                ->value( 'deenk_tijdschrijven_users.userid' )
                ->label( 'deenk_tijdschrijven_users.naam' )
                 ->where( function ($q) {
                    $q->where( 'actief', 1, '=' );
                    //$q->or_where( 'userid', 'TheCurrentUserIdFromTheDatabase', '=' );
                } )
                ),
            Field::inst( 'deenk_tijdschrijven_users.naam' ),
            Field::inst( 'deenk_tijdschrijven_tijdschrijven.datum' )
                ->validator( 'Validate::notEmpty' )
                ->validator( 'Validate::dateFormat', array( 'format'=>'d-m-Y' ) )
                ->getFormatter( 'Format::date_sql_to_format', 'd-m-Y' )
                ->setFormatter( 'Format::date_format_to_sql', 'd-m-Y' ),
            Field::inst( 'deenk_tijdschrijven_tijdschrijven.datum as datum_sort' )
                ->getFormatter( 'Format::date_sql_to_format', 'Y-m-d' ),
            Field::inst( 'deenk_tijdschrijven_tijdschrijven.onderdeel_id' )
                ->validator( 'Validate::notEmpty' )
                ->options( 'deenk_tijdschrijven_onderdelen', 'onderdeel_id', 'onderdeel')
                ->validator( 'Validate::dbValues' ),
            Field::inst( 'deenk_tijdschrijven_onderdelen.onderdeel' ),
            Field::inst( 'deenk_tijdschrijven_tijdschrijven.activiteit_id' )
                ->validator( 'Validate::notEmpty' ),
            Field::inst( 'deenk_tijdschrijven_activiteiten.activiteit' ),
            Field::inst( 'deenk_tijdschrijven_onderdelen.eenheid' ),
            Field::inst( 'deenk_tijdschrijven_tijdschrijven.toelichting' ),
            Field::inst( 'deenk_tijdschrijven_tijdschrijven.tijdsduur' )
                ->validator( 'Validate::notEmpty' ),/*
                ->validator( 'Validate::minMaxNum', array( 'min'=>5, 'max'=>720 ) ),*/
            Field::inst( 'deenk_tijdschrijven_tijdschrijven.hoeveelheid' )
                ->validator( 'Validate::numeric' )
        )
        ->leftJoin( 'deenk_tijdschrijven_users', 'deenk_tijdschrijven_users.userid', '=', 'deenk_tijdschrijven_tijdschrijven.userid' )
        ->leftJoin( 'deenk_tijdschrijven_projecten', 'deenk_tijdschrijven_projecten.project_id', '=', 'deenk_tijdschrijven_tijdschrijven.project_id' )
        ->leftJoin( 'deenk_tijdschrijven_onderdelen', 'deenk_tijdschrijven_onderdelen.onderdeel_id', '=', 'deenk_tijdschrijven_tijdschrijven.onderdeel_id' )
        ->leftJoin( 'deenk_tijdschrijven_activiteiten', 'deenk_tijdschrijven_activiteiten.activiteit_id', '=', 'deenk_tijdschrijven_tijdschrijven.activiteit_id' )
        ->on( 'preCreate', function ( $editor, $values ) {
            $editor
                ->field( 'deenk_tijdschrijven_tijdschrijven.project_id' )
                ->setValue( $_SESSION['project_id'] );
        } )
        ->where('deenk_tijdschrijven_tijdschrijven.project_id', $_SESSION['project_id'], '=')
        ->where( 'deenk_tijdschrijven_tijdschrijven.datum', date('Y-m-d',strtotime('now - 3 month')), '>=' )
        ->process( $_POST )
        ->json();
    
  • allanallan Posts: 61,439Questions: 1Answers: 10,053 Site admin

    Oh - so it will change per row edited? That's a little more difficult since you'll need to get a new set of options for every row. That can be done using the initEdit event, and make an Ajax request to the server to get the list of options for the row being edited. Then use field().update() to set the options based on what is returned from the server.

    You'll also need a script at the server-side which will get the list of options for you. Unfortunately the Editor libraries don't provide that ability themselves, but it sounds like it should be a relatively simple SQL statement to get the data.

    Allan

  • Koen VerhoevenKoen Verhoeven Posts: 25Questions: 7Answers: 0

    So the table with where de data should be stored is : deenk_tijdschrijven_tijdschrijven. The column with the userid is called userid

    The referenced/parent table with the users is called deenk_tijdschrijven_users and also contains the column userid as primary key. It also contains a column for active users (deenk_tijdschrijven_users.actief = 1), and a column for the username itself (deenk_tijdschrijven_users.naam)

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

    Koen, are you actually doing parent / child editing? When you do the editing of the child table do you have a parent record selected at that time? Because then you could simply pass the user id of the parent table to the server very easily and use this variable at the back end as well. That would make things a lot easier.
    So if that is your use case just let me know.
    Roland

  • Koen VerhoevenKoen Verhoeven Posts: 25Questions: 7Answers: 0
    edited May 2020

    It is just a select list, so no parent/child editing in the way you mean I guess. If you can help me with the very easy solution, that would be great:)

  • Koen VerhoevenKoen Verhoeven Posts: 25Questions: 7Answers: 0

    Maybe this helps:

    var editor = new $.fn.dataTable.Editor( {
            ajax: 'php/table.deenk_tijdschrijven_tijdschrijven.php',
            table: '#deenk_tijdschrijven_tijdschrijven',
            fields: [
                {
                    "label": "Onderdeel:",
                    "name": "deenk_tijdschrijven_tijdschrijven.onderdeel_id",
                    "type": "select",
                    "def": 0,
                    placeholder: "Selecteer een onderdeel"
                },
                {
                    "label": "Activiteit:",
                    "name": "deenk_tijdschrijven_tijdschrijven.activiteit_id",
                    "type": "select",
                },
                {
                    "label": "Naam:",
                    "name": "deenk_tijdschrijven_tijdschrijven.userid",
                    "type": "selectize"
                    //,def: uidFromOauth
                },
                {
                    "label": "Datum:",
                    "name": "deenk_tijdschrijven_tijdschrijven.datum",
                    def: function () { return new Date(); },
                    "type": "datetime",
                    "format": "DD-MM-YYYY"
                    //"format": "YY-MM-DD"
                    
                },
                {
                    "label": "Tijdsduur:",
                    "name": "deenk_tijdschrijven_tijdschrijven.tijdsduur",
                    "def": "5",
                    "fieldInfo": 'Tijdsduur in minuten'
                },
                {
                    "label": "Hoeveelheid:",
                    "name": "deenk_tijdschrijven_tijdschrijven.hoeveelheid",
                    "def": "1",
                    "fieldInfo": 'Aantal van onderdeel'
                },
                {
                    "label": "Toelichting:",
                    "name": "deenk_tijdschrijven_tijdschrijven.toelichting",
                    "type": "textarea",
                }
            ]
        } );
    
  • rf1234rf1234 Posts: 2,801Questions: 85Answers: 406

    Well, let me word it differently: Do you have this user id "available" at the front end at the time you are editing the record? (I mean you should have it available: How else would you know who this user "from the database based on the current database value" really is?!)

    If you have that user available this would be the simplest solution, I guess.
    (In parent / child editing you have it available automatically because it is part of the selected parent record.)

    So assuming the parent user Id is available in the field "parentUserId" at the front end this would be the code:

    In "ajax.data" you are sending the parentUserId to the server as a POST variable. I guess you would also need this in your Data Table definition as well, not only in the Editor instance.

    var editor = new $.fn.dataTable.Editor( {
            ajax: {
                url: 'php/table.deenk_tijdschrijven_tijdschrijven.php',
                type: 'POST',
                data: function ( d ) {
                    d.parentUserId = parentUserId;
                }
            },
            table: '#deenk_tijdschrijven_tijdschrijven',
            fields: [
                {
                    "label": "Onderdeel:",
                    "name": "deenk_tijdschrijven_tijdschrijven.onderdeel_id",
                    "type": "select",
                    "def": 0,
                    placeholder: "Selecteer een onderdeel"
                },
                {
                    "label": "Activiteit:",
                    "name": "deenk_tijdschrijven_tijdschrijven.activiteit_id",
                    "type": "select",
                },
                {
                    "label": "Naam:",
                    "name": "deenk_tijdschrijven_tijdschrijven.userid",
                    "type": "selectize"
                    //,def: uidFromOauth
                },
                {
                    "label": "Datum:",
                    "name": "deenk_tijdschrijven_tijdschrijven.datum",
                    def: function () { return new Date(); },
                    "type": "datetime",
                    "format": "DD-MM-YYYY"
                    //"format": "YY-MM-DD"
                     
                },
                {
                    "label": "Tijdsduur:",
                    "name": "deenk_tijdschrijven_tijdschrijven.tijdsduur",
                    "def": "5",
                    "fieldInfo": 'Tijdsduur in minuten'
                },
                {
                    "label": "Hoeveelheid:",
                    "name": "deenk_tijdschrijven_tijdschrijven.hoeveelheid",
                    "def": "1",
                    "fieldInfo": 'Aantal van onderdeel'
                },
                {
                    "label": "Toelichting:",
                    "name": "deenk_tijdschrijven_tijdschrijven.toelichting",
                    "type": "textarea",
                }
            ]
        } );
    

    Then on the server side you use the $_POST variable in your WHERE clause:

    // Build our Editor instance and process the data coming from _POST
    if ( ! isset($_POST['parentUserId ']) || ! is_numeric($_POST['parentUserId ']) ) {
            echo json_encode( [ "data" => [] ] );
    } else {
       Editor::inst( $db, 'deenk_tijdschrijven_tijdschrijven', 'tijdschrijven_id' )
        ->fields(
            Field::inst( 'deenk_tijdschrijven_tijdschrijven.project_id' )
                ->set( Field::SET_CREATE ),
            Field::inst( 'deenk_tijdschrijven_projecten.project' ),
            Field::inst( 'deenk_tijdschrijven_tijdschrijven.userid' )
                //->options( 'deenk_tijdschrijven_users', 'userid', 'naam')
                ->validator( 'Validate::dbValues' )
                ->validator( 'Validate::notEmpty' )
                 
                ->options( Options::inst()
                ->table( 'deenk_tijdschrijven_users' )
                ->value( 'deenk_tijdschrijven_users.userid' )
                ->label( 'deenk_tijdschrijven_users.naam' )
                 ->where( function ($q) {
                    $q->where( 'actief', 1, '=' );
                    $q->or_where( 'userid', $_POST['parentUserId '] );
                } )
                ),
            Field::inst( 'deenk_tijdschrijven_users.naam' ),
            Field::inst( 'deenk_tijdschrijven_tijdschrijven.datum' )
                ->validator( 'Validate::notEmpty' )
                ->validator( 'Validate::dateFormat', array( 'format'=>'d-m-Y' ) )
                ->getFormatter( 'Format::date_sql_to_format', 'd-m-Y' )
                ->setFormatter( 'Format::date_format_to_sql', 'd-m-Y' ),
            Field::inst( 'deenk_tijdschrijven_tijdschrijven.datum as datum_sort' )
                ->getFormatter( 'Format::date_sql_to_format', 'Y-m-d' ),
            Field::inst( 'deenk_tijdschrijven_tijdschrijven.onderdeel_id' )
                ->validator( 'Validate::notEmpty' )
                ->options( 'deenk_tijdschrijven_onderdelen', 'onderdeel_id', 'onderdeel')
                ->validator( 'Validate::dbValues' ),
            Field::inst( 'deenk_tijdschrijven_onderdelen.onderdeel' ),
            Field::inst( 'deenk_tijdschrijven_tijdschrijven.activiteit_id' )
                ->validator( 'Validate::notEmpty' ),
            Field::inst( 'deenk_tijdschrijven_activiteiten.activiteit' ),
            Field::inst( 'deenk_tijdschrijven_onderdelen.eenheid' ),
            Field::inst( 'deenk_tijdschrijven_tijdschrijven.toelichting' ),
            Field::inst( 'deenk_tijdschrijven_tijdschrijven.tijdsduur' )
                ->validator( 'Validate::notEmpty' ),/*
                ->validator( 'Validate::minMaxNum', array( 'min'=>5, 'max'=>720 ) ),*/
            Field::inst( 'deenk_tijdschrijven_tijdschrijven.hoeveelheid' )
                ->validator( 'Validate::numeric' )
        )
        ->leftJoin( 'deenk_tijdschrijven_users', 'deenk_tijdschrijven_users.userid', '=', 'deenk_tijdschrijven_tijdschrijven.userid' )
        ->leftJoin( 'deenk_tijdschrijven_projecten', 'deenk_tijdschrijven_projecten.project_id', '=', 'deenk_tijdschrijven_tijdschrijven.project_id' )
        ->leftJoin( 'deenk_tijdschrijven_onderdelen', 'deenk_tijdschrijven_onderdelen.onderdeel_id', '=', 'deenk_tijdschrijven_tijdschrijven.onderdeel_id' )
        ->leftJoin( 'deenk_tijdschrijven_activiteiten', 'deenk_tijdschrijven_activiteiten.activiteit_id', '=', 'deenk_tijdschrijven_tijdschrijven.activiteit_id' )
        ->on( 'preCreate', function ( $editor, $values ) {
            $editor
                ->field( 'deenk_tijdschrijven_tijdschrijven.project_id' )
                ->setValue( $_SESSION['project_id'] );
        } )
        ->where('deenk_tijdschrijven_tijdschrijven.project_id', $_SESSION['project_id'], '=')
        ->where( 'deenk_tijdschrijven_tijdschrijven.datum', date('Y-m-d',strtotime('now - 3 month')), '>=' )
        ->process( $_POST )
        ->json();
    }
    
  • Koen VerhoevenKoen Verhoeven Posts: 25Questions: 7Answers: 0
    edited May 2020

    I think I'm missing something. I don't think I will need the same structure in my datatable definition since my table is retrieving the all required userdata with a join. My serverside code now is:

    <?php
    session_start();
    //include("login/config.php");
    include("../oauth/session.php");
    
    //$userDetails=$userClass->userDetails($session_uid);
    /*
     * Editor server script for DB table deenk_tijdschrijven_tijdschrijven
     * Created by http://editor.datatables.net/generator
     */
    
    // DataTables PHP library and database connection
    include( "lib/DataTables.php" );
    
    // Alias Editor classes so they are easy to use
    use
        DataTables\Editor,
        DataTables\Editor\Options,
        DataTables\Editor\Field,
        DataTables\Editor\Format,
        DataTables\Editor\Mjoin,
        DataTables\Editor\Upload,
        DataTables\Editor\Validate,
        DataTables\Editor\ValidateOptions;
    
    // Build our Editor instance and process the data coming from _POST
    if ( ! isset($_POST['userid']) ) {
        echo json_encode( [ "data" => [] ] );
    } else {
    Editor::inst( $db, 'deenk_tijdschrijven_tijdschrijven', 'tijdschrijven_id' )
        ->fields(
            Field::inst( 'deenk_tijdschrijven_tijdschrijven.project_id' )
                ->set( Field::SET_CREATE ),
            Field::inst( 'deenk_tijdschrijven_projecten.project' ),
            Field::inst( 'deenk_tijdschrijven_tijdschrijven.userid' )
                //->options( 'deenk_tijdschrijven_users', 'userid', 'naam')
                ->validator( 'Validate::dbValues' )
                ->validator( 'Validate::notEmpty' )
                
                ->options( Options::inst()
                ->table( 'deenk_tijdschrijven_users' )
                ->value( 'deenk_tijdschrijven_users.userid' )
                ->label( 'deenk_tijdschrijven_users.naam' )
                 ->where( function ($q) {
                    global $userid;
                    $q->where( 'actief', 1, '=' );
                    $q->or_where( 'userid', $_POST['userid'], '=' );
                } )
                ),
            Field::inst( 'deenk_tijdschrijven_users.naam' ),
            Field::inst( 'deenk_tijdschrijven_tijdschrijven.datum' )
                ->validator( 'Validate::notEmpty' )
                ->validator( 'Validate::dateFormat', array( 'format'=>'d-m-Y' ) )
                ->getFormatter( 'Format::date_sql_to_format', 'd-m-Y' )
                ->setFormatter( 'Format::date_format_to_sql', 'd-m-Y' ),
            Field::inst( 'deenk_tijdschrijven_tijdschrijven.datum as datum_sort' )
                ->getFormatter( 'Format::date_sql_to_format', 'Y-m-d' ),
            Field::inst( 'deenk_tijdschrijven_tijdschrijven.onderdeel_id' )
                ->validator( 'Validate::notEmpty' )
                ->options( 'deenk_tijdschrijven_onderdelen', 'onderdeel_id', 'onderdeel')
                ->validator( 'Validate::dbValues' ),
            Field::inst( 'deenk_tijdschrijven_onderdelen.onderdeel' ),
            Field::inst( 'deenk_tijdschrijven_tijdschrijven.activiteit_id' )
                ->validator( 'Validate::notEmpty' ),
            Field::inst( 'deenk_tijdschrijven_activiteiten.activiteit' ),
            Field::inst( 'deenk_tijdschrijven_onderdelen.eenheid' ),
            Field::inst( 'deenk_tijdschrijven_tijdschrijven.toelichting' ),
            Field::inst( 'deenk_tijdschrijven_tijdschrijven.tijdsduur' )
                ->validator( 'Validate::notEmpty' ),/*
                ->validator( 'Validate::minMaxNum', array( 'min'=>5, 'max'=>720 ) ),*/
            Field::inst( 'deenk_tijdschrijven_tijdschrijven.hoeveelheid' )
                ->validator( 'Validate::numeric' )
        )
        ->leftJoin( 'deenk_tijdschrijven_users', 'deenk_tijdschrijven_users.userid', '=', 'deenk_tijdschrijven_tijdschrijven.userid' )
        ->leftJoin( 'deenk_tijdschrijven_projecten', 'deenk_tijdschrijven_projecten.project_id', '=', 'deenk_tijdschrijven_tijdschrijven.project_id' )
        ->leftJoin( 'deenk_tijdschrijven_onderdelen', 'deenk_tijdschrijven_onderdelen.onderdeel_id', '=', 'deenk_tijdschrijven_tijdschrijven.onderdeel_id' )
        ->leftJoin( 'deenk_tijdschrijven_activiteiten', 'deenk_tijdschrijven_activiteiten.activiteit_id', '=', 'deenk_tijdschrijven_tijdschrijven.activiteit_id' )
        ->on( 'preCreate', function ( $editor, $values ) {
            $editor
                ->field( 'deenk_tijdschrijven_tijdschrijven.project_id' )
                ->setValue( $_SESSION['project_id'] );
        } )
        ->where('deenk_tijdschrijven_tijdschrijven.project_id', $_SESSION['project_id'], '=')
        ->where( 'deenk_tijdschrijven_tijdschrijven.datum', date('Y-m-d',strtotime('now - 3 month')), '>=' )
        ->process( $_POST )
        ->json();
    }
    

    My js code is:

    (function($){
    
    $(document).ready(function() {
        //var uidFromOauth = '81a1f29b-453a-4426-94c2-7e5266477074';
        //var uidFromOauth = "<?php echo $_SESSION['uidFromOauth']; ?>"; 
        //window.alert(<?php echo $uidFromOauth; ?>);
        var editor = new $.fn.dataTable.Editor( {
            ajax: {
                url: 'php/table.deenk_tijdschrijven_tijdschrijven.php',
                type: 'POST',
                data: function ( d ) {
                    d.userid = "deenk_tijdschrijven_tijdschrijven.userid";
                }
            },
            table: '#deenk_tijdschrijven_tijdschrijven',
            fields: [
                {
                    "label": "Onderdeel:",
                    "name": "deenk_tijdschrijven_tijdschrijven.onderdeel_id",
                    "type": "select",
                    "def": 0,
                    placeholder: "Selecteer een onderdeel"
                },
                {
                    "label": "Activiteit:",
                    "name": "deenk_tijdschrijven_tijdschrijven.activiteit_id",
                    "type": "select",
                },
                {
                    "label": "Naam:",
                    "name": "deenk_tijdschrijven_tijdschrijven.userid",
                    "type": "selectize"
                    //,def: uidFromOauth
                },
                {
                    "label": "Datum:",
                    "name": "deenk_tijdschrijven_tijdschrijven.datum",
                    def: function () { return new Date(); },
                    "type": "datetime",
                    "format": "DD-MM-YYYY"
                    //"format": "YY-MM-DD"
                    
                },
                {
                    "label": "Tijdsduur:",
                    "name": "deenk_tijdschrijven_tijdschrijven.tijdsduur",
                    "def": "5",
                    "fieldInfo": 'Tijdsduur in minuten'
                },
                {
                    "label": "Hoeveelheid:",
                    "name": "deenk_tijdschrijven_tijdschrijven.hoeveelheid",
                    "def": "1",
                    "fieldInfo": 'Aantal van onderdeel'
                },
                {
                    "label": "Toelichting:",
                    "name": "deenk_tijdschrijven_tijdschrijven.toelichting",
                    "type": "textarea",
                }
            ]
        } );
    

    The editor shows the select list with all the active users, but does not show the current database value.

  • rf1234rf1234 Posts: 2,801Questions: 85Answers: 406
    edited May 2020

    I think I'm missing something. I don't think I will need the same structure in my datatable definition since my table is retrieving the all required userdata with a join.

    That has no connection: I mean you would need to pass the user id as a post variable. That is independent of the join you have server side.

    but does not show the current database value.

    Again: What exactly do you mean by "current database value"? Where is it in your code?

    Are you really sure you are passing the parent user id in your code like this?

    data: function ( d ) {
       d.userid = "deenk_tijdschrijven_tijdschrijven.userid";
    }
    

    This makes no sense. You seem to be trying to pass back a variable as a post variable to the server that you are currently retrieving at the same time.

    Once again: Without understanding what you mean by "current database value" and where you have it in your client side code I cannot help you.

    My assumption was that it is the id of some parent table. Then you must have it available in your code client side. The fact that you are joining with the parent table in your server side code does not mean it provides you with that particular "current" user id.

    Can you please also read this blog to understand parent / child editing a little better. It might help you to get some clarity on what you are trying to achieve:
    https://datatables.net/blog/2016-03-25

  • Koen VerhoevenKoen Verhoeven Posts: 25Questions: 7Answers: 0

    No, I think it is not passing the value as I think it should work.
    Maybe a screenshot works better:

    As you can see in the table next to the editor, the first row shows the name the user 'Leon...'. But when clicking on Edit the username is not shown (at the location of the mouse pointer). This is because Leon is not an active user anymore. So what I mean with 'current database value' I mean that the value for Leon is present in the table, but I cannot retrieve it when editing because he is not present in the active user list.

  • rf1234rf1234 Posts: 2,801Questions: 85Answers: 406
    edited May 2020

    Since you don't really seem to have a parent / child relationship between two tables you would probably need to handle this server side

    ->where( function ($q) {
         // global $userid; why? this is not being used here!
          $q->where( 'actief', 1, '=' );
          $q->or_where( 'userid', $data['deenk_tijdschrijven_tijdschrijven.userid'] );
     } )
    

    not sure whether $data['deenk_tijdschrijven_tijdschrijven.userid'] is available inside the options instance at that time. You might want to give it a try.

  • Koen VerhoevenKoen Verhoeven Posts: 25Questions: 7Answers: 0

    I added the global var during some tests, but forgot to remove it.

    Unfortunately $data does not seem to be available at that moment. Error:

    Undefined variable: data in .... 
    

    Any other ideas. It should not be too difficult I guess :smile:

  • rf1234rf1234 Posts: 2,801Questions: 85Answers: 406
    edited May 2020

    You would need to manipulate the options returned from the server on "select" of the table row. Just append the options returned from the server by the selected user id using the api:
    https://editor.datatables.net/reference/api/field().update()

    Use append: true.

  • rf1234rf1234 Posts: 2,801Questions: 85Answers: 406
    edited May 2020 Answer ✓
    editor
       .on('initEdit', function (e, node, data, items, type) {
            var selected = table.row( {selected: true} ); 
            if (selected.any()) {
                var label = selected.data().deenk_tijdschrijven_users.naam;
                var value = selected.data().deenk_tijdschrijven_users.userid;
                var field = this.field('deenk_tijdschrijven_tijdschrijven.userid');
                field.update( [ {label: label, value: value} ], true );
            }
       } );
    

    Make sure you retrieve deenk_tijdschrijven_users.userid in your server side editor instance. Right now I only see you retrieving it INSIDE the server options instance which is NOT sufficient.

  • Koen VerhoevenKoen Verhoeven Posts: 25Questions: 7Answers: 0

    Thanks for all the hard work! Works almost perfect right now. It does work when I use the type 'select', but not with 'selectize'. I'll try to figure this out tomorrow.

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

    Search the forum for selectize please. The options change is probably too late for selectize. It may be impossible or difficult to change them after initialization. In that case you would need to append the options on “xhr“ which isn't possible in your case because at that time the select of the table row hasn't been made ... Could be difficult. You might be forced to stick with the select field in this case ...

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

    In that case you would need to append the options on “xhr“ which isn't possible in your case because at that time the select of the table row hasn't been made ...

    This is also the reason why the server side manipulations in the where clause of the options instance make no sense! All of this only works in parent / child editing, not in your case: The options are returned ONCE for ALL of the records of your data table. Hence you can only manipulate them AFTER selecting one record which obviously doesn't work with SELECTIZE. You would also need to make sure that when selecting a different record you change the options again because then the selected user is a different one. That might require that you save the original options in a global variable and every time you open Editor you append a copy of the original options with the respective user and replace (not append) the existing Editor options. A bit of work to do for you. To save the original options in a variable you'll probably have to intercept them on "xhr" like in this code example:

    var savedOptions; //global variable!!
    
    table
       .on('xhr', function( e, settings, json, xhr ) {
            if ( json != null ) {
                if ( typeof json.options !== 'undefined' ) {
                    savedOptions = json.options["deenk_tijdschrijven_tijdschrijven.userid"];
                }
            }          
        });
    

    Then later you replace the Editor options with those copied options and the current user:

    editor
       .on('initEdit', function (e, node, data, items, type) {
            var selected = table.row( {selected: true} );
            if (selected.any()) {
                var label = selected.data().deenk_tijdschrijven_users.naam;
                var value = selected.data().deenk_tijdschrijven_users.userid;
                var field = this.field('deenk_tijdschrijven_tijdschrijven.userid');
                var options = savedOptions; // from global variable
                options.push( {label: label, value: value} );
                field.update( options, false ); //replace, not append
            }
       } );
    
  • Koen VerhoevenKoen Verhoeven Posts: 25Questions: 7Answers: 0

    Thanks! As you already noticed I'm not really experienced with this. Writing the codes from your examples would take me hours:). I think I have to stick with the standard select for now, but I'm going to give it another try with the information from your last post. I'll let you know if it works. Many thanks again!

This discussion has been closed.