Editor Duplicate button form not submitting json object

Editor Duplicate button form not submitting json object

sarath_surisettysarath_surisetty Posts: 54Questions: 13Answers: 0

i had included Duplicate button for the editor,when clicked on button the form popup is generated with the data selected for duplicate, but the actual problem is when i click create button, data not posted like object how it posted when i used add button.instead it is posting like datatable input.
Could you please support in achieving the format how the add button form post works?

CODE:

buttons: [
          { extend: "create", editor: propertyEditor },
          { extend: "edit",   editor: propertyEditor },
          {
              extend: "selected",
              text: 'Duplicate',
              action: function ( e, dt, node, config ) {
                  // Start in edit mode, and then change to create
                  propertyEditor
                      .edit( PropertyTable.rows( {selected: true} ).indexes(), {
                          title: 'Duplicate record',
                          buttons: 'Create from existing'
                      } )
                      .mode( 'create' );
              }
          },
          { extend: "remove", editor: propertyEditor },
          {
              text: 'Reload',
              action: function ( e, dt, node, config ) {
                  dt.ajax.reload();
              }
          }
          ]

This question has an accepted answers - jump to answer

Answers

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

    I'm not certain as you don't say, but I've just answered a very similar question by someone else, and they are using the ajax option to define different options for Ajax submission of data. I'm guessing that is the same here.

    This was my reply:

    The issue here is that the data in d.data doesn't have an index 0. Instead try:

      data: function ( d ) {
        var submit = $.map( d.data, function ( value, key ) {
          return value;
        } );
    
        return JSON.stringify( submit[0] );
      }
    

    That will convert the object (or an array if it really is a create action) to an array and then use that to submit to the server.

    Regards,
    Allan

  • sarath_surisettysarath_surisetty Posts: 54Questions: 13Answers: 0
    edited August 2018

    hi allan

    i have already ajax for editor :
    it is working as expected for create(add),edit,remove.
    problem is when duplicate button implemented.
    reference link : https://editor.datatables.net/examples/api/duplicateButton.html

    new $.fn.dataTable.Editor( {
             "ajax":{
                 create: {
                     type: 'POST',
                     url:  '/api/add',
                     contentType: 'application/json',
                     data: function ( addData ) {
                         return JSON.stringify( addData.data[0] );
                     },
                      error: function (e) {
    
                         alert("ERROR: ", JSON.stringify(e));
                     }
                 },
    
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Answer ✓

    It looks like you haven't tried adding the code I suggested above. The issue is that the addData value has a primary key row identifier from the edit, so you need to map it down to a plain array.

    Allan

  • sarath_surisettysarath_surisetty Posts: 54Questions: 13Answers: 0

    It worked thank allan

This discussion has been closed.