Editor - serverside: How to fill a field with a value from a variable?

Editor - serverside: How to fill a field with a value from a variable?

JanNL_FRJanNL_FR Posts: 47Questions: 15Answers: 2

I am testing the duplicate example, so when I click "Create from excisting", I want to change a field value in to the value of a variable.

Is this possible?

var editor = new DataTable.Editor({
    ajax: '../php/join.php',
    fields: [
        {
            label: 'First name:',
            name: 'users.first_name'
        },
        {
            label: 'Last name:',
            name: 'users.last_name'
        },
        {
            label: 'Phone #:',
            name: 'users.phone'
        },
        {
            label: 'Site:',
            name: 'users.site',
            type: 'select'
        }
    ],
    table: '#example'
});

Jan.

This question has accepted answers - jump to:

Answers

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Answer ✓

    The field().val() or val() methods can be used to set a value for a field - e.g.:

    editor.field('users.first_name').val('Allan');
    

    That value passed in can come from anywhere, including an Ajax data fetch if you need.

    Allan

  • rf1234rf1234 Posts: 2,991Questions: 87Answers: 421
    Answer ✓

    See "set" method below. If you want to use a variable for "Santa Claus" you could use a global variable unless the variable is just a different field of the same Editor.

    const table = new DataTable('#example', {
        ajax: {
            url: '../php/join.php',
            type: 'POST'
        },
        buttons: [
            { extend: 'create', editor },
            { extend: 'edit', editor },
            {
                extend: 'selected',
                text: 'Duplicate',
                action: function (e, dt, node, config) {
                    // Start in edit mode, and then change to create
                    editor
                        .edit(table.rows({ selected: true }).indexes(), {
                            title: 'Duplicate record',
                            buttons: 'Create from existing'
                        })
                        .set({'users.first_name': "Santa Claus"})
                        .mode('create');
                }
            },
            { extend: 'remove', editor }
        ],
        columns: [
            { data: 'users.first_name' },
            { data: 'users.last_name' },
            { data: 'users.phone' },
            { data: 'sites.name' }
        ],
        dom: 'Bfrtip',
        select: true
    });
    
  • JanNL_FRJanNL_FR Posts: 47Questions: 15Answers: 2
    edited December 2023

    Thanks, I used .set to fill it with a number

    .set({'users.userid': member_id})
    

    Jan.

Sign In or Register to comment.