Sequencing of PHP Editor Events

Sequencing of PHP Editor Events

TomBajzekTomBajzek Posts: 163Questions: 36Answers: 1
edited November 2017 in DataTables 1.10

I seeme to be having a problem with the sequencing of PHP editing events, which I will describe here:

I have a database of trouble tickets which have a status field, indicating whether a ticket is "unseen", "in progress", "closed", etc. Right now, when a technician opens an "unseen" ticket, he is supposed to set its status to be "in progress". The goal is to update the status automatically when the ticket is edited, rather than requiring the technician to do this. In the case of other prior values of the status field, nothing should be done automatically.

Perhaps I'm misunderstanding the PHP Events, or their sequencing.

I'm trying to use a postGet event handler to obtain the prior value of the status field. I intend to then test this prior value of the status field and set the value of the status field, if necessary, in a preEdit event handler. This did not work, so I inserted some debugging statements. These revealed that the preEdit event occurs before the postGet event, which I did not expect, and which cannot work.

Have I misunderstood what the postGet event? Have I misunderstood the sequence of events? Is there some other way I should be going about this?

Here are the relevant event handlers from my Editor code:

    ->on('postGet',function($editor,$data,$id) {
        global $priorStatus;
        error_log('t.t.p: pG.id= ' . $id);
        if (isset($data['status'])) {
            error_log('t.t.p: priorStatus= ' . $data['status']);
            $priorStatus = $data['status'];     // Fetch status at time of edit     
        }
    } )
    ->on('preEdit', function($editor,$id,$values) {
        global $priorStatus;
        error_log('t.t.p: pE.id= ' . $id . ', priorStatus= ' . $priorStatus);
        if (isset($values['status']) and $values['status'] == 'closed') {
            $editor->field('priority')->setValue(NULL);
        } 
        if ($priorStatus == 'unseen' or $priorStatus == 'urgent') {
            $editor->field('status')->setValue('in  queue');
        }
    } )         

Thanks in advance,
Tom

Replies

  • allanallan Posts: 61,650Questions: 1Answers: 10,094 Site admin

    Hi Tom,

    What is happening is that postGet will be triggered when the initial data is fetched for the table to be populated with data.

    Then, when you submit an edit, it will perform the edit, and then get the data for that row from the database again (specifically for that row only). That is why you are seeing postGet happen after an edit.

    The goal is to update the status automatically when the ticket is edited, rather than requiring the technician to do this.

    Should this happen automatically, or only if they submit the form? If automatically, use initEdit on the client-side and have it submit an Ajax request to the server to mark it as read.

    If it needs the form to be submitted, use initEdit with field().val() to set the value to be submitted.

    Allan

This discussion has been closed.