Don't display an Editor field, if data is null.
Don't display an Editor field, if data is null.
Hi all. I have an internal website, using Editor. When I click an "Edit" button, I would like for one of the fields to not appear on the Editor form, if the data is null. I have tried multiple code variations with the "render" option and have had no luck. I have also messed with the dependent fields option. Example code snippet is below. I would like Field 4 to not display at all, on the form, if tablename.columnname4 is null, for the given record. Some file, table, and field names have been changed, to protect the innocent. Help!
var editor = new $.fn.dataTable.Editor( {
ajax: "somefile.php",
table: "table.table",
fields: [
{
"label": "Field1:",
"name": "tablename.columnname1",
"type": "readonly"
},
{
"label": "Field2:",
"name": "tablename.columnname2"
},
{
"label": "Field3:",
"name": "tablename.columnname3"
},
{
"label": "Field4",
"name": "tablename.columnname4",
"type": "ckeditor",
'className': 'full block'
},
{
"label": "Field5:",
"name": "tablename.columnname5"
}
]
} );
Replies
As you mentioned
dependent()
is how to show/hide fields based on a condition. See the third example in the docs.Possibly something like this?
You may need to debug the if statement to see if
val
is an empty string or null or something else if null. Here is a simple example:https://live.datatables.net/guwafemu/440/edit
Cedric Kelly has an empty position field which is hidden when editing that row.
Kevin
@kthorngren, I tried almost this exact piece of code earlier with no errors, but also with no change in behavior. I suspect there is something to what you mentioned about debugging val for something other than empty or null. Thank you. I'll start digging.
@kthorngren, any tips on that debugging val? I can't seem to get console.log to display anything.
Something like this for console output:
https://live.datatables.net/guwafemu/442/edit
Or put a debugger breakpoint on the
return
statement.Kevin
@andy_barber Of you could post your code, I can take a look.
Allan
@allan
Basically, if there are no validation steps entered, I don't want that field to show up at all.
What does the console.log statement show when there are no validation steps? Paste the output here.
Kevin
On a whim, I replaced tbl_validation_form.list_steps with tbl_validation_form.id, another valid field. All of a sudden, val starts showing up in the console.log. That said, I would have expected the tbl_validation_form.id to not be there, and it was. I feel like there are two separate issues going on here: 1) something that is preventing the dependent field code from working and 2) something with the tbl_validation_form.list_steps field, specifically, that causes nothing to display in the log, despite the console.log code being correct.
Console.log output when tbl_validation_form.id is used in the dependent field code:
[Violation] 'setTimeout' handler took 145ms
jquery-3.7.0.min.js:2 [Violation] 'load' handler took 2244ms
drts-validation-form.js:210 val: "233"
dataTables.editor.js:4958 [Violation] 'setTimeout' handler took 268ms
dataTables.editor.js:4958 [Violation] 'setTimeout' handler took 249ms
[Violation] Forced reflow while executing JavaScript took 68ms
9[Violation] Avoid using document.write().
ckeditor.js:260 [Violation] 'setTimeout' handler took 442ms
[Violation] Forced reflow while executing JavaScript took 82ms
Console.log output when tbl_validation_form.list_steps is used in the dependent field code:
5[Violation] 'setTimeout' handler took <N>ms
jquery-3.7.0.min.js:2 [Violation] 'load' handler took 2402ms
[Violation] Forced reflow while executing JavaScript took 68ms
9[Violation] Avoid using document.write().
[Violation] Forced reflow while executing JavaScript took 68ms
The console.log code is unchanged in each example.
console.log('val: "' + val + '"');
@kthorngren & @allen, I believe it is the MySQL field type that is preventing the console.log message from appearing. That doesn't appear to be the reason the dependent field code isn't working, though.
When I choose a "varchar" type field, like tbl_validation_form.id, it displays in the console.log. When I choose a "mediumtext" type field, like tbl_validation_form.list_steps, it doesn't display.
Do all those other messages appear at the same time? If so it seems there is something else that might be wrong. Do they appear if you remove the console.log statement?
I would set a browser debugger breakpoint on the
return
statement. This will allow you to see theval
value and the values of the other variables within the scope. Post a screenshot of what you see.Kevin
@kthorngren, I had things set for "verbose." I have now turned it off. When I turn it off, those messages go away from the console. They seem to have more to do with settings I have chosen, like turning off paging and things like that. I'll try to figure out how to set a break point. Full disclosure, I am not a full-time developer. I have to learn much of this on the fly.
If the console.log outputs with
tbl_validation_form.id
but nottbl_validation_form.list_steps
then that suggeststbl_validation_form.list_steps
does not match thefield.name
you have defined so the event handler is not executing. Make sure it matches and the character case is correct. Post your Editor code without renaming the fields.Each browser has different steps but if using Chrome go to View > Developer Tools. In the tools select
Sources
then open your file. Find thereturn
statement and click on the line number to the left. This is the break point. Edit a row and browser will stop when it hits that statement, like this:Kevin
Full page code:
@kthorngren, if I set a breakpoint on line 203, the debugger pauses appropriately. If I set one on line 210, the debugger does not pause.
Interesting. I'm not sure why but it seems
dependent()
doesn't fire forckeditor
fields. @allan can confirm why this is the case. As a workaround you can use thetbl_validation_form.id
then use thedata
parameter to check the value oftbl_validation_form.list_steps
. Something like this:See this example:
https://live.datatables.net/lulomejo/1/edit
Edit
Bradley Greer
. You will see theeditor.dependent( 'office', function ( val, data, callback, e ) {...});
event doesn't fire asoffice
is a ckeditor field. But theeditor.dependent( 'name', function ( val, data, callback, e ) {..});)
event does. It checks th office value withdata.rows[0].office
.Kevin
@kthorngren, this worked. Thank you so much! I'll check back for Allen's response.
I wonder if CKEditor isn't triggering a change event, which is what
dependent()
listens for by default. I'll look into that.Allan