简体   繁体   中英

jquery jtable deleteConfirmation function not working

I am trying to use the deleteConfimation function option but I find that the default confirmation box pops up before I even get into the deleteConfimation function - what am I missing? In the code below I can set break points and watch the data object being set up correctly with its new defaultConfirmMessage, but the basic jtable default delete confirmation box has already appeared and I never see an altered one.

$(container).jtable({
    title: tablename,
    paging: true,
    pageSize: 100,
    sorting: true,
    defaultSorting: sortvar + ' ASC',
    selecting: false,
    deleteConfirmation: function(data) {
        var defaultMessage = 'This record will be deleted - along with all its assignments!<br>Are you sure?';
        if(data.record.Item) { // deleting an item
            // Check whether item is in any preset lists
            var url = 'CampingTablesData.php?action=CheckPresets&Table=items';
            $.when(
                ReturnAjax(url, {'ID':data.record.ID}, MyError)
            ).done(
                function(retdata, status) {
                    if(status=='success') {
                        if(retdata.PresetList) {
                            data.deleteConfirmMessage = 'Item is in the following lists: ' + retdata.PresetList + 'Do you still want to delete it?';
                        }
                    } else {
                        data.cancel = true;
                        data.cancelMessage = retdata.Message;
                    }
                }   
            );
        } else {
            data.deleteConfirmMessage = defaultMessage;
        }
    },
    messages: {
        addNewRecord: 'Add new',
        deleteText: deleteTxt
    },
    actions: {
        listAction: function(postData, jtParams) {
            <list action code>
        },
        createAction: function(postData) {
            <create action code>
        },
        updateAction: 'CampingTablesData.php?action=update&Table=' + tablename,
        deleteAction: 'CampingTablesData.php?action=delete&Table=' + tablename
    },
    fields: tableFields --- preset variable
});

========== After further testing the problem is only when deleting an item and it goes through the $.when().done() section of code. The Ajax call to the deletion url does not wait for this to complete - how do I overcome this?

i don't think you can get your design to work. What does the A in ajax stand for? Asynchronous! Synchronous Ajax has been deprecated for all sorts of good design and performance reasons.

You need to design you application to function asynchronously. Looking at your code, it feels you are misusing the deleteConfirmation event.

Consider changing the default deleteConfirmation message to inform the user, that the delete might not succeed if certain condition are met. Say

messages: { deleteConfirmation: "This record will be deleted - along with all its assignments, unless in a preset list. Do you wish to try to delete this record?" },

Then on the server, check the preset lists, and if not deletable, return an error message for jTable to display.

Depending on how dynamic your preset lists are, another approach might be to let the list function return an additional flag or code indicating which, if any, preset lists the item is already in, then your confirmation function can check this flag / indicator without further access to the server.

Thanks to MisterP for his observation and suggestions. I also considered his last approach but ended up setting deleteConfirmation to false (so as not to generate a system prompt) then writing a delete function that did not actually delete, but returned the information I needed to construct my own deleteConfimation message. Then a simple if confirm(myMessage) go ahead and delete with another Ajax call.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM