Not able to access web path in "Display" function

Not able to access web path in "Display" function

camerondeckercamerondecker Posts: 7Questions: 2Answers: 1

I keep getting the error that "row" is not defined in the "Display" function. Here is my JS:

var authorEdit = new $.fn.dataTable.Editor( {
        ajax: { type: 'POST', url:  'php/author-rest.php'},
        table: "#authors",
        idSrc: "Author.Author_ID",
        fields: [ 
                  { label: "Author Name:",name: "Author.Author_Name"}, 
                  { label: "Primary Language:",name: "Author.Primary_Lang" }, 
                  { label: "Birth Date:",name: "Author.Birth_Date",type:"date",dateFormat: $.datepicker.ISO_8601},
                  { label: "Author Photo:",name: "Author.Photo_FK",type:"upload",clearText: "Remove", noImageText: 'No image',
                    display: function (val, row) { return '<img src="'+row.Files.Web_Path+'"/>' }
                  } 
                ]
    }).on( 'open', function ( e, type ) {
       
} );

$("#authors").DataTable({ 
     dom: "Bfrtip",
     ajax: { "url": "php/author-rest.php", "type": "POST", data: { className: "Author"}},
     bLengthChange: false,
     columns: [
         { data: "Author.Author_ID", "width":"5%",className: "alignCenter" },
         { data: "Author.Author_Name" },
         { data: "Author.Primary_Lang" },
         { data: "Author.Birth_Date" },
         { data: "Author.Mod_Date","render": function(data, type, row) { return '<span rel="tooltip" title="'+row.Author.Mod_By+'" >'+data+'</span>';} },
         { data: "Author.Mod_By",visible: false}
     ],
     select: true,
     buttons: [
        { extend: "create", editor: authorEdit },
        { extend: "edit",   editor: authorEdit },
        { extend: "remove", editor: authorEdit }
    ]

 }); 

And here is my PHP:

<?php
define('__ROOT__', dirname(dirname(__FILE__))); 
require_once(__ROOT__."/users/models/config.php");
include( "../editor/php/DataTables.php" );
// Alias Editor classes so they are easy to use
use
    DataTables\Editor,
    DataTables\Editor\Field,
    DataTables\Editor\Format,
    DataTables\Editor\Join,
        DataTables\Editor\Upload,
    DataTables\Editor\Validate;

// Build our Editor instance and process the data coming from _POST
$editor = Editor::inst( $db, 'Author','Author_ID')
    ->fields(
        Field::inst( 'Author.Author_ID' )->validator( 'Validate::notEmpty' ),
        Field::inst( 'Author.Author_Name' )->validator( 'Validate::notEmpty' ),
        Field::inst( 'Author.Primary_Lang' ),
        Field::inst( 'Author.Birth_Date' ),
                Field::inst( 'Author.Created_By' )->set( Field::SET_CREATE ),
                Field::inst( 'Author.Mod_By' )->set( Field::SET_EDIT ),
                Field::inst( 'Author.Mod_Date' )->set( Field::SET_EDIT ),
                Field::inst( 'Author.Photo_FK' )->setFormatter( 'Format::nullEmpty' )
                    ->upload( Upload::inst( $_SERVER['DOCUMENT_ROOT'].'/appendix/uploads/authors/__NAME__' )
                    ->db( 'Files', 'File_ID', array(
                        'Files.File_Name'    => Upload::DB_FILE_NAME,
                        'Files.File_Size'    => Upload::DB_FILE_SIZE,
                        'Files.Web_Path'     => Upload::DB_WEB_PATH,
                        'Files.System_Path'  => Upload::DB_SYSTEM_PATH
                    ) )
                    ->validator( function ( $file ) {
                        return$file['size'] >= 5000000 ?
                            "Files must be smaller than 5MB" :
                            null;
                    } )
                    ->allowedExtensions( [ 'png', 'jpg', 'gif' ], "Please upload an image" )
                    )
    )->on( 'preCreate', function ( $editor, $values ) {
        $editor
            ->field( 'Author.Created_By' )
            ->setValue($_SESSION["userCakeUser"]->username);
        } )->on( 'preEdit', function ( $editor, $values ) {
        $editor
            ->field( 'Author.Mod_By' )
            ->setValue($_SESSION["userCakeUser"]->username);
        $editor
            ->field( 'Author.Mod_Date' )
            ->setValue(date("Y-m-d H:i:s"));
            
    } );

$editor ->leftJoin( 'Files', 'Files.File_ID', '=', 'Author.Photo_FK' )
    ->process($_POST)
    ->json();

I've also run the debugger, but haven't found anything there either: http://debug.datatables.net/ogamek

I'm not sure what I'm missing or doing incorrectly. Any help would be great!

Cameron

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    Hi Cameron,

    Have you upgraded from 1.4.x to 1.5? It looks like you are using 1.4 options for the upload there - they have changed a little in 1.5 to try and make it a little bit easier over all, but it does mean a little change. For example there is no second parameter passed into the display method now and you need to use the file() method to get upload file information now.

    The upgrade documentation should cover this, but if you have any questions, please let me know.

    Allan

  • camerondeckercamerondecker Posts: 7Questions: 2Answers: 1

    Hi Allan,

    I've changed the Editor JS to match the correct number of params on the Display function:

    var authorEdit = new $.fn.dataTable.Editor( {
            ajax: { type: 'POST', url:  'php/author-rest.php'},
            table: "#authors",
            idSrc: "Author.Author_ID",
            fields: [ 
                      { label: "Author Name:",name: "Author.Author_Name"}, 
                      { label: "Primary Language:",name: "Author.Primary_Lang" }, 
                      { label: "Birth Date:",name: "Author.Birth_Date",type:"date",dateFormat: $.datepicker.ISO_8601},
                      { label: "Author Photo:",name: "Author.Photo_FK",type:"upload",clearText: "Remove", noImageText: 'No image',
                        display: function ( imageId ) { return '<img src="'+table.file( 'Files', imageId ).Web_Path+'"/>'; } 
                      } 
                    ]
        }).on( 'open', function ( e, type ) {
           
    } );
    

    But now it's saying that "table" is not defined, and I can't find any documentation of that variable's use or where it comes from. Is that something I need to change?

    Thanks!
    Cameron

  • camerondeckercamerondecker Posts: 7Questions: 2Answers: 1
    Answer ✓

    I just figured it out! I had to assign the datatable to a variable and call file() on that variable! Thanks for your help!

This discussion has been closed.