ajax url or post data while uploading

ajax url or post data while uploading

nessinitsnessinits Posts: 86Questions: 27Answers: 0

Hi all,

I have an child editor in a parent-child solution, that I use to add documents to my employees. I need to add the foreign id of the employee to the document table, but I need that data also while uploading the file. I tried several ways. Below you find te latest not working solution. As you can see, I tried to use a function to build up the url dynamically. But this results in adding the function to the ajaxurl and this eventually results in a 404 of course.

I've also tried to add a regular variable to the url ( url: 'datatables/php/table.document.php?foreign_id='+key and changing the variable when a row is selected in the parent (employee) table, but the url always sends the initial value of the variable.

    var documenteditor = new $.fn.dataTable.Editor( {
        ajax: {
            url: function ( u ) {
                var selected = employeetable.row( { selected: true } );
 
                if ( selected.any() ) {
                    return 'datatables/php/table.document.php?foreign_id='+selected.data().employee_id;
                }
            },
            data: function ( d ) {
                var selected = employeetable.row( { selected: true } );
 
                if ( selected.any() ) {
                    d.foreign_id = selected.data().employee_id;
                }
            }
        },

Does anyone have a clue?

Kind regadrs,
nessinits

Replies

  • bindridbindrid Posts: 730Questions: 0Answers: 119

    Your url is in an if statement. If it evaluates false, the url will be undefinded.

    Your data section does not return anything but undefined since it does not return anything.

    And you don't need both a url with the parameters attached and a data that also returns parameters.

  • nessinitsnessinits Posts: 86Questions: 27Answers: 0

    Hi,

    Thanks for your response.

    It doesn't evaluate false, because it can only be posted when an emloyee is selected.

    I don't understand your emark on the data section. Mainly the part where you say: since it does not return anything. Could you be more specific why?

    I tried both ways because of I want to resolve this one way or the other. The data way is the cleanest, of course.

    Hope you can help me a little more.

    Kind regards,
    nessinits

  • tangerinetangerine Posts: 3,365Questions: 39Answers: 395
           data: function ( d ) {
                var selected = employeetable.row( { selected: true } );
     
                if ( selected.any() ) {
                    d.foreign_id = selected.data().employee_id;
                }
            }
    

    That function does not return anything, as @bindrid says.

  • nessinitsnessinits Posts: 86Questions: 27Answers: 0

    How is that different from the example from the website below:

    var editor = new $.fn.Editor( {
        ajax:  {
            url: 'php/staff.php',
            data: function ( d ) {
                d.user_id = $('#user_id').val();
            }
        },
        table: '#myTable'
    } );
    
  • allanallan Posts: 63,258Questions: 1Answers: 10,421 Site admin

    I don't understand your emark on the data section. Mainly the part where you say: since it does not return anything. Could you be more specific why?

    That statement wasn't quite accurate - although I completely concur with the point about the url being undefined. It should at least point at an error script I would say.

    The way the ajax.data option works as a function is that you can modify the object passed in and that will be used. Alternatively, you can return an object and DataTables / Editor will use that instead. Both are valid.

    As you can see, I tried to use a function to build up the url dynamically

    I don't think you can define the URL as a function in jQuery and that isn't a feature that DataTables adds.

    but I need that data also while uploading the file

    This with the upload field type is it? You can give it its own ajax option that submit parameters specifically for that. However, the ajax.data function should be used. Could you give me a link to the page please?

    Allan

This discussion has been closed.