Programmatically Setting a Field's Value / ajax option

Programmatically Setting a Field's Value / ajax option

Karl53Karl53 Posts: 72Questions: 29Answers: 0

There are aspects of Editor that are a black box to me which I'm seeking clarification on.

Setup/Configuration

I do not consider that my application follows a client/server model as documented (thought maybe from Editor's point of view it sitll is client / server.) My code initializes an empty table and exclusively uses the DataTable API.

That is:

    table = $('#MY_TABLE').DataTable({
        "data": null,

The table is initialized this way:

    function addEmptyRows(cnt) {
        for (i = 0; i < cnt; i += 1) {
            table.row.add({"idx" : i,
                "COL_A" : i + 1,
                "COL_B" : null,
                "COL_C" : null,
                "COL_D" : null,
                 });
        }

NOTE 1: I'm using inline editing.

NOTE 2: The null columns' defaultContent option are each set to an empty string.

Question 1:

All data either comes from user input or needs to be programmatically generated. It is the programmatically generated data that I can't get to work. If a user enters data in COL_B I want to supply a default value in COL_C based upon the value entered in COL_B. The user may or may not edit the initialized value in COL_C.

I would like that the value for COL_C to be determined after the user has edited COL_B, so sometime during the submit process is fine.

I've tried:

    editor
        .on('preSubmit', function (e, o, a) {
            if (o.action === 'create' || o.action === 'edit') {
                o.data.COL_C = 100;
            }
        }

But now I understand that o.data.COL_C is only submitted to the server. It is not used to update DataTables data.

1: How do I dynamically set the value of COL_C and where do I place the code?

I'm always only setting a value in a column in the row being edited.

Question 2:

I'm using the ajax option as a function. I'm confused about the callback functions, particularly the success function.

This is my code, and it seems to work fine.

    "ajax": function (method, url, data, success, error) {
        success({});
    },

2: What does success need to accomplish? I'm confused by the parameter. I note that success is required and when it's missing, the field editor never closes. (Should there not be a time out exception raised rather than having the editor close fail?)

2b: Is my implementation of the success call back ok?

2c Do I need to be providing an error callback and if so, what would be the pattern inside the ajax function given the above example with the success implementation.

Thanks.

(BTW, my preview button is disabled. I hope that does not indicate there is an error in this posts markup that prevents it from being viewed ok.)

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Answer ✓

    Hi Karl,

    Q1) You are absolutely correct, the problem here is that preSubmit will modify the data that is being submitted to the server. It does not alter the values that Editor contains.

    When the success callback is called (above you are giving it as an empty object), Editor will detect if there is a row parameter or not. If there is it will use the data from it as the data for the row. If there is not it will attempt to use the data from the field values as take a best guess at reconstructing an object for DataTables to use.

    The problem you are facing here is that reconstructed object is based on the unmodified values from before preSubmit.

    I would strongly encourage you to update your ajax function mimic the client / server data exchange that Editor expects. The data for the row would come from wherever you have stored it in this case.

    Q2a) success is a callback function. It should be executed when the function you have provided has completed whatever tasks it needs to do, in order to tell Editor that it is finished. Not executing the function means that the task will never finish.

    Q2b) As above I would very much suggest you implement the protocol Editor expects. Otherwise you will encounter issues such in Q1.

    Q2c) If you want to handle errors, then yes the error callback should be executed when an error occurs. Again this is to let Editor know that an error has occurred. Only one of success or error should be executed, but one of them must at some point. It needn't be immediately (that's why they are provided as callbacks - so you can execute them asynchronously after whatever task has been completed). But one of them be at some point.

    Hope this helps to explain it!

    Regards,
    Allan

  • Karl53Karl53 Posts: 72Questions: 29Answers: 0

    Hi Allan,

    As always, you got me on track. I think I was just skimming the documentation on your site too much because I had the wrong mindset thinking that the discussion about client server didn't apply to what I wanted to do.

    Karl

This discussion has been closed.