Adding a pdf attachment to a datatable

Adding a pdf attachment to a datatable

brmtgirlbrmtgirl Posts: 1Questions: 1Answers: 0

Hi - I am looking to be able to add an attachment from my form into the datatable on that form. I see an example of adding an image to the row directly, but that's not helping me. Are there any examples anywhere that I could look at?

Answers

  • rf1234rf1234 Posts: 2,809Questions: 85Answers: 406
    edited August 2017

    In this example from my own coding you can upload multiple documents through Editor subsequently they all get displayed in the datatable as well and you can click on them to view them.

    Javascript:

    columns: [
    ....
    {data: null,
        render: function (data, type, row) {
            return renderFilesDataTable(row.file);
        }
    }
    ], ....
    
    //function being called for rendering:
    function renderFilesDataTable(rowFile) {
        var ix=0;
        var returnString = '';
        var fileName;
        var fileExtension;
        var n;
        var iconField;
        while (rowFile[ix]) {
            if (ix !== 0) {
                returnString = returnString.concat('<br>');
            }
            n = rowFile[ix].name.lastIndexOf("."); 
            fileExtension = rowFile[ix].name.substring(n+1);
            iconField = getIconFieldforFiles(fileExtension, '');
            fileName = rowFile[ix].name.substring(0, n);
            if (fileName.length > 11) {
                fileName = fileName.substring(0, 11) + '...';
            }
            returnString = returnString.concat
                (iconField + ' ' + fileName.link(rowFile[ix].web_path)); 
            ix++;       
        }
        return returnString;
    }
    
    function getIconFieldforFiles(fileExtension, size) {
        fileExtension = fileExtension.toLowerCase();
        var iconField = '';
        if ( fileExtension == 'pdf' ) {
            iconField = "<i class='fa fa-file-pdf-o " + size + "' aria-hidden='true'></i>";                   
        } else {
            if ( fileExtension == 'xls'     || 
                 fileExtension == 'xlsx'    ||
                 fileExtension == 'ods'     ||
                 fileExtension == 'csv'      ) {
                iconField = "<i class='fa fa-file-excel-o " + size + "' aria-hidden='true'></i>";
            } else {
                if ( fileExtension == 'doc'     ||
                     fileExtension == 'docx'    ||
                     fileExtension == 'rtf'     ||
                     fileExtension == 'odt'     ) {
                    iconField = "<i class='fa fa-file-word-o " + size + "' aria-hidden='true'></i>";
                } else {
                    if ( fileExtension == 'ppt'     ||
                         fileExtension == 'pptx'    ||
                         fileExtension == 'odp'    ) {
                    iconField = "<i class='fa fa-file-powerpoint-o " + size + "' aria-hidden='true'></i>";    
                    }
                }
            }
        }    
        return iconField;
    }
    

    PHP:

    ....
    ->join(
        Mjoin::inst( 'file' )
            ->link( 'contract.id', 'contract_has_file.contract_id' )
            ->link( 'file.id', 'contract_has_file.file_id' )
            ->fields(
                Field::inst( 'id' )
                ->upload( Upload::inst( $_SERVER['DOCUMENT_ROOT'].'/lgfuploads/contracts/__ID__.__EXTN__' )
                        ->db( 'file', 'id', array(
                            'about'   => 'C',  //contract
                            'name'    => Upload::DB_FILE_NAME,
                            'size'    => Upload::DB_FILE_SIZE,
                            'web_path'    => Upload::DB_WEB_PATH,
                            'system_path' => Upload::DB_SYSTEM_PATH
                        ) )
                        ->validator( function ( $file ) {
                            if ($file['size'] >= 52428800) {
                                return "Files must be smaller than 50MB";
                            } else {
                                return true;
                            }
                        } )
                        ->allowedExtensions( array  //php is not case sensitive here
                          ( 'pdf', 'xls', 'xlsx', 'csv', 'doc', 'docx', 'rtf', 'ppt',  
                            'pptx', 'odt', 'ods', 'odp' ),                                
                            "Please upload Pdf, MS Office or Open Office documents." )
                ),
                Field::inst( 'web_path' )->set( false ),
                Field::inst( 'name' )->set( false )             
            )
        )
    ....
    
    

    just to complete the picture, this is the function I call to render files in Editor.

    Javascript

    function renderFilesEditor(fileNameExt) {
        //find the last period in the filename
        var n = fileNameExt.lastIndexOf(".");
        var fileExtension = fileNameExt.substring(n+1);
        var fileName = fileNameExt.substring(0, n);
        if (fileName.length > 35) {
            fileName = fileName.substring(0, 35) + '...';
        }
        var iconField = getIconFieldforFiles(fileExtension, 'fa-lg');    
        return iconField + '  ' + fileName;
    }
    
    
This discussion has been closed.