reference data in preSubmit

reference data in preSubmit

montoyammontoyam Posts: 568Questions: 136Answers: 5
edited March 2020 in Free community support

given the ajax return like this:

DT_RowId: "row_1508"
Submissions: {SubmissionStatusID: 2, AssignedToUserID: 2, SubmissionTypeID: 0, QuestionCategoryID: 0,…}
SubmissionStatus: {SubmissionStatus: "Assigned"}
SubmissionTypes: {SubmissionType: null}
QuestionCategories: {QuestionCategory: null}
ResolutionMethods: {ResolutionMethod: null}
AssignedTo: {UserName: "montoyam"}
CCAssignedTo: {UserName: null}

how do I reference a data element in presubmit?

            SubmissionsEditor.on('preSubmit', function (e, data, action) {
                alert(data.Data.QuestionCategories.QuestionCategory);
                //return false;
            });

Replies

  • rf1234rf1234 Posts: 2,801Questions: 85Answers: 406
    edited March 2020

    Please use your debugger and check the contents of "data". That is the simplest way to find out.

    In this example I change the action to be submitted from "edit" to "create" for a special reason ...

    .on( 'preSubmit', function ( e, d, action ) {
        if ( action === 'edit' && editCreate ) {
            d.action = 'create';
            editCreate = false;
        }        
    })
    

    But that is not going to work for your submitted field values. They should be in an array like d.data[0].yourField if you are using single edit.

  • montoyammontoyam Posts: 568Questions: 136Answers: 5

    this worked in the browser console, but not in javascript:
    data.data[0].QuestionCategories.QuestionCategory

  • rf1234rf1234 Posts: 2,801Questions: 85Answers: 406
    edited March 2020

    Mmhhh... don't know. You might have a different problem. This works fine for me:

    thisTable
    .on( 'preSubmit', function ( e, d, action ) {
        if ( action === 'create') {
            var selected = thisTable.row({selected: true});
            if (selected.any()) {
                var acctSystem = selected.data().parentTable.acct_system;
                if ( acctSystem !== 'someSystem') {
                    if ( d.data[0].thisTable.external_id <= '' ) {
                        this.error(function () { return lang === 'de' ?
                            "Sie müssen zuerst FiBu Vertragsdaten erfassen." : 
                            "You need to enter Accounting Contract Data first.";
                        }); 
                        return false; // prevent the submission
                    }
                }
            }
        }
    })
    

    Sorry, I got confused when renaming my original data table names ... you'll figure it out ...

  • montoyammontoyam Posts: 568Questions: 136Answers: 5

    ah, I am not looking at selected row. I thought that was just for the datatable, I didn't know it was needed for the editor. I will give it a try

  • montoyammontoyam Posts: 568Questions: 136Answers: 5

    ok, so it works when I use the datatable. however, that of course would not have the data that is about to be submited. When I change line 2 to pull from selected editor row it doesn't work. I'm guessing line 2 needs to change??

                SubmissionsEditor.on('preSubmit', function (e, data, action) {
                    var selected = SubmissionsEditor.row({ selected: true });
                    if (selected.any()) {
                        //alert(selected.data().Submissions.QuestionCategoryOther);
                        if (selected.data().QuestionCategories.QuestionCategory == "Other" && selected.data().Submissions.QuestionCategoryOther == null) {
                            alert("You must enter something in 'QuestionCategoryOther");
                            return false;
                        }                    
                    }
                });
    
    
  • kthorngrenkthorngren Posts: 20,139Questions: 26Answers: 4,734

    SubmissionsEditor.row({ selected: true })

    Instead of SubmissionsEditor, which is your Editor instance, you need to use the variable assigned to your Datatable instance.

    Kevin

  • montoyammontoyam Posts: 568Questions: 136Answers: 5

    but this is on presubmit. the data doesn't live in the datatable instance yet (at least it doesn't appear to be).

    In line 1 of my posted code there is a variable in the function 'data'

    function (e,data,action){
    

    doesn't that contain the data without having to grab a selected row? if it does, how do I reference it given the structure of my json?

  • kthorngrenkthorngren Posts: 20,139Questions: 26Answers: 4,734

    doesn't that contain the data without having to grab a selected row? if it does, how do I reference it given the structure of my json?

    Well, maybe the getting the selected row is not what you want. I was just following rf1234 answer. Yes, according to the preSubmit docs the data parameter is:

    The data object that will be submitted to the server

    Ho do you access it? Its hard to say without understanding your data structure, ie, the columns config. What is the output of this?

    SubmissionsEditor.on('preSubmit', function (e, data, action) {
        console.log(data);
    });
    

    Kevin

  • rf1234rf1234 Posts: 2,801Questions: 85Answers: 406

    I hope I didn't confuse you guys. It is almost 2 am here ... and I had a little celebration by myself: I rolled out a big piece of software today! ... my favorite bar is closed .. my wife's sleeping ... youTube ... a couple of beers ... not a real life isn't it? I hope Covid-19 will disappear soon!!

  • colincolin Posts: 15,112Questions: 1Answers: 2,583

    Congrats on the software release! All bars are closed here too, everyone at home, so all very odd!

  • montoyammontoyam Posts: 568Questions: 136Answers: 5

    ok, so I was looking at the json return of the api, not realizing that the editor does have all the left joined tables. my first mistake :neutral:

    But, I still am not sure how to access the editor data. I see a row number in the console dump, which of course would change for each row.

    data:
    row_1508:
    Submissions:
    SubmissionStatusID: 2
    AssignedToUserID: 2
    SubmissionTypeID: 0
    QuestionCategoryID: 0
    EmployeeName: ""
    EmployeeDepartment: ""
    EmployeeClassification: ""
    EmployeeQuestion: ""
    DueDate: ""
    NeedsCCReview: "1"
    CCUserID: 23
    ResolutionMethodID: 0
    ResolutionMethodOther: ""
    
  • montoyammontoyam Posts: 568Questions: 136Answers: 5

    this works, but the row number is hard coded, which of course won't work

    data.data.row_1510.Submissions.QuestionCategoryID
    
  • kthorngrenkthorngren Posts: 20,139Questions: 26Answers: 4,734
    edited March 2020

    See if data.QuestionCategoryID works. Did you try adding the console.log statement I suggested? That should show you exactly what you need to use.

    Kevin

  • montoyammontoyam Posts: 568Questions: 136Answers: 5

    yes, the console log results were posted above. data.QuestionCategoryID didn't work.

    here is the console log in the exact format:

    "{"data":{"row_1510":{"Submissions":{"SubmissionID":"1510","SubmissionStatusID":1,"AssignedToUserID":13,"SubmissionTypeID":2,"QuestionCategoryID":5}}}}"
    
  • montoyammontoyam Posts: 568Questions: 136Answers: 5

    so this seems to work. not sure if it is the best way...

                    var questionCatID = data.data[Object.getOwnPropertyNames(data.data)].Submissions.QuestionCategoryID
                    alert(questionCatID);
    
  • rf1234rf1234 Posts: 2,801Questions: 85Answers: 406

    Hi montoyam,

    from the console log above I guess I understand what your challenge is: How do you get the content not knowing what the rowId of the row is that you are actually dealing with. If that isn't your challenge forget all of the following please :smile:

    The records I edit or create have this structure. (The row_id would be 0 for "create").

    Now I had the requirement to manipulate this stuff before it gets send to the server. This is what I did to handle both "edit" and "create":

    editor = new $.fn.dataTable.Editor( {
       ajax: {
            url: 'actions.php?action=myAction',
            data: function ( d ) {
        //make selectize work with an mjoin and single select!
                if (d.action === 'create' || d.action === 'edit') {                
                    var keys = Object.keys(d.data); //array of object keys
                    var rowId = keys[0]; //row_xxx (edit) or 0 (create)
                    if ( typeof d.data[rowId].base !== 'undefined' ) {                    
                        var baseId = d.data[rowId].base;            
                        if ( typeof baseId === 'string' || baseId instanceof String) {
                            delete d.data[rowId].base;
                            if (baseId > '0') {
                                var base = []; var element = {};
                                element.id = baseId; //set property id of element object
                                base.push(element); //put element object into base array
                                d.data[rowId].base = base; //set new property of object and put the array into it
                                d.data[rowId]["base-many-count"] = 1; //set the many count as well                           
                            } else {                            
                                d.data[rowId]["base-many-count"] = 0; //set the many count as well            
                            }
                        }
                    }
                }
            }
        },
    

    If I look at this:

    "{"data":{"row_1510":{"Submissions":{"SubmissionID":"1510","SubmissionStatusID":1,"AssignedToUserID":13,"SubmissionTypeID":2,"QuestionCategoryID":5}}}}"
    

    This should also work (in your case it should be "data.data" instead of "d.data"):

    var keys = Object.keys(d.data); //array of object keys
    var rowId = keys[0]; //row_xxx (edit) or 0 (create)
    var questionCatID = d.data[rowId].Submissions.QuestionCategoryID;
    
  • montoyammontoyam Posts: 568Questions: 136Answers: 5

    that looks like it does basically the same as the code I posted. but thanks.

    Object.getOwnPropertyNames
    
  • montoyammontoyam Posts: 568Questions: 136Answers: 5

    actually, I stumbled upon an even easier way:

    var questionCatID = SubmissionsEditor.get('Submissions.QuestionCategoryID');
    
This discussion has been closed.