Editor node.js - Editing multiple columns and use value from another column to set a column?

Editor node.js - Editing multiple columns and use value from another column to set a column?

CapamaniaCapamania Posts: 233Questions: 81Answers: 5
edited June 2019 in Editor

I use .on('preEdit') ... to set a value of a column ('title'). The value I use is from another column ('name'), that I transform. It works fine if I edit one column. But if I edit multiple columns ... the same value gets inserted for all edited columns.

How can I edit and set a value from another column if I want to edit multiple columns at once? That's the part:

.fields(
    new Field( 'groups.group_id' ),
    new Field( 'groups.name' ),
    new Field( 'groups.title' )
)
.on( 'preEdit', (editor, id, values) => {

  slugifyName = slugify(values.groups.name);

  console.log(values.groups.name);
  console.log(slugifyName);

  editor
    .field( 'groups.title' )
    .setValue( slugifyName );

})

In console I see though it distinguishes between the columns?

Output Console:

ABC - Documents History
abc-documents-history

ABC - Documents
abc-documents

Answers

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    Hi @Capamania ,

    You can use field().multiSet() or multiSet() and pass unique values for each of those records,

    Cheers,

    Colin

  • CapamaniaCapamania Posts: 233Questions: 81Answers: 5

    Hi Colin,

    multiSet() is not working on serverside ... I'm getting

    TypeError: editor.field(...).multiSet is not a function

    Can you please advise on how to set multiple values on serverside?

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    Hi @Capamania ,

    multiSet() is a client-side Editor method. I'm not sure if there's an easy way to do it on the server. Does the client edit multiple records and send those details through?

    Cheers,

    Colin

  • CapamaniaCapamania Posts: 233Questions: 81Answers: 5
    edited June 2019

    I tried client side, but I'm phasing two issues. That's what I have so far:

    editor.on( 'preSubmit', function ( e, data, action ) {
        var groupRows = editor.field( 'title' ).multiGet();
        console.log(groupRows);
        $.each( groupRows, function ( id, val ) {
            var slugified = slugify(val);
            editor.field( 'path' ).multiSet( id, slugified );
            console.log(slugified);
        });
    });
    

    It works if I just edit it without any changes.

    But first issue:

    If I only select one value ... the path inserted in the database is the converted previous title value. So not the altered value.

    Second issue:

    If I create a new record ... (and I set the path column to be not empty) ... I get error message:

    {"data":[],"fieldErrors":[{"name":"path","status":"Input not valid"}]} 
    

    How can I fetch the title value and alter the patch record before it gets inserted/submitted ?

  • CapamaniaCapamania Posts: 233Questions: 81Answers: 5

    I think I got it. initSubmit https://editor.datatables.net/reference/event/initSubmit seems to solve the issues.

    editor.on( 'initSubmit', function ( e, data, action ) {
        var groupRows = editor.field( 'title' ).multiGet();
        console.log(groupRows);
        $.each( groupRows, function ( id, val ) {
            var slugified = slugify(val);
            editor.field( 'path' ).multiSet( id, slugified );
            console.log(slugified);
        });
    });
    

    Thanks.

This discussion has been closed.