datatables.net-editor-server - passing a SetType to Field().set() has no effect

datatables.net-editor-server - passing a SetType to Field().set() has no effect

icdebicdeb Posts: 20Questions: 5Answers: 0
edited November 2018 in Editor

I am trying to prevent the update of one of the fields, from my understanding it is possible to do this using Field().set().

From the docs within the field.ts file:

/**
* Set the field's `set` configuration.
*
* A field can be marked as read only using this option, to be set only
* during an create or edit action or to be set during both actions. This
* provides the ability to have fields that are only set when a new row is
* created (for example a "created" time stamp).
*
* @param {(boolean|SetType)} flag Set flag.
* @returns {Field} Self for chaining.
*/

So we can pass a boolean value or a SetType, the set type I assume could be one of:

/**
 * Set types
 * @export
 */
export enum SetType {
  /** Do not set data */
  None,

  /** Write to database on both create and edit */
  Both,

  /** Write to the database only on create */
  Create,

  /** Write to the database only on edit */
  Edit
}

Seems easy enough, so I have tried:

new Field('somefield').set('Create')

But the record is written during an update. So I tried:

new Field('somefield').set({ SetType: 'Create' })

But this reacts in the same way. If I do:

new Field('somefield').set(false)

Sure enough, the record is no longer written during a create or update.

So it seems that it works when passing a boolean value but when you try to send a string value like the ones in SetType it actually treats this as Both?

How can I make this work? :-)

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 61,743Questions: 1Answers: 10,111 Site admin
    Answer ✓

    The key thing here is that SetType is an enum - not a list of strings. So:

    new Field('somefield').set('Create')
    

    is an unexpected input for the set method. Instead you would use:

    new Field('somefield').set(Field.SetType.Create)
    

    You could also use:

    new Field('somefield').set(2)
    

    since Field.SetType.Create === 2 in the above (see the TypeScript docs), but its best practice to use the enum name, just in case for some reason the enum needs to change in future (although that absolutely wouldn't be best practice on my part!).

    Allan

  • icdebicdeb Posts: 20Questions: 5Answers: 0

    Thanks, that makes perfect sense now.

This discussion has been closed.