Classname set on Editor field is not passed into form
Classname set on Editor field is not passed into form
Description of problem:
I want to dynamically mark certain fields in the main Editor form. To that tend I am adding the class c9mandatory
to the relevant fields like this in preOpen
and in initCreate
if (c9.appVar.options.editorUseFormEntry) {
let mandatoryColumnNames = c9.appVar.document.chronology.getMandatoryColumnNames();
for (let i = 0, fieldName; i < c9.appVar.editor.fields().length; i++) {
fieldName = c9.appVar.editor.fields()[i];
if (mandatoryColumnNames.includes(fieldName)) {
c9.appVar.editor.field(fieldName).className = 'c9mandatory';
}
}
}
I can see the class name is added to the fields, but when the form opens there is no mention of it. I hope you will see what I mean in the following screenshot:
I tried to set up a suitable jsbin, but kept getting script errors.
This question has accepted answers - jump to:
Answers
I think you will want to use
field().node()
to add the class. Something like this:Here is a simple test case:
https://live.datatables.net/guwafemu/615/edit
Kevin
Thanks Kevin, that works a treat!
Perhaps an update to https://editor.datatables.net/reference/option/fields.className might be worthwhile.
What update are you referring to? This is meant for setting the options during initialization not dynamically later.
Kevin
I had assumed - obviously wrongly - that adding the className to the field dynamically would have an effect the next time the form was displayed. I just thought it would have been useful if it had been pointed out / emphasised that this was not the case in https://editor.datatables.net/reference/option/fields.className
The
field()
method returns an object that has a bunch of methods available - e.g.field().def()
,field().enable()
, etc. It does not properties that can be set (e.g..className
) - however, it is a Javascript object, so you can assign whatever properties you want to it:will run just fine, but similar to
.className
it won't do anything useful.field.className
is an initialisation object and I don't think anywhere in the documentation it says that you can set an initialisation object via the API in this manner.I'll have a think about how I can improve the documentation for this, but it applies to every method that returns an object - you just happened to hit a specific case. I'm inclined, at the moment, to say that since the documentation doesn't say that it can be done, then that is probably good enough (the docs can't possibly say everything that can't be done!).
Allan
Thanks Allan. I'm sure you'll find a way to stop others going off at half cock like me!