getting idSrc in preSubmit

getting idSrc in preSubmit

jangojango Posts: 5Questions: 3Answers: 0
edited December 2015 in Editor

I was looking at this question: https://datatables.net/forums/discussion/25609/retrieving-the-mysql-id-for-a-newly-created-row

When I do this:

  editor.on( 'preSubmit', function ( e, data, action ) {
    console.log(data);
 });

I get the following in the console:

data: Object
--- 2: Object
------ category_name: "Car - Fuel"

2 is the correct id set by idSrc, however, when I try to do data.id I get "undefined", because obviously "id" is not a valid property name. Any idea why is my data object messed up?

There is I am trying to do that, is because I would like to push an extra parameter so that what goes to my server is:

data: Object
--- 2: Object
------ category_name: "Car - Fuel"
------ category_id: "23"

Update:

I just did this:

  editor.on( 'preSubmit', function ( e, data, action ) {
   var fieldInstance = editor.field( editor.displayed()[0] );

   $.each( data.data, function ( key, value ) {
      console.log(key);
      data.data[key].category_id = fieldInstance.input().attr('item_id');
    });

    console.log(data);

 });

This kind of works... I get this in the post:

(u'data[1][category][name]', u'Car - Fuel')
(u'data[1][category_id]', u'7')

So now I can use 7 to lookup the category and set it for the row. Item_id is just an input attribute I set when autocomplete on the field sets a value.

I guess the question is - is this a sane approach or is there a simpler way to do something like that?

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,522Questions: 1Answers: 10,473 Site admin
    Answer ✓

    2 is the correct id set by idSrc, however, when I try to do data.id I get "undefined", because obviously "id" is not a valid property name. Any idea why is my data object messed up?

    I don't think it is messed up. The ID is in the key information as you noted, so Editor doesn't also send it as a field value since that would seem to be redundant information.

    Getting the keys in the manner you have in your update is the perfect way to approach this :-).

    Allan

  • jangojango Posts: 5Questions: 3Answers: 0
    edited January 2016

    Thank you Allan!

This discussion has been closed.