Disable field based on row

Disable field based on row

MaikelMaikel Posts: 75Questions: 18Answers: 1

Hi,

i have an editor working, but for some rows it should not be possible to update a certain field.
so i wanted to disable the fields if a certain data attribute is set.

how should we do this?
i was looking at the preCreate event, but i'm not sure if its possible from within an event

Maikel

This question has an accepted answers - jump to answer

Answers

  • rf1234rf1234 Posts: 2,991Questions: 87Answers: 421
    edited May 2020
    editor
     .dependent('yourField', function ( val, data, callback ) {
        if (val == '1') {
            editor.enable( ['otherField'] );
        } else {
            editor.disable( ['otherField'] );
        }
      })
    

    It is also possible from within an event but it doesn't seem to be required, right?

    If you use "dependent" from within an event you should make sure to use "undependent" on close of the Editor to avoid having the "otherField" in a state that you don't want for other events, e.g. editing a record as opposed to creating it.

    e.g.

    editor
      .on ( 'initCreate', function ( e ) {
           this.dependent('yourField', function ( val, data, callback ) {
              if (val == '1') {
                  editor.enable( ['otherField'] );
              } else {
                  editor.disable( ['otherField'] );
              }
            })
      })
      .on ( 'close', function() {
           this.undependent('yourField');
      })
    
  • MaikelMaikel Posts: 75Questions: 18Answers: 1

    its not dependence between 2 fields, its dependency between external data and a field.

    The external data is in data-attributes on the row html tag

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

    I would make the data attribute a hidden field in Editor. Makes it a lot easier.

  • MaikelMaikel Posts: 75Questions: 18Answers: 1

    but this data is different for every row ...

  • rf1234rf1234 Posts: 2,991Questions: 87Answers: 421
    edited May 2020

    What does it look like? You can't transform it into a value range of one attribute? Like the attribute “color“ with value range green, blue, yellow, red, brown etc. different for every row of the table.
    What is the rule to decide whether or not the other field should be disabled? Can you post your use case?

  • MaikelMaikel Posts: 75Questions: 18Answers: 1

    use-case

    i have a table that defined in dom with about 20 columns, most are static columns but 5 or them are changeable by a user.
    1 column can only be changed if certain data is set for this row (i can still define where the data will be placed)

    so defining this data in the editor is not an option as this is row specific enable/disable

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

    Don't understand it. All values in Editor are row specific. Why do you think this is special? Cannot help you based on this. Sorry.

  • MaikelMaikel Posts: 75Questions: 18Answers: 1

    my table looks like:

    http://live.datatables.net/bohunafu/2/edit?html,js,console,output

    if data-enabled=yes (on the row) i want to enabled one field in the editor, if data-enabled=no then that field should be disabled

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

    if you must use data-attributes please take a look at this.
    https://editor.datatables.net/manual/templates#Inserting-fields
    I guess you will need to make changes if you want to have this in Editor.

    Alternatively you could just loop through the dom table and create a hidden td for each tr containing the "enable" / "disable" or whatever you require with jQuery and subsequently initialize the data table and editor.
    Your example has a script error and no Editor instance unfortunately.

  • MaikelMaikel Posts: 75Questions: 18Answers: 1

    i used the last sugestion, and got it working

This discussion has been closed.