Multiple editor, initEdit and row id

Multiple editor, initEdit and row id

cent89cent89 Posts: 19Questions: 6Answers: 0

Hi, I have a page with an array of editors, attached to multiple tables.

var editors = [];
var table = $('#weektable_'+id_week).DataTable({...});
editors[id_week] = new $.fn.dataTable.Editor({ table: '#weektable_'+id_week, });

The editor has a field, "valore"; it's type (and value) depends on an attribute of the selected row, so the type can be text, number, checkbox, select, ...

I have a trigger on initEdit that retrive the type and value from ajax, and update the field:

editors[id_week].on('initEdit', function ( e, node, data, items, type ) {
            $.ajax({
            type: "POST",
                async: false,
            url: "{{ route('eventspecvalues.valore_field') }}",
            data: {
              id_eventspecvalue: data['DT_RowId'],
              _token : "{{csrf_token()}}",
            },
            success: function(data2){
              editors[id_week].clear('valore');
              editors[id_week].add(JSON.parse(data2));
            },
            error: function(XMLHttpRequest, textStatus, exception) { alert("Ajax failure\n" + XMLHttpRequest.responseText + "\n" + exception); },
          });

});

It works only with bubble editing; with inline editing, the "valore" field takes the type/values of the previus rows selected.
After some debug, I have noticed that first editor fires inline trigger, after initEdit.
I can't add the ajax code to inline trigger because I can't retrive the row id:

$('#weektable_'+id_week).on('click', 'tbody td:not(:last-child)', function (e) {
            console.log(editors[id_week].field('id').val()); //NULL VALUE
            
        editors[id_week].inline(this, {
          onBlur: 'submit',
          submit: 'allIfChanged'
        });

            console.log(editors[id_week].field('id').val()); //WORKS!
      });

Have some idea on how to solve the problem?

Thanks!
Roberto

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 64,008Questions: 1Answers: 10,554 Site admin
    Answer ✓

    Hi Roberto,

    Editor isn't really designed to have the field type changed once the form editing has been triggered. What I would suggest is that you make the Ajax call to get the field type before triggering inline editing (i.e. do it in your click event handler, rather than in the initEdit event).

    Then, once you've changed the field type around in the Ajax success handler, call the inline() method.

    Allan

This discussion has been closed.