Is there a prescribed approach to preventing the idSrc from being changed in editor's update dialog?

Is there a prescribed approach to preventing the idSrc from being changed in editor's update dialog?

MHPjr001MHPjr001 Posts: 7Questions: 3Answers: 0

Hi,

I realize I can mock something up that will work for me.

But, I'd much prefer to use any prescribed Datatables Editor (version 1.5) approach to doing what I feel is a common need in this area of data maintenance on a web page. Especially, if you're dealing with a "natural" key in the UI and you want to protect it from being updated by the end-user.

This works below, but it has flaws (e.g., the UI doesn't place the cursor in the first non-key field being the biggest issue):

    editor.on('onInitEdit', function () {
        // if doing an edit, then we want to only show the key not allow it to be changed.
        editor.disable('ID');

    });

Any other suggestions would be much appreciated!

Thanks,
Mike

This question has an accepted answers - jump to answer

Answers

  • MHPjr001MHPjr001 Posts: 7Questions: 3Answers: 0

    I have a fully customized way of doing things that work for me. I'm posting this custom solution just in case anyone else would need this.

    I'm still interested if Datatables staff and/or Allan would have a better way at accomplishing the same.

        // Custom change to typical Datatables editor behavior:
        //     Since we use a single Datatables "editor" instance for adding 
        //     and updating the data record, we make adjustments in both 
        //     the edit and create scenarios to ensure the key field is blocked
        //     from being changed during update and yet available to be set 
        //     during creation.
        editor.on('onOpen', function () {
            if (editor.s.action) {
                if (editor.s.action.toLowerCase() === "edit") {
                    editor.disable('ID');
                    $('body').find("#DTE_Field_FirstNonKeyField").focus();
                }
                if (editor.s.action.toLowerCase() === "create") {
                    editor.enable('ID');
                    $('body').find("#DTE_Field_ID").focus();
                }
            }
        });
    

    Mike

  • allanallan Posts: 61,863Questions: 1Answers: 10,135 Site admin
    Answer ✓

    The "DataTables staff" is just me :-).

    I think there are two options:

    1. If you want the id to be visible then you need to disable it as you are using disable(). You can then use field().focus() to set the focus or form-options which I think would be slightly better. Also an important point - I would suggest you never read anything from the .s object. That is internal to Editor and considered to be private - you won't see it documented anywhere. Also the properties in it can, will and do change between versions. Use mode() to find out want editing mode Editor is in.
    2. If you are okay with the id not being visible then use show() and hide() which will resolve the focus issue.

    The reason that this isn't more obvious in the Editor API is that setting the ID on create is something that is typically left to the database.

    Regards,
    Allan

  • MHPjr001MHPjr001 Posts: 7Questions: 3Answers: 0

    Hi Allan,

    Wow... you are the entire staff... gees - amazing!

    What you said makes sense to a degree.. but I'm not sure how to tell if it is a "create" or an "edit" without the .s object. Is there a way to do that properly with a single editor instance?

    Thanks so much for all your help!

    Mike

  • allanallan Posts: 61,863Questions: 1Answers: 10,135 Site admin

    Use the mode() API method.

    Allan

  • MHPjr001MHPjr001 Posts: 7Questions: 3Answers: 0

    Sorry I missed that from your first response - thanks for all the assistance on this one! :-)

This discussion has been closed.