Is DataTables.Api a row-selector?
Is DataTables.Api a row-selector?
I'm confused about one of the examples for edit
. This code works, but I don't understand why.
// Edit all selected rows in a DataTable:
editor.edit(
table.rows( { selected: true } ).indexes()
);
The example calls rows().indexes()
and passes its return value of type DataTables.Api
to edit()
. I'm confused because this API operates on a row-selector
argument and the documentation for that does not list DataTables.Api
as a valid argument. The documentation does mention that an array of integers is a valid argument, but DataTables.Api
instances are only array-like objects instead of real arrays.
Should I assume that any DataTables.Api
instance can be a valid row-selector
as long as its items are themselves valid row-selector
or is there something else going on?
This question has an accepted answers - jump to answer
Answers
In this particular case, yes that is a safe assumption. Basically what is happening is that the API instance return from your
rows().indexes()
call is being used as a row selector by Editor, passing intorows()
. Because it looks like an array of indexes, that is how it is treated.I'll update the documentation for that. Good point.
Allan
Oh so can't I just pass the return value of
rows()
as the first argument and eliminate the conversion from rows to indexes to rows?Or else, can I pass any instance of
selector-modifier
toedit()
and let theedit()
API find the rows?Yes, you can do that, although it isn't explicitly documented.
You can't do that though since it isn't an API instance. If it works, its unexpected!
Allan
You will be surprised by the results of my experiment.
See this example with three different ways of calling
edit()
http://live.datatables.net/zivilifa/4/edit?js,output
rows(selector-modifier).indexes()
rows(selector-modifier)
selector-modifier
Select a single row and try all the buttons.
Button 1 works, button 2 works, button 3 unexpectedly works.
Change the selection to multiple rows and retry all the buttons.
Button 1 still works, button 2 stopped working, button 3 unexpectedly still works.
Slightly related (sorry if this should be a different thread).
There's a number of APIs with a similar pattern where the documentation contradicts the actual behavior. I'm talking about (almost) every API that takes some kind of selector and an optional selector modifier.
Example
function row( rowSelector [, modifier ] )
https://datatables.net/reference/api/row()
Here, the
rowSelector
part is documented as required but it is not actually needed becauseundefined
is a valid instance ofrow-selector
.Per the documentation, if you want to only use a modifier, you'd have to explicitly pass in
undefined
for the selector.The surprising, undocumented behavior is that you can skip the selector.
I think this is true for
row()
,column()
andcell()
.The point that I'm trying to make is that a
selector-modifier
seems to be a valid argument to any method that requires a selector of some sort as the first argument, such asedit(row-selector)
.It is yes - this is explicitly documented for the plural selectors - e.g
rows()
.I didn't document it for the singular methods since I wasn't really expecting it to be used there and thought it might be confusing (e.g.
table.row( { page: 'current' } )
doesn't make much sense, but it is valid - it will just truncate the result to 1 row).That perhaps changed when I introduced the selector modifier extensions so things like
selected: true
could be used where it is valid to have only a single row selected and it makes sense to userow()
there.I'll update the documentation with a note about this. Thanks for pointing out that discrepancy.
Allan
It's starting to click in my mind now. Do the singular methods follow the same code path as the plural methods and then truncate the result set to a single item? Or do the singular methods have separate code paths and it's all just very similar code?
Yup - same code path. This is where it runs the selector as normal and then truncates as required.
Allan
Okay thanks for the help. With this information, I can finally fix the TypeScript definition files over at DefinitelyTyped.
https://github.com/StevenLiekens/DefinitelyTyped/commits/improve-datatables.net/types/datatables.net
Brilliant! I'll see if I can contribute as well.
Allan