how to target table in stateRestore ajax request

how to target table in stateRestore ajax request

cuspensercuspenser Posts: 16Questions: 4Answers: 0
edited May 2023 in Free community support

With the ajax: function(data, callback) from stateRestore.ajax, can you target a specific table for the request. I have 5 tables on my page and I'm trying to use $.fn.dataTable.defaults to extend the savedStates button. All 5 tables will use the same ajax script but I need to identify the table from whence the request comes from in order to process correctly in my script. I know I can duplicate the buttons code I have from the defaults section and copy that entire section over 5 times, but I'm hoping I don't need to do that, since that would require me to copy several buttons worth of code, where the only thing that needs to change between them is the tableID.

Here's the code I'm using:

{
    extend: 'savedStates',
    config: 
        {
            ajax: function (data, callback) {
            if (data.action === 'rename') {
                $.ajax({
                    type: 'POST',
                    url: 'ajaxStateSave.php',
                    data: {
                        "tableID": "table1",
                        "activeuser": "<?=$activeuser?>",
                        "action": "rename",
                        "stateRestore": JSON.parse(JSON.stringify(data.stateRestore))
                    },
                });
                callback();
            }
            else if (data.action === 'remove') {
                $.ajax({
                    type: 'POST',
                    url: 'ajaxStateSave.php',
                    data: {
                        "tableID": "table1",
                        "activeuser": "<?=$activeuser?>",
                        "action": "remove",
                        "stateRestore": JSON.parse(JSON.stringify(data.stateRestore))
                    },
            
                });
                callback();
            }
            else if (data.action === 'save') {
                $.ajax({
                    type: 'POST',
                    url: 'ajaxStateSave.php',
                    data: {
                        "tableID": "table1",
                        "activeuser": "<?=$activeuser?>",
                        "action": "save",
                        "stateRestore": JSON.parse(JSON.stringify(data.stateRestore))
                    },
                });
                callback();
            }   
        }
    }
}

Within the above code, I want to replace "table1" with the actual table ID and then send that as part of my ajax request.

Thanks for any help you can offer.

Tanner

This question has an accepted answers - jump to answer

Answers

  • colincolin Posts: 15,240Questions: 1Answers: 2,599
    Answer ✓

    That's not possible, unfortunately. You could reduce the code duplication by just adding a wrapper, something like:

    {
        extend: 'savedStates',
        config:
            {
                ajax: function (data, callback) {
                    wrapperFunction('table1', data, callback);
                }
            }
    }
    

    and then have your original code in wrapperFunction(). Not ideal, but not too much duplication,

    Colin

  • cuspensercuspenser Posts: 16Questions: 4Answers: 0

    Thanks, Colin. That will work and does cut down on code duplication quite a bit

  • manusfreedommanusfreedom Posts: 1Questions: 0Answers: 0

    That's possible:

    {
        extend: 'savedStates',
        config:
        {
            ajax: function (data, callback) {
                let category = location.pathname.split('/')[1],
                    name = this.table().node().id,
                    queryString = 'category=' + category + '&name=' + name + '&action=' + data.action + '&stateRestore=' + JSON.stringify(data.stateRestore);
                ...
            }
        }
    },
    
Sign In or Register to comment.