Any neat/useful custom row/column selectors out there?
Any neat/useful custom row/column selectors out there?
jLinux
Posts: 981Questions: 73Answers: 75
Working on a project at work, I needed to be able to get the Row ID's of all rows that had its child row open. I ended up looking around through the DataTables source code to see how the other selectors work.
$.fn.dataTable.ext.selector.row.push( function ( settings, opts, indexes ) {
var dtApi = new $.fn.dataTable.Api( settings ),
child = opts.child,
results = [],
nouns = {
shown : [ 'shown', 'isshown' ],
hidden : [ 'hidden', 'ishidden' ]
},
data
// If child isnt defined, then show everything
if ( child === undefined )
return indexes
// If opts.child is defined as something other than a string or boolean, then return nothing
if( [ 'string', 'boolean' ].indexOf( typeof child ) === -1 ){
console.error( 'Expected "child" row selector to be a string or a boolean value - received typeof: %s', typeof child )
return []
}
// If child is a boolean, then change it to one of the valid strings
if( typeof child === 'boolean' )
child = ( child ? 'shown' : 'hidden' )
// If its a string, convert it to lower, since everything in nounds.shown and nouns.hidden are lower
else
child = child.toLowerCase()
// Iterate over the rows, adding the rows index to the results array if the result of row( idx ).child.isShown() is whats being filtered for
for ( var i=0, ien=indexes.length ; i<ien ; i++ ) {
data = settings[ 'aoData' ][ indexes[i] ]
if( ( nouns.shown.indexOf( child ) !== -1 && dtApi.row( indexes[i] ).child.isShown() === true )
|| ( nouns.hidden.indexOf( child ) !== -1 && dtApi.row( indexes[i] ).child.isShown() === false ) )
results.push( indexes[i] )
}
return results
})
Its pretty easy to use:
// All rows with open children
api.rows({ child: true })
api.rows({ child: 'shown' })
api.rows({ child: 'isShown' })
// All rows with closed children
api.rows({ child: false })
api.rows({ child: 'hidden' })
api.rows({ child: 'isHidden' })
This got me thinkin though, does anyone else have any useful row/column/cell selectors?
This discussion has been closed.
Replies
Or, instead of the
selector-modifier
beingchild
, which may be somewhat ambiguous (hence why I created a list for the different values that can be used), maybe theselector-modifier
key should beisShown
? (which somewhat makes sense, since it usesrow().child.isShown()
.. But someone less familiar with the DT API may thinkisShown
could be referring to the row itself... not the child.Clever solution - nice one. Great to see the selector API methods being uses - as far as I am aware, you are the first other than myself to do so.
Another option for what you were trying to achieve is:
although that doesn't allow interfacing with the row selector which can be really useful for repeated use.
Regards,
Allan