Upload with serverSide = true

Upload with serverSide = true

marianidiegomarianidiego Posts: 62Questions: 19Answers: 1
edited 10:18AM in DataTables 2

Page Code:

                            fileEditor = new $.fn.dataTable.Editor( {
                                ajax: {
                                                url: "dist/cont/tpl_lista_prodotti/prodotti_file.php",
                                                type: 'POST',
                                                data: function ( d ) {
                                                    d.csrf_token = "<?=$csrf1?>";
                                                    d.products_id = rowData.products.products_id;
                                                }
                                },
                                "table": "#file",
                                fields: [
                                    {
                                        label: "ID:",
                                        name: "products_id",
                                        type:  "readonly",
                                        def: rowData.products.products_id
                                    },
                                    {
                                        label: "Ordine Visione:",
                                        name: "order",
                                        type: "text",
                                        def: "50"
                                    },
                                    {
                                        label: "Descrizione:",
                                        name: "description",
                                        "type": "select",
                                        "def": "Info",
                                        "options": [
                                            "ST",
                                            "SS",
                                            "Info",
                                            "Manual"
                                        ]
                                    },
                                    {
                                        label: "Lingua:",
                                        name: "lang",
                                        type: "select"
                                    },
                                    {
                                        label: "Pubblicare?",
                                        name: "publish",
                                        type: "checkbox",
                                        separator: "|",
                                        options:   [
                                            { label: '', value: 1 }
                                        ],
                                        def: 1
                                    },
                                    {   label: 'File',
                                        name: 'file',
                                        type: "upload",
                                        display: function ( file_id ) {
    
    
                                            var filename = fileEditor.file( 'products_file_data', file_id ).file;
                                            var extention = filename.split('.').pop().toLowerCase();
    
                                            if("pdf" == extention)
                                                    return '<img src="dist/img/pdf.png"/> ' + filename;
                                            if("zip" == extention)
                                                    return '<img src="dist/img/zip.png"/> ' + filename;
    
                                            return '<img src="dist/img/doc.png"/> ' + filename;
    
                                        },
                                        clearText: "Clear",
                                        noImageText: 'No image'
                                    }
    
                                ]
                            });
    
    
                            fileTable = $('#file').DataTable({
                            "language": {   "url": "<?=$ADMIN['lng']->t('DATATABLES', 'i18n');?>" },
                            "dom":              'Bfrtlip',
                            stateSave:      false,
                            serverSide:     true,
                            select: {
                                            info: false
                            },
                            ajax: {
                                        url: "dist/cont/tpl_lista_prodotti/prodotti_file.php",
                                        type: 'POST',
                                        data: function ( d ) {
                                            d.csrf_token = "<?=$csrf1?>";
                                            d.products_id = rowData.products.products_id;
                                        }
                            },
                            columns: [
                                {
                                title: '', data: 'file',
                                className: "dt-body-center",
                                width: '5px',
                                /*render: function ( file_id ) {
                                    return file_id ? "<a href='"+
                                        window.location.origin + fileEditor.file( 'products_image_data', file_id ).web_path + "'>"+  fileEditor.file( 'products_image_data', file_id ).file + " [" +formatSizeUnits(fileEditor.file( 'products_image_data', file_id ).filesize)+ "]" + "</a>":
                                        null;
                                    },
                                    */
    
                                render: function ( file_id) {
    
                                    //stringa += '<a href="'+window.location.origin+fileEditor.file( 'products_image_data', file_id ).web_path+'" target="_blank">';
    
                                    if(file_id){
                                        var extention = fileEditor.file( 'products_file_data', file_id ).file.split('.').pop().toLowerCase();
                                        if("pdf" == extention){
                                            return '<i class="fas fa-file-pdf"></i>';
                                        }
                                        else if("doc" == extention){
                                            return '<i class="fas fa-file-word"></i>';
                                        }
                                        else if("docx" == extention){
                                            return '<i class="fas fa-file-word"></i>';
                                        }
                                        else if("zip" == extention){
                                            return '<i class="fas fa-file-archive"></i>';
                                        } else {
                                            return '<i class="fas fa-file-image"></i>';
                                        }
                                    } else
                                    return null;
                                    //return stringa;
                                },
    
    
    
                                defaultContent: "No image"
                                },
                                {
                                title: 'File Name',
                                data: 'file',
                                render: function ( file_id ) {
    
                                    return '<a target="_blank" href="dist/dl.php?t=products/file&f='+fileEditor.file( 'products_file_data', file_id ).web_path.split('/').pop() + '&c=' + "<?=$dl?>" + '">' + fileEditor.file( 'products_file_data', file_id ).file + '</a>';
                                }
                                },
                                {
                                title: 'Descrizione',
                                data: 'description'
                                },
                                {
                                title: 'Lingua',
                                data: 'lang',
                                width: '4em'
                                },
                                {
                                    title: "Pubblicare?",
                                    data: "publish",
                                    render: simple_spunta,
                                    className: "dt-body-center",
                                    width: '3em'
                                },
                                {
                                title: 'Ordine',
                                data: 'order',
                                width: '4em'},
                                {
                                    data:                   null,
                                    defaultContent:         '<i class="fa fa-pencil text-info"/>',
                                    className:              'row-edit dt-center',
                                    orderable:              false,
                                    width:                  '10px',
                                    responsivePriority:     1,
                                    <?=$ADMIN['login']->hasPermission('ModifyData')?"":"visible:                false"?>
                                },
                                {
                                    data:                   null,
                                    defaultContent:         '<i class="fa fa-trash text-danger"/>',
                                    className:              'row-remove dt-center',
                                    orderable:              false,
                                    width:                  '10px',
                                    responsivePriority:     1,
                                    <?=$ADMIN['login']->hasPermission('delete')?"":"visible:                false"?>
                                }
                            ],
                            columnDefs: [
                            { targets: 0, responsivePriority: 1, orderable: false},
                            ],
                            "searching":        false,
                            "paging":           false,
                            "order": [[ 1, "asc" ]],
    
                            buttons: [
                                { extend: 'create', editor: fileEditor },
                                { extend: 'edit',   editor: fileEditor },
                                { extend: 'remove', editor: fileEditor }
                                /*,'print', 'copy', 'excel', 'pdf', 'csv', 'colvis'*/
                            ]
                        });

Server Code PHP:

            Editor::inst( $ADMIN['db'], 'products_file', 'products_file_id' )
                    ->field(
                        Field::inst( 'products_file_id' ),
                            Field::inst( 'lang' )
                            ->options( Options::inst()
                                    ->table( 'lang' )
                                    ->value( 'code' )
                                    ->label( 'name' )
                                    ->where( function ($q){
                                        $q->where('lang.enable', 1, '=', true);
                                    })
                                    ->order( 'lang.code' )
                                ),
                        Field::inst( 'publish' ),
                        Field::inst( 'file' )               
                            ->setFormatter( Format::ifEmpty( null ) )
                            ->upload(
                                Upload::inst( $_SESSION['config']['main_folder'] . 'data/products/file/__ID__.__EXTN__' )
                                    ->db( 'products_file_data', 'products_file_data_id', array(
                                    'file'        => Upload::DB_FILE_NAME,
                                    'filesize'    => Upload::DB_FILE_SIZE,
                                    'web_path'    => Upload::DB_WEB_PATH,
                                    'system_path' => Upload::DB_SYSTEM_PATH
                                    ))
                                /*  ->dbClean( function ($data) {
                                        
                                        // Remove the files from the file system
                                        try {
                                            for ( $i=0, $ien=count($data) ; $i<$ien ; $i++ ) {
                                                    unlink( $data[$i]['system_path'] );
                                            }
                                        } catch (Exception $e) {}
                         
                                        // Have Editor remove the rows from the database
                                        return true;
                                    })
                                */  ->validator( Validate::fileSize( 20000000, 'Files must be smaller that 5Mb' ) )
                                    ->validator( Validate::fileExtensions( array( 'pdf', 'zip', 'doc', 'docx' ), "Please upload a document" ) )
                        ),
                        Field::inst( 'products_id' ),
                        Field::inst( 'order' )
                                ->validator( Validate::notEmpty() )
                                ->validator( Validate::numeric() ),
                        Field::inst( 'description' )
                    )
                    ->where( 'products_file.products_id', $_POST['products_id'], '=' )
                    
                    ->where( 'products_file.enable', 1)
                    ->process( $_POST )
                    ->write( $ADMIN['login']->hasPermission('ModifyData') )
                    ->debug($_SESSION['config']['PHP']['SHOW_ERROR'])
                    ->json();

Description of problem:

If datables id configure with "serverSide: true", deleting a file takes a lot of time, and sometimes it is not completed.

Other, if "serverSide: false" the software works correctly, but the table is not update when the file deletion is completed.

I would like that when I delete a file or insert it, the table is updated at the end of execution. Unfortunately, enabling ServerSide creates this problem for me.

Answers

  • allanallan Posts: 64,059Questions: 1Answers: 10,559 Site admin

    I guess the first thing to do is to confirm if you want server-side processing enabled or not. How many records do you have in the table?

    The table should certainly update either way. Can you show me the JSON response when you are running into the problem?

    Thanks,
    Allan

Sign In or Register to comment.