Redirect after adding a row?

Redirect after adding a row?

T RayT Ray Posts: 4Questions: 1Answers: 0
edited December 4 in Free community support

After adding a row to the table with row.add(), what is a good way to trigger redirecting to another page?

I don’t think createdRow is what I need, as that is for all rows, not just the added one.

Similar question here:
https://datatables.net/forums/discussion/30233/redirect-to-a-new-page-after-insert-a-new-row-passing-values

I see it may be possible with MutationObserver, but I’d like to see if there is a better way.

Thanks for any guideance!

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 7,010Questions: 22Answers: 1,529

    Unless I'm missing something Datatables does not need to be involved in this. After you use row.add() then using something like window.location.replace to redirect to the desired page.

    Kevin

  • T RayT Ray Posts: 4Questions: 1Answers: 0

    Yes but what can I use to trigger that? The row is added via save function, not my function.

  • kthorngrenkthorngren Posts: 7,010Questions: 22Answers: 1,529
    Answer ✓

    There are a list of Datatables events here. The draw event would be an option as the draw() API is typically used with row.add(). However draw triggers for other events like sorting and searching. So there would need to be a way to determine if a new row was added.

    How is this Save function used? Is it a Datatables Editor function? Maybe there is a trigger from it that can be used.

    Do you need to get the data of the new row?

    I've never used a MutationObserver but in looking at the docs it triggers when there is a DOM event. Using row.add() might not change the DOM if the new row is not displayed on the page. But I can't ay for sure.

    Kevin

  • T RayT Ray Posts: 4Questions: 1Answers: 0
    edited December 4

    Oh thanks much!! I’m sure I can make one of those work. I missed the events page in all my searches...
    https://datatables.net/reference/event/
    https://editor.datatables.net/reference/event/
    Will share solution once complete.

  • T RayT Ray Posts: 4Questions: 1Answers: 0
    edited December 5

    I ended up using the editor postCreate event.

    $(document).ready(function () {
    editor = ...
    
    editor.on('postCreate', function (e, json, data) {
            if (!isNaN(data['myTable']['ID'])){
                window.location.href = 'step2.aspx?ID=' + data['myTable']['ID'];
            } else {
                console.error('Error - no ID found when creating row.  Data: ' + JSON.stringify(data));
            }
        });
    ...
    });
    

    Not sure if above is the best way to access the ID data, but it works!

Sign In or Register to comment.