Is there a way to put a dynamic message in the datatables editor?

Is there a way to put a dynamic message in the datatables editor?

Stacey1134Stacey1134 Posts: 47Questions: 6Answers: 0

The following pieces : field().message() field().fieldInfo() field().labelInfo() only accept string types. Is there something like fields.def which accepts a function?

    $(document).ready(function() { 
        editor1 = new $.fn.dataTable.Editor( {
            
            ajax: "ajaxCorrespondence.php",
            table: "#correspondenceTable",
            display: 'bootstrap',      
            fields: [ {       
                    label: "Contact ID:",
                    name:  "cr_id",
                    def: function () {
                        var gotID = $('#myClipboard').attr('data-crid');
                        return gotID;
                    },
                    message: function () {
                        var gotName = $('#myClipboard').attr('data-fname');
                        return gotName;
                    },
                }, {

message won't work, and nothing else. I need add dynamic information that the user needs. I am actually combining variables when creating this info from elsewhere in the web app. Thanks.

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 18,215Questions: 25Answers: 4,322

    I haven't used these APIs but it looks like field().fieldInfo(), field().labelInfo() and field().message() support functions. The corresponding options for these, like in your code snippet only takes a string. It looks like you can use open event with the API's to dynamically set the values.

    Kevin

  • Stacey1134Stacey1134 Posts: 47Questions: 6Answers: 0

    Thanks, in theory it could work. But I cant even get an the open event to work as standard. I copied it directly from here : https://editor.datatables.net/reference/api/open()

            $('#newRecord').on( 'click', function () {
                editor1
                    .create()
                    .title( 'Create new record' )
                    .buttons( 'Create' )
                    .open();
            } );
    

    even in its most basic form won't activate, I am using the buton function , and these have no names I could find no id or names so I cannot catch the event.

            // Display the buttons
            new $.fn.dataTable.Buttons( table, [
                { extend: "create", editor: editor1, formTitle: 'Scan New Correspondence' },
                { extend: "edit",   editor: editor2, formTitle: 'Change Existing Scanned Correspondence'  },
                { extend: "remove", editor: editor2 },
                {
                    text: 'Contact Lookup',
                    action: function ( e, dt, node, conf ) {
                        $('#contactLookup').modal('show'); 
                    }
                }
    
            ] );
        
            table.buttons().container()
                .appendTo( $('.col-md-6:eq(0)', table.table().container() ) );
        } );
    

    I tried this:

    editor1.field( 'scanned_mail.cr_id' ).fieldInfo( 'Your user name should be at least six characters long and can contain any set of characters.' );
    

    even inside the one event handler that I can kick off

            editor1.on('open', function(e, mode, action) {
               $('div.modal-dialog', editor1.displayNode()).addClass('modal-xl');
            });  
    

    As well as every other variation I could try. I had tried before that's why I tried to go back to fields. But tried again after your post. Still no luck.

  • kthorngrenkthorngren Posts: 18,215Questions: 25Answers: 4,322
    Answer ✓

    Here is an example:
    https://live.datatables.net/guwafemu/367/edit

    Using the standard editor Create and Edit buttons or the custom button, newRecord, is working. If this doesn't help then please update the test case to show what you are tying to do so we can help debug.

    Kevin

  • Stacey1134Stacey1134 Posts: 47Questions: 6Answers: 0

    Oh my goodness. Somehow the message got crossed. I am not trying to create new buttons, I am trying to use the system as designed. There is not test case because there isn't a bug.

    Thanks for trying, but this does not at all answer the question I posed, and unfortunately the solution posted here https://live.datatables.net/guwafemu/367/edit, require creating my own buttons. I am actually trying to use editor. I can easily make a bootstrap modal with my own buttons and text inside the modal. I am asking how to do it within the Editor framework.

    Thanks anyway.

  • Stacey1134Stacey1134 Posts: 47Questions: 6Answers: 0

    Scratch that, Thanks @kthorngren I totally missed this part in your example:

            editor1.on('open', function(e, mode, action) {
               $('div.modal-dialog', editor1.displayNode()).addClass('modal-xl');
               editor1.field( 'scanned_mail.cr_id' ).message( function () {
                    return 'Message: ' + Math.floor(Math.random() * 100);
                });           
            }); 
    

    Well, a little different after I was done, but this work great. Thanks!

  • kthorngrenkthorngren Posts: 18,215Questions: 25Answers: 4,322

    The example shows two options. One using the Editor's buttons and the other using a button with the id of newRecord. I assumed you wanted the second option based on this code snippet:

    $('#newRecord').on( 'click', function () {
        editor1
            .create()
            .title( 'Create new record' )
            .buttons( 'Create' )
            .open();
    } );
    

    I thought your questions where how to dynamically set field().message() field().fieldInfo() field().labelInfo() values. This is demonstrated in the example with this code:

      editor.on( 'open', function ( e, mode, action ) {
        editor.field( 'name' ).message( function () {
          return 'Message: ' + Math.floor(Math.random() * 100);
        });
        editor.field( 'name' ).fieldInfo( function () {
          return 'Field Info: ' + Math.floor(Math.random() * 100);
        });
        editor.field( 'name' ).labelInfo( function () {
          return 'Field Label: ' + Math.floor(Math.random() * 100);
        });
      } );
    

    Its likely I misunderstand your questions. If this doesn't help then my suggestions is to update the example with your solution. This way we can see exactly what you are trying to do to offer suggestions.

    Kevin

  • kthorngrenkthorngren Posts: 18,215Questions: 25Answers: 4,322

    Looks like we cross posted :smile:

    Kevin

Sign In or Register to comment.