Create with some defaults

Create with some defaults

greggreggreggreggreggreg Posts: 42Questions: 19Answers: 2
edited January 2020 in Buttons

I am trying to do a create with some editing of the field from the same editor that gets used for a Edit. Should this be possible? Pressing my button results in nothing happening.

buttons: [
                                        {
                                            extend: "create",
                                            text: 'New Entry',
                                            action: function ( e, dt, node, config ) {
                                                editor.field( 'field_one' ).hide();
                                                var randomstring = "abc";
                                                editor.field( 'field_one' ).val(randomstring);
                                                editor.field( 'status' ).val('New');
                                            }
                                        },

Also, is it just me or does code formatting not work sometimes and the code button sometimes trim CR.

Edited by Colin - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.

This question has an accepted answers - jump to answer

Answers

  • rf1234rf1234 Posts: 2,988Questions: 87Answers: 421
    edited January 2020 Answer ✓

    I wouldn't use the code button but triple back ticks in order to use Markdown - see below. If you add "js" or "php" or whatever after the initial back ticks it gets even better.

    When you do what you do in your code you are extending "create" but you are also completely overwriting the "action" parameter. Hence your button does nothing.

    This button does what a "create" button does plus your stuff:

    buttons: [
        //{   extend: "create", editor: rateEditor, className: "lgfAdminOnly" },
        {
            extend: "create",
            text: 'New Entry',
            action: function ( e, dt, node, config ) {
                rateEditor
                    .title($.fn.dataTable.Editor.defaults.i18n.create.title)
                    .buttons({
                            label: $.fn.dataTable.Editor.defaults.i18n.create.submit,
                            className: 'btn-showall-color',
                            fn: function () {
                                this.submit();
                            }
                        })
                    .create()
                    rateEditor.field( 'rate.currency' ).hide();
                    var randomstring = "abc";
                    rateEditor.field( 'rate.currency' ).val(randomstring);
                    rateEditor.field( 'rate.rate' ).val('New');
            }
        },
    

    Better not extend the button but use an "on open", "on initCreate" or other events and manipulate your editor values. Like in here for example which is a very special use case of "dependent" for "create" only.

    editor
        .on('initCreate', function () {
            this.dependent('fixed.first_payment_date', function (val, data, callback) {
                editor.set( 'fixed.following_payment_day', val.substr(0,2) );
            })           
        })
        .on('close', function () {
            //kill "dependent" event listener on first_payment_date 
            this.undependent('fixed.first_payment_date');
        })
    
  • greggreggreggreggreggreg Posts: 42Questions: 19Answers: 2

    that was perfect thanks.

This discussion has been closed.