Create a leftjoin in a getformatter

Create a leftjoin in a getformatter

marianidiegomarianidiego Posts: 54Questions: 17Answers: 1
edited March 2022 in Editor

I take my cue from an official example....

Let's say I start from the following example: https://editor.datatables.net/examples/simple/join.html

$(document).ready(function() {
    editor = new $.fn.dataTable.Editor( {
        ajax: "../php/join.php",
        table: "#example",
        fields: [ {
                label: "First name:",
                name: "users.first_name"
            }, {
                label: "Last name:",
                name: "users.last_name"
            }, {
                label: "Phone #:",
                name: "users.phone"
            },** {
                label: "Site:",
                name: "users.site",
                type: "select",
                placeholder: "Select a location"
            }**
        ]
    } );

    $('#example').DataTable( {
        dom: "Bfrtip",
        ajax: {
            url: "../php/join.php",
            type: 'POST'
        },
        columns: [
            { data: "users.first_name" },
            { data: "users.last_name" },
            { data: "users.phone" },
           ** { data: "sites.name" }**
        ],
        select: true,
        buttons: [
            { extend: "create", editor: editor },
            { extend: "edit",   editor: editor },
            { extend: "remove", editor: editor }
        ]
    } );
} );




<?php

// DataTables PHP library
include( "../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;


/*
 * Example PHP implementation used for the join.html example
 */
Editor::inst( $db, 'users' )
    ->field(
        Field::inst( 'users.first_name' ),
        Field::inst( 'users.last_name' ),
        Field::inst( 'users.phone' ),
        Field::inst( 'users.site' )
            ->options( Options::inst()
                ->table( 'sites' )
                ->value( 'id' )
                ->label( 'name' )
            )
            ->validator( Validate::dbValues() ),
        Field::inst( 'sites.name' )
    )
    ->leftJoin( 'sites', 'sites.id', '=', 'users.site' )
    ->process($_POST)
    ->json();
?>

Ora, io voglio tradurre i sites.name da una funzione creata da PHP.... Potrei risolverlo in questo modo:

                Field::inst( 'users.site' )
                    ->options( Options::inst()
                        ->table( 'sites' )
                        ->value( 'id' )
                        ->label( 'name' )
                        ->render( function ( $row ) {
                 return $myfunctiontraslating($row['name']);
                })
                    )

Only in this way, in my $.fn.dataTable.Editor I will have the translated names (in the select, but in the table the displayed data are those of "sites.name". , i.e. they will not be translated.

How can I use my server-side translation function?

I tried a:

 Field::inst( 'users.site' )
                    ->getFormatter( function ( $val, $data ) {
             return myfunctiontraslating($val);
                })
                    ->options( Options::inst()
                        ->table( 'sites' )
                        ->value( 'id' )
                        ->label( 'name' )
                        ->render( function ( $row ) {
                 return myfunctiontraslating($row['name']);
                })
                    )

But it clearly can't work. Can I in the getFormatter create a call $db and bring to the Client directly the translated word (users.site ->sites.id ->site.name -> myfunctiontranslating() )?

Or can I do it in the LEFTJOIN call?

In short, I can't process the data from the server side. I could do it from the client side, but it would be much more elegant if I could from the server lavo....

Thanks for the help

Answers

  • marianidiegomarianidiego Posts: 54Questions: 17Answers: 1

    I'm really Stupid!!!! The reply:

    Field::inst( 'sites.name' )
         ->getFormatter( function ( $val, $data ) {
             returnmyfunctiontraslating($val])                                   
           }),
    
  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    We've all been there! :)

    Colin

Sign In or Register to comment.