Refresh page using custom button without ajax and stay on it.

Refresh page using custom button without ajax and stay on it.

rudrajitrudrajit Posts: 18Questions: 5Answers: 0

I am using a custom button which sends data to a server and then i have to manually refresh my page to show the change.
I want to to be automatic.Is there a way to refresh the page on button click without using ajax

Answers

  • kthorngrenkthorngren Posts: 21,147Questions: 26Answers: 4,918

    Since you didn't post any code or details of what you are doing I'll give a generic answer. In the success function of your custom button's ajax request you can update the table data. You can use ajax.reload() or have the server return the updated data and use row().data() to update the row or row.add() to add the row.

    If you still have questions please provide more details of what you are doing and expect.

    Kevin

  • rudrajitrudrajit Posts: 18Questions: 5Answers: 0

    ok so this is my button which send data to my servlet and i update the table .

    dom: 'Bfrtip',
              buttons: [
                  {
                    text: 'Approve',
                    action: function () {
                         var oid = $.map(table.rows({selected:true}).data(), function (item) {
                            return item[3]
                         });
                         var appby = $.map(table.rows({selected:true}).data(), function (item) {
                            return item[2]
                         });
                         var asts = $.map(table.rows({selected:true}).data(), function (item) {
                            return item[7]
                         });
                         var amt = $.map(table.rows({selected:true}).data(), function (item) {
                            return item[6]
                         });
                        
                        let flag="Approve";
                        var xhr = new XMLHttpRequest();
                        var formData = new FormData();
                        formData.set('oid', oid);
                        formData.set('appby', appby);
                        formData.set('asts', asts);
                        formData.set('amt', amt);
                        formData.set('flag', flag);
                            
                            // Set up our request
                          xhr.open( 'POST', './Ord_Details2','true' );
                        
                          
                
                        // Define what happens on successful data submission
                          xhr.onload = function () {
                        // do something to response
                            alert("Data sent"+appby+"--"+asts+"--"+flag);
                        };
                        
                          // Define what happens in case of error
                          xhr.addEventListener(' error', function( event ) {
                            alert( 'Oops! Something went wrong.' );
                          } );
                
                          // Send our FormData object; HTTP headers are set automatically
                          xhr.send( formData );
                          
                          
                    }
                }]
    

    I want to know if there is any method or any such thing which will allow me to refresh the page(if possible the datatable) after the button has been clicked.

  • kthorngrenkthorngren Posts: 21,147Questions: 26Answers: 4,918

    How are you initially loading the table data? If using ajax option you can use ajax.reload().

    Kevin

  • kthorngrenkthorngren Posts: 21,147Questions: 26Answers: 4,918

    Another option is to have the server return the updated rows then use rows().remove() to remove the selected rows followed by rows.add() to add the returned rows. Something like this:

    table.rows({selected:true}).remove();
    table.rows.add( jsonData ).draw( false );
    

    Where jsonData is the returned rows. Use draw() to update the client table with false to stay on the same page.

    Kevin

  • rudrajitrudrajit Posts: 18Questions: 5Answers: 0
    edited November 2020

    I am loading the table using JDBC. I created a list of orders and used that to get my table in jsp.
    There i linked my js file where i implemented the data-table
    I am doing my backend in java

  • kthorngrenkthorngren Posts: 21,147Questions: 26Answers: 4,918

    Ok, so my second suggestion is the one I would go with.

    Kevin

  • rudrajitrudrajit Posts: 18Questions: 5Answers: 0

    ok.
    I will look into it. Thank you

This discussion has been closed.