If statement to determine bulk vs single edit

If statement to determine bulk vs single edit

ggerbushggerbush Posts: 7Questions: 3Answers: 0

I have one endpoint that handles editing a single row and another that handles bulk edits. I am trying to write a function to determine which url to pass to the editor for the edit/PUT method, but I cannot find a way to determine whether more than one row was edited. Is there a way to do this?

This question has accepted answers - jump to:

Answers

  • rf1234rf1234 Posts: 2,809Questions: 85Answers: 406
    Answer ✓

    You might want to count the number of selected rows. If it is more than one you could conclude that it is bulk editing.

    Here is an example how to count the number of selected rows:
    https://datatables.net/reference/api/count()

    You could use this in an event, e.g. on preEdit and put your if statement in there.
    https://editor.datatables.net/reference/event/preEdit

  • allanallan Posts: 61,938Questions: 1Answers: 10,157 Site admin
    Answer ✓

    Interesting one. Perhaps one option would be to have a unified end point at the server, which effectively acts as a proxy for the other two. It could do a count on the submitted data to check if it is a single or multiple row edit, and then send it on to the correct place.

    I think on the client-side, you'd need to use ajax as a function to send it to different URLs, making your own Ajax call as needed.

    Regards,
    Allan

  • ggerbushggerbush Posts: 7Questions: 3Answers: 0

    Thank you, both, for the help!

    @rf1234 I just realized that it is possible to select several rows, but whiles using keys to navigate, edit only one row. Is it possible to just get a count of the rows being edited?

    @allan I am leaning towards this, but I would rather just have an if statement in the javascript directing bulk edits to one endpoint and a single edit to another.

  • rf1234rf1234 Posts: 2,809Questions: 85Answers: 406
    Answer ✓

    "Is it possible to just get a count of the rows being edited?"
    Not through a command I think. But of course you can analyze what is being sent to the server and count it yourself.

    Here is an example where I analyze and manipulate what is being sent to the server myself:

    var contractEditor = new $.fn.dataTable.Editor( {
       ajax: {
            url: 'actions.php?action=tblContractEntryGov',
            data: function ( d ) {
                var selected = contractGovDeptTable.row( {selected: true} ); 
                if (selected.any()) {
                    d.govdept_id = selected.data().govdept_has_user.govdept_id;
                    d.gov_id = selected.data().govdept.gov_id;
                    var role = selected.data().govdept_has_user.role;
                    d = determineApproverEditor(d, role);
                }
        //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            
                            }
                        }
                    }
                }
            }
        },
        table: "#tblContractGov",
    fields: [ { .....
    

    As you can see it is easy to check whether the user action is "edit" and then play around with "d.data" and all the other nice objects in there ...

  • ggerbushggerbush Posts: 7Questions: 3Answers: 0

    Thank you @rf1234 !

This discussion has been closed.