Problems modeling data with Editor

Problems modeling data with Editor

Lucho_1312Lucho_1312 Posts: 30Questions: 9Answers: 0
edited September 2015 in Free community support

Hi!
I'm using Editor in my dataTable, and I'm trying to use it with a REST API in a NodeJS server.

The problem is that I need the data in this way:

{
   "name": "newValue"
}

but Editor gives me the data like this:

{
   "action": "edit",
   "data": {
      "row_2": { 
         "name": "newValue"
      }
   }
}

Is there anyway to model that data before the submit?
I found the presubmit event, but I don't know how to used and I couldn't find any suitable example :(

Thanks!
Luciano

This question has an accepted answers - jump to answer

Answers

  • Lucho_1312Lucho_1312 Posts: 30Questions: 9Answers: 0

    Ok, so I did this, but it doesn't work :(

    editor.on('preSubmit', function(e, data, action) {
        var newObj = {};
        for (var key in data.data) {
            if (data.data.hasOwnProperty(key)) {
                newObj = data.data[key];
                break;
            }
        }
        data = newObj;
    });
    

    My REST API only supports editing one row at a time, so I simplify my data. Also, I'm trying to replace all the information with a new object that has all the data as I need it.

    The problem is that the posted data is the same as before. If I add parameters, everything works perfect, but when I try to replace the data object, my luck is gonne :(

    Any ideas?

    Thanks!

  • Lucho_1312Lucho_1312 Posts: 30Questions: 9Answers: 0

    So I finally did it. I don't know if it's the best way, but it worked...

    editor.on('preSubmit', function(e, data, action) {
        // Create new object in the needed format
        var newObj = {};
        for (var key in data.data) {
            if (data.data.hasOwnProperty(key)) {
                newObj = data.data[key];
                break;
            }
        }
    
        // Delete unused components of the data object
        delete data.action;
        delete data.data;
    
        // Apply the new data object to the original data object
        data = $.extend(data, newObj);
    };
    

    The only problem I have now, and that I'll like to solve is this:
    How can I post ONLY ONE row in the 'data.data' object?

    I want to avoid the need to check for the first element in the for and then do a break.

    Thanks!
    Luciano

  • allanallan Posts: 63,745Questions: 1Answers: 10,509 Site admin
    Answer ✓

    How can I post ONLY ONE row in the 'data.data' object?

    You could enable the legacyAjax option which is limited to a single row only. This page documents the legacy data format that Editor used.

    Allan

  • Lucho_1312Lucho_1312 Posts: 30Questions: 9Answers: 0

    THANKS!

    It worked perfectly, now I can remove my little "hack" :D

This discussion has been closed.