preRemove User Side Notification

preRemove User Side Notification

Restful Web ServicesRestful Web Services Posts: 202Questions: 49Answers: 2

With this very basic example of validating whether or not a row can be deleted how do I present a notification to the user on the client side if the row cannot be removed?

                ->on( 'preRemove', function ( $editor, $id, $values ) {
                    if($values['t_emailtemplate']['emailtemplate_id'] == 31){
                        return false;
                    }
                })    

I can see the JSON response is,

{"data":[],"cancelled":["row_31"],"debug":[]}

Is it possible to use this reponse to trigger a notification to the user, i.e. Row 31 could not be removed.

Thanks

Answers

  • allanallan Posts: 61,892Questions: 1Answers: 10,144 Site admin

    At the moment you need to use either a simple alert() or a third party notification library. Listen for the submitComplete event and check to see if there are any cancelled rows in the returned JSON. Then you can show the notification.

    Allan

  • Restful Web ServicesRestful Web Services Posts: 202Questions: 49Answers: 2

    Hi Allan,

    Thanks for the response. In the end I have decided to prevent the delete rather than deal with any responses.

    In this simple example I have an mJoin table containing bookings linked to the parent client table. If a booking exists the client cannot be deleted so I simply sum the length of the joined records and if the result is greater than 0 then I trigger a notification to the user rather than allow the delete.

                table.on( 'select deselect', function () {
                    var bookings = table.rows( { selected: true } ).data().pluck('t_booking').pluck('length').sum();                                 
                    if( bookings > 0 ){
                        table.button( 2 ).disable();
                        $(".buttons-remove").click(function(){
                            table.buttons.info( 'Notification', 'Sorry, but the selected client(s) cannot be deleted', 3000 );
                        });
                    }else{
                        table.button( 2 ).action();  
                        $(".buttons-remove").click(function(){
                            $('#datatables_buttons_info').hide();
                        });                    
                    }             
                });   
    

    Chris

  • Restful Web ServicesRestful Web Services Posts: 202Questions: 49Answers: 2

    For anyone interested I have changed the approach slightly to the below as this easily allows me to potentially check various separate conditions which could prevent the delete from being allowed. It also allows me to include the specific identifiers of the items blocking the delete which will be useful to the user.

                    var errors = [];
                    var rows = table.rows( { selected: true } );
                    rows.every( function ( rowIdx, tableLoop, rowLoop ) {
                        var data = this.data();
                        if( data['t_booking']['length'] > 0 ){
                            errors.push(" '" + data['t_clientowner']['iClientOwnerId'] + "'");
                        }
                    });  
                    if( errors.length > 0 ){
                        table.button( 2 ).disable();
                        $(".buttons-remove").click(function(){
                            table.buttons.info( 'Notification', 'Sorry, the following client(s) ' + errors + ' cannot be deleted.', 5000 );
                        });
                    }else{  
                        $(".buttons-remove").click(function(){ $('#datatables_buttons_info').hide(); });                    
                    }  
    

    I have tried to write the code as succinctly and in a reusable way but any suggestions welcome!

This discussion has been closed.