preRemove - function

preRemove - function

MadMax76MadMax76 Posts: 122Questions: 26Answers: 0

HI,

I have the following function in my definition:

            editor_chat
                .on('preRemove', function (val, data, callback) {
                    if ( data.chat_pid === 3) {return true;}
                    else {return false;}
                 });

this should forbid delete for everyone apart from PID 3. At the moment this doesnt - can you see any error?

Thanks
Max

This question has accepted answers - jump to:

Answers

  • colincolin Posts: 12,635Questions: 0Answers: 2,157

    My only thought is whether chat_pid is a string, so the test should be data.chat_pid === '3'. It would be worth displaying data with console.log() to see what the values are.

    Colin

  • MadMax76MadMax76 Posts: 122Questions: 26Answers: 0

    Hi Colin,

    there gets no output into the console, but I "found" an error

    Uncaught TypeError: data is undefined

    As in preRemove there really seems to be no "data", I now tried this instead:

                    .on('preSubmit', function (e, data, action) {
                        console.log(data.chat_pid);
                        console.log(__thisPID__);
                        $.each( data.data, function ( key, values ) {
                            data.data[ key ][ 'chat_pid' ] === __thisPID__ ;
                        } );
    

    But again I get the same error.

    Thanks
    Max

  • allanallan Posts: 54,709Questions: 1Answers: 8,557 Site admin

    Hi Max,

    Is this on the client-side (i.e. preRemove)? If so, that event is not cancellable.

    If you need to cancel a delete action it either needs to be done before the data is submitted to the server (preSubmit) or you need to do it at the server-side.

    Is chat_pid === 3 referring to a row in the data, or to the logged-in user's session, or something else?

    console.log(data.chat_pid);

    Should be undefined - assuming you have a field in the Editor form called chat_pid then data.data[key].chat_pid would be the way to access it. If that is giving you an error, I'd need a link to a page showing the issue please.

    Allan

  • MadMax76MadMax76 Posts: 122Questions: 26Answers: 0

    i have the preSubmit likethis:

                editor_chat
                    .on('preSubmit', function (e, data, action) {
                        $.each(data.data, function (key, values) {
                            console.log(data.data[key]['chat_pid']);
                            console.log(3);
                                data.data[key]['chat_pid'] === 3;
                        });
    

    in the console I only get this:

    Uncaught TypeError: data is undefined
    
  • kthorngrenkthorngren Posts: 13,801Questions: 25Answers: 3,246

    I would start with adding console.log( data ); between lines 2 and 3 to see the structure of the data parameter.

    The data.data[key]['chat_pid'] === 3 statement on line 3 is a comparison statement not an assignment.

    Kevin

  • allanallan Posts: 54,709Questions: 1Answers: 8,557 Site admin

    On this example if you pop open your browser's console and enter:

    editor
        .on('preSubmit', function (e, data, action) {
            $.each(data.data, function (key, values) {
                console.log(data.data[key]['first_name']);
            });
    

    You'll get the console output the first_name value from any rows you submit.

    Do you have anything else modifying the data structure perhaps? Failing that, and Kevin's suggestion, we'd need to see a link to a page showing the issue.

    Allan

  • MadMax76MadMax76 Posts: 122Questions: 26Answers: 0

    I did set up a test case:

    http://freigabe.kontura.at/test/allan2.html

    BUT: here it also does not work, but no error is showing and nothing comes into the console?!

    Thanks
    Max

  • allanallan Posts: 54,709Questions: 1Answers: 8,557 Site admin

    I'm getting an error at that link I'm afraid:

    Allan

  • MadMax76MadMax76 Posts: 122Questions: 26Answers: 0

    Sorry we switched site when implementing ssl:
    https://portal.kontura.at/test/allan2.html

  • allanallan Posts: 54,709Questions: 1Answers: 8,557 Site admin

    Thanks! This is what I am getting in the console when I attempt to edit the first row in the first table:

    I'm not seeing the data: undefined that you described above?

    Allan

  • MadMax76MadMax76 Posts: 122Questions: 26Answers: 0

    Hi Allan,

    sorry the error is coming from another part of the function, which I eliminated in your test-page as it is calling further functions ....(and that part does work although showing an error)

    So yes there are no errors shown, and the console shows the values expected, BUT: the presubmit does not stop from deleting entries that have been made by someone else.
    I tried this

    if ( data.data[key]['chat_pid'] === 32) {return true;}
                                else {return false;}
    

    and this

    data.data[key]['chat_pid'] === 32
    

    no success...

    Thanks
    Max

  • allanallan Posts: 54,709Questions: 1Answers: 8,557 Site admin

    Hi Max,

    Apologies, I've not had a chance to look at this today. I will do so first thing tomorrow.

    Allan

  • allanallan Posts: 54,709Questions: 1Answers: 8,557 Site admin
    Answer ✓

    There are a few key issues with the preSubmit event handler used:

    1. data.chat_pid doesn't exist - it should be values.chat_pid inside the $.each, which is looping over data.data.
    2. The value will not be a number - since it is read from the DOM it will be a string so the strict equality operator on a number will fail: === 32 should be == 32 in this case or === '32'
    3. The two return statements used are inside the $.each callback function, so won't return from the `e-event preSubmit callback.

    Try this:

    editor_chat
        .on('preSubmit', function(e, data, action) {
            var valid = true;
    
            $.each(data.data, function(key, values) {
                if (value.chat_pid != 32) {
                    valid = false;
                }
            });
    
            return valid;
        });
    

    I may had inverted the logic - I wasn't 100% clear on what the logic is doing here or if the 32 is dynamic or not.

    Regards,
    Allan

  • MadMax76MadMax76 Posts: 122Questions: 26Answers: 0

    Hi Allan,

    i changed this (with valueS instead of value) to

    editor_chat
        .on('preSubmit', function(e, data, action) {
            var valid = true;
     
            $.each(data.data, function(key, values) {
                if (values.chat_pid != 32) {
                    valid = false;
                }
            });
     
            return valid;
        });
    

    and now it works!

    But I also have this code:

            editor_chat
            .on('submitSuccess', function (e, json, data, action) {
                if  (action != 'remove') {
                       ajax_call('__plugName__', 'XPruefenFreig', data.chat_empf, data.chat_stapelname, data.chat_inp_id, data.chat_pid);
                            }
            } );
    

    This code (due to the called function I can not set it up in the test-page) also works. But: On doing a delete I get the "Uncaught TypeError: data is undefined" pointing at this code, and this editor is dead afterwoods.

    I tried this

            editor_chat
            .on('submitSuccess', function (e, json, data, action) {
                if  (action != 'remove') {
                       ajax_call('__plugName__', 'XPruefenFreig', data.chat_empf, data.chat_stapelname, data.chat_inp_id, data.chat_pid);
                            }
            } );
    

    but this doesn't change anything.

    Thanks!
    Max

  • colincolin Posts: 12,635Questions: 0Answers: 2,157
    Answer ✓

    Yep, data would be undefined on a remove, but I'm surprised your test for 'remove' isn't capturing, as it's doing the trick here: http://live.datatables.net/jaxenisi/1/edit

    What is action for you when you do a deletion? It would be worth adding debug to understand why it still goes into that ajax_call.

    Colin

  • MadMax76MadMax76 Posts: 122Questions: 26Answers: 0

    Hi Colin,

    the delete isnt standard (maybe I think too complex :-1: ) but I in fact have an edit, as I only set a delete-marker....
    So I changed the condition to action == 'create'
    Now everything works :smiley:

    THANKS!
    Max

Sign In or Register to comment.