Dependent Function Fires when other fields clicked

Dependent Function Fires when other fields clicked

Scott BowersScott Bowers Posts: 13Questions: 2Answers: 0
edited April 2020 in Free community support

I tried to create a live example with inline editing and no luck, the inline just wouldn't work. I have a table, with inline editing setup. I have a couple dependent functions. One has an ajax call in it and I noticed that when I click in a different cell that also has a dependent function both dependent function fire off. They end up firing back and forth just from clicking in the cell. Why would that happen.

dependent function 1:

this.editor.dependent("attributes.AVG_MSTR",dojo.hitch(this,function(val,data,callback){
                    if(val == data.row.attributes.AVG_MSTR) callback({});
                    else{
                        some code....
                       callback(data);
                    }
                })),

dependent function 2

                this.editor.dependent("MAP_AGCY_CROP_ID", dojo.hitch(this, function(val,data,callback) {
                    if(val === "" || (val ===  data.row.attributes.MAP_AGCY_CROP_ID)) callback({});
                    else{

                        do bunch of ajax stuff then callback(data);
                },

Replies

  • kthorngrenkthorngren Posts: 21,343Questions: 26Answers: 4,954

    Not sure about your problem but here is a basic Inline Editor example you might be able to use for a test case:
    http://live.datatables.net/vagapezu/1/edit

    Kevin

  • Scott BowersScott Bowers Posts: 13Questions: 2Answers: 0
    edited April 2020

    Here, http://live.datatables.net/vagapezu/6/edit click any field to edit and view the console, the Name dependent function fires no matter what cell you click in. Shouldn't that only fire if you change the Name column? So, what is happening then when you have multiples of these functions, you are having functions firing all over the place when someone click a cell. This is not good if one of those is an ajax call.

  • allanallan Posts: 63,534Questions: 1Answers: 10,475 Site admin

    Hi,

    It triggers as soon as editing starts because the value of the field is set. It has to be called at that point so the form can be synced up to the value that is being edited.

    Keep in mind that with inline editing, the whole form is actually placed into edit mode - although only a single field is visibly editable, you could use the API to update the other fields if you needed.

    Allan

  • Scott BowersScott Bowers Posts: 13Questions: 2Answers: 0

    That renders the dependent function kind of useless when using inline editing as it doesn't matter what you make dependent. All functions trigger no matter what you edit. This can cause circular loops and if you have Ajax calls in any of them you can clog up your browser. I did add some logic to check for a few things:

    // is the form loading 
    if(!data.row) callback({});
    //did someone click a a cell but not edit
    else if(val === "") {
                                    let obj = {
                                        values:{MAP_AGCY_CROP_ID:data.row.attributes.MAP_AGCY_CROP_ID}
                                    };
                                    callback(obj);
                                }
    //did some one click this cell but not actually change the value
     else if(val ===  data.row.attributes.MAP_AGCY_CROP_ID) callback({});
    //someone actually change the value
                                else{
    //do the actually work you needed....
    }
    
    
  • allanallan Posts: 63,534Questions: 1Answers: 10,475 Site admin

    There is one option in Editor that might be of some help here - the scope property of form-options. By default it is set to row which causes the whole row to be placed into editing mode. But you could set it to be cell which might address what you want here.

    But you need to be aware that if you use that option, you can't edit the other cells in the row - which is why it defaults to row (I did actually have it default to cell back when I first introduced that option, but it caused all sorts of support requests!).

    Allan

  • Scott BowersScott Bowers Posts: 13Questions: 2Answers: 0

    Thanks Allan, I will give this a try.

This discussion has been closed.