Editor - Join - Link Table - Unable to automatically determine field from source.

Editor - Join - Link Table - Unable to automatically determine field from source.

fmshighcompanyfmshighcompany Posts: 11Questions: 4Answers: 0
edited July 2019 in Editor

Please help.
I'm trying to use JOIN with linkTable as shown: https://editor.datatables.net/examples/advanced/joinLinkTable.html

My code is at:
http://www.hy.com.br/assets/datatables-editor/sale-example.html

**When I use INLINE it raises a javascript error:
uncaught exception: Unable to automatically determine field from source. Please specify the field name. For more information, please refer to https://datatables.net/tn/11**

But, if I use the EDIT button it works ok.

JAVASCRIPT

(function($){
    $(document).ready(function(){
        var autocomplete_customer_id = function (request, response) {
                $.getJSON('/hy++/livedatatables/customerid/s:'+request.term, {},response);
            }
    
        var saleEditor = new $.fn.dataTable.Editor( {
            ajax: '/hy++/sale_example_datatables_editor/',
            table: '#sale',
            fields: 
                    [
                        { label: 'Customer:', name: 'sale.customer_id'  , type: 'select'                                                                  },
                        { label: 'Due Date:', name: 'sale.due_date'     , type: 'datetime'    ,format: 'DD/MM/YYYY',def: function(){ return new Date(); } },
                        { label: 'Total:'   , name: 'sale.total'                                                                                          },
                    ],
            });
                
        var saleTable = $('#sale').DataTable({
            ajax: {
                 url: '/hy++/sale_example_datatables_editor/'
                ,type: 'post'
                ,data: function( data ) { 
                            // test
                        }
            }
            ,dom : 'Bfrtip'
            ,buttons: [
                         { extend: 'create', editor: saleEditor }
                        ,{ extend: 'edit',   editor: saleEditor }
                        ,{ extend: 'remove', editor: saleEditor }
                    ]
            ,select : true
            ,serverSide    : true

            ,columns: [ 
                        {
                        data: 'sale.row_id'
                        ,className: 'dt-type-int dt-searchable'
                        },

                        {
                        data: 'example.customer.name'
                        ,editField: 'example.customer.id'
                        ,className: 'dt-type-autoComplete dt-dbtype-char dt-editable dt-searchable'
                        },

                        {
                        data: 'sale.due_date'
                        ,className: 'dt-type-date dt-editable dt-searchable'
                        },

                        {
                        data: 'sale.total'
                        ,className: 'dt-type-float dt-dbtype-decimal dt-editable dt-searchable'
                        } 
                    ]
            });

        // INLINE EDITOR
        $('#sale').on( 'click', 'tbody td:not(:first-child)', function (e) {
            saleEditor.inline( this );
        });
    });
}(jQuery));

SERVERSCRIPT:

<?php
global $db;
include( "datatables/lib/DataTables.php" );

use
    DataTables\Editor,
    DataTables\Editor\Field,
    DataTables\Editor\Format,
    DataTables\Editor\Mjoin,
    DataTables\Editor\Options,
    DataTables\Editor\Upload,
    DataTables\Editor\Validate,
    DataTables\Editor\ValidateOptions;

$vg = function($val, $data, $field) {return str_replace ( '.' , ',' , $val );};
$vs = function($val, $data, $field) {return str_replace ( ',' , '.' , $val );};
$vv = function($val, $data, $opts ) {return $val < 0  ? 'Please input positive values.' :  true; };

Editor::inst( $db, 'example.sale', 'row_id' )
        ->fields(
             Field::inst( 'sale.row_id' )

            //this is the select
            ,Field::inst( 'sale.customer_id' )  
                ->options( Options::inst()
                            ->table( 'example.customer' )
                            ->value( 'id' )
                            ->label( 'name' )
                        )

            //this is needed to display it joined table.field_name
            ,Field::inst('example.customer.name')

            ,Field::inst( 'sale.due_date' )
                ->validator( Validate::dateFormat( 'd/m/Y' ) )
                ->getFormatter( Format::datetime( 'Y-m-d', 'd/m/Y' ) )
                ->setFormatter( Format::datetime( 'd/m/Y', 'Y-m-d' ) )

            ,Field::inst( 'sale.total' )
                ->getFormatter( $vg )
                ->setFormatter( $vs )
                ->validator( $vv)
            )
        ->leftJoin( 'example.customer', 'sale.customer_id', '=', 'example.customer.id' )
        ->process( $_POST )
        ->json();

return;
?>

Answers

  • kthorngrenkthorngren Posts: 20,142Questions: 26Answers: 4,736

    Did you follow the steps provided in the link from the Unable to automatically determine field from source alert?
    https://datatables.net/manual/tech-notes/11

    That is the place to start.

    Kevin

  • fmshighcompanyfmshighcompany Posts: 11Questions: 4Answers: 0

    Yes, I read a lot of threads and posts.
    If you look to my JS you'll see the "editField" config, but it does not work.

     {
                            data: 'example.customer.name'
                            ,editField: 'example.customer.id'
                            ,className: 'dt-type-autoComplete dt-dbtype-char dt-editable dt-searchable'
                            },
    
  • kthorngrenkthorngren Posts: 20,142Questions: 26Answers: 4,736

    Looks like your editor field is sale.customer_id:

    { label: 'Customer:', name: 'sale.customer_id'
    

    But the field name doesn't match in the Datatables config:

    ,editField: 'example.customer.id'
    

    Kevin

  • fmshighcompanyfmshighcompany Posts: 11Questions: 4Answers: 0

    Wow!!!! Thhhaaannnk you kthorngren! It worked using "sale.customer_id".
    Thank you very much.

     {
             data: 'example.customer.name'
            ,editField: 'sale.customer_id'
            ,className: 'dt-type-autoComplete dt-dbtype-char dt-editable dt-searchable'
     },
    

    My goal is to use "autoComplete" on this field, so I started with the basic example and now that it worked I'll try to use autoComplete plugin.

    :-)

  • kthorngrenkthorngren Posts: 20,142Questions: 26Answers: 4,736

    Glad you got it working!

    Kevin

This discussion has been closed.