uploadMany : editor.files not populating on Standalone editor.

uploadMany : editor.files not populating on Standalone editor.

cgvmcgvm Posts: 8Questions: 2Answers: 0
edited March 2017 in Free community support

Debug info

http://debug.datatables.net/eqorob

Introduction

I send the following json response to the datatables client after uploading a file through the row editor modal:

JSON Data

{
    "files": {
      "files": {
        "124": {
          "filename": "testfile.png",
          "web_path": "\/images\/9c8b7860-78ce-4d17-823a-39673ce0a645.png"
        }
      }
    },
    "upload": {
      "id": "124"
    }
  }

Code that threw an error

    {
      label: "Images",
      name: "files[].id",
      type: "uploadMany",
      display: function (fileId, counter) {
        return '<img src="' + editor.file('files', fileId).web_path + '"/>';
      },
      noFileText: 'No images',
      ajax: '/upload/'
    }

Error: No table named files

Digging deeper

Editor.files shows nothing in it!

If I add

Editor.files.files = {}

to

                    if ( json.files ) {
                        // Loop over the tables that are defined
                        $.each( json.files, function ( table, files ) {
                            $.extend( Editor.files[ table ], files );
                        } );
                    }

It appears to work fine.

Cause ?

It's possible that the following event needs to run but does not?

// Global listener for file information updates via DataTables' Ajax JSON
$(document).on( 'xhr.dt', function (e, ctx, json) {
    if ( e.namespace !== 'dt' ) {
        return;
    }

    if ( json && json.files ) {
        $.each( json.files, function ( name, files ) {
            Editor.files[ name ] = files;
        } );
    }
} );

Solution ?

Added

$.fn.DataTable.Editor.files.files = {};

Answers

  • allanallan Posts: 55,832Questions: 1Answers: 8,817 Site admin

    Hi,

    The xhr.dt event that you mention most certainly does need to run for this to work. Are you saying that it doesn't run?

    Looking at your debug trace, you aren't using ajax in the DataTable - so yes, it wouldn't be running, and that would certainly explain the issue.

    Interesting one - I'm not immediately sure what the fix should be since DataTables doesn't (currently) have an event for the table being populated with data (although there is the init event which it could possibly listen for and check if the data is Ajax sourced or not).

    Allan

  • cgvmcgvm Posts: 8Questions: 2Answers: 0
    edited March 2017

    Right, I'm not using the ajax because I plan on using the local edits to the data table to create a larger submit combined with other data; I don't want anything submitted to the database until all the details are added. (Header Data+Rows)

    Thanks!
    -Cody

  • allanallan Posts: 55,832Questions: 1Answers: 8,817 Site admin

    I think your workaround is probably as good as it gets at the moment in that case.

    What I probably need to do is define a public API that has the ability to set this information. Thinking about it listening for init isn't going to be good enough since it has no access to the file information! It only has access (or perhaps more correctly knowledge) of the row data.

    Allan

This discussion has been closed.