Is it possible, in an event handler, to update the value of a record in a related table?

Is it possible, in an event handler, to update the value of a record in a related table?

TomBajzekTomBajzek Posts: 164Questions: 37Answers: 1

I've built a trouble-ticket application which allows technicians to post a note to a ticket. (The relation is that a ticket may have zero or more notes attached.) In posting a note to a ticket, the technician may indicate that the ticket has been resolved. I would like to be able to close the ticket when a note is posted that indicates that the ticket has been resolved. In the "submitSuccess" handler for notes (client-side), I can determine that the ticket has been resolved. I haven't figured out how to update the ticket to set its status to be 'closed' from the event handler for the note.

Is it possbie to do this? The Javascript file that does this contains editor instances and datatables for both the tickets and the notes.

Thanks,
Tom

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,689Questions: 1Answers: 10,500 Site admin

    Hi Tom,

    How do you want to mark the ticket as closed? A CSS colour, a class, an API method or something else?

    Allan

  • TomBajzekTomBajzek Posts: 164Questions: 37Answers: 1

    Allan,

    I want to change the status field in the tickets table to be 'closed'. It's a DB change, not a visual indicator that I need. I think I need to update the tickets table from within the event handler for notes.

    Tom

  • TomBajzekTomBajzek Posts: 164Questions: 37Answers: 1

    Allan,

    Studying the documentation, I came to believe that what I need to do here is to use the editor.edit method as described in https://editor.datatables.net/reference/api/edit
    to update the status field in the selected row of the tickets table.

    I tried that, but the status field is not being updated. Here is the relevant section of my code:

        // If a note is marked as resolving the ticket, close the ticket
            noteEditor.on( 'submitSuccess', function () {
                if (noteEditor.field('notes.resolved').val() === 'yes') {
                    ticketEditor.off('initEdit');   // Don't need to trap this as we are closing it
                    ticketEditor
                        .edit(ticketTable.row( { selected: true } ).data().id,false)
                        .set('status','closed')
                        .submit();
                }
                alert('resolved');
                ticketTable.ajax.reload();
            } );
         
            ticketEditor.on( 'submitSuccess', function (e,json,data,action) {
                noteTable.ajax.reload();
                if (data.status === 'closed') {
                    ticketTable.row( { selected: true } ).deselect();
                    ticketTable.ajax.reload(null,true);
                } else {
                    ticketTable.ajax.reload(null,false);                
                }
            } );
    

    The architecture of the application is modeled on your blog post Parent / Child Editing, https://datatables.net/blog/2016-03-25. The application has been working for many months now, and the problem arises in a feature addition I am trying to add now. By using the debugger, I know that I'm going through this piece of code at the proper time and that the correct ticket is selected in the 'submitSuccess' handler for the noteEditor. I have included the 'submitSuccess" handler for the ticketEditor, because that could conceivably matter, although the behavior is the same if I disable that handler before invoking the tickerEditor.

    Can you suggest what might be wrong, or how I might isolate the problem?

    Thanks,
    Tom

  • allanallan Posts: 63,689Questions: 1Answers: 10,500 Site admin
    Answer ✓

    Remove line 11 from the above (ticketTable.ajax.reload();). That shouldn't be required and it might be causing a problem since both it and the submit from a few lines above are async.

    Allan

  • TomBajzekTomBajzek Posts: 164Questions: 37Answers: 1

    Allan,

    I tried that, but it did not change the outcome. However, I'm going to look other callbacks in the code (there are many other that I did not include in my snippet), to see if any of those might be interfering. I'll let you know if that approach fixes this.

    Thanks for the suggestion,
    Tom

  • TomBajzekTomBajzek Posts: 164Questions: 37Answers: 1

    Allan,

    I tried disabling each ajax.reload of the ticketTable. All of those happen to be in the snippet of code I posted, at lines 11, 18, and 20.

    I then inserted a line after 5 to disable the "submitSuccess" handler for the ticketTable, in case its presence was the cause, but that did not make a difference, either.

    I examined all of the event handlers in my code and did not find any others that should have any effect on this. I've determined that the handler for the ticketTable "submitSuccess" in line 14 does not hit, anyway, which eliminates one possibility I was considering.

    This leaves me at a loss as to why this is failing. Do you have any other suggestions?

    Thanks,
    Tom

  • allanallan Posts: 63,689Questions: 1Answers: 10,500 Site admin

    If you could link to a page showing the issue I can take a look into it.

    Allan

  • TomBajzekTomBajzek Posts: 164Questions: 37Answers: 1

    Allan,

    I appreciate your offer. However, this page is in a protected directory. Can we communicate privately so that I can send you credentials for accessing it?

    Thanks,
    Tom

  • allanallan Posts: 63,689Questions: 1Answers: 10,500 Site admin

    Certainly - you can send me a PM by clicking my name above and then the "Send message" button.

    Allan

This discussion has been closed.