Editor insert only "datatable" field

Editor insert only "datatable" field

unibocconi-ltccunibocconi-ltcc Posts: 14Questions: 4Answers: 0
edited August 2023 in Free community support

Hi,
I have a "datatable" field that I want to make it configurable only on insert.

On the server side I've already blocked the "update" action with ->set(Field::SET_CREATE), but I was wondering if there is a way also to prevent the change on the frontend.

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,689Questions: 1Answers: 10,500 Site admin
    Answer ✓

    Yes, similar to the other field types, datatable can be enabled and disabled (field().enable() and field().disable()).

    Combine those two method with the initCreate and initEdit events and you can enable and disable the field as needed - e.g.:

    editor.on('initCreate', function () {
      editor.field('myDataTable').enable();
    });
    
    editor.on('initEdit', function () {
      editor.field('myDataTable').disable();
    });
    

    Allan

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

    This example from this thread is doing something similar to what you want. It's using field().enable() to make the age field editable on creation, but read-only afterwards.

    Is that what you were after? Or did you want the field to not appear on the form? If so, you could use field().hide() instead for that.

    Colin

  • unibocconi-ltccunibocconi-ltcc Posts: 14Questions: 4Answers: 0

    Thank you all for the suggestions and hints!

    The type "datatable", when disabled, can however be searched and the pagination is active. I found this confusing.

    So I came up with the approach of creating a new field and then show it an populate it when needed:

        const myField = 'MY_FIELD';
        editor.add({label: myField + ':', name: myField + '_READONLY', type: "readonly", def: ""}, null);
    
        editor.on('initCreate', function () {
            editor.enable(myField);
            editor.show(myField);
            editor.hide(myField + '_READONLY');
        });
    
        editor.on('initEdit', function(e, node, data, items, type) {
            editor.disable(myField);
            editor.hide(myField);
            editor.set(myField + '_READONLY', data[myField]);
            editor.show(myField + '_READONLY');
        });
    
  • allanallan Posts: 63,689Questions: 1Answers: 10,500 Site admin

    Sounds like a nice solution :)

    Allan

Sign In or Register to comment.