Any neat/useful custom row/column selectors out there?

Any neat/useful custom row/column selectors out there?

jLinuxjLinux 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' })

Heres the JS bin instance

This got me thinkin though, does anyone else have any useful row/column/cell selectors?

Replies

  • jLinuxjLinux Posts: 981Questions: 73Answers: 75

    Or, instead of the selector-modifier being child, which may be somewhat ambiguous (hence why I created a list for the different values that can be used), maybe the selector-modifier key should be isShown? (which somewhat makes sense, since it uses row().child.isShown().. But someone less familiar with the DT API may think isShown could be referring to the row itself... not the child.

  • allanallan Posts: 63,812Questions: 1Answers: 10,516 Site admin

    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:

    var indexes = table.rows().every( function () {
      return this.child.isShown() ? this.index() : null;
    } ).filter( function ( val ) {
      return val !== null;
    } );
    

    although that doesn't allow interfacing with the row selector which can be really useful for repeated use.

    Regards,
    Allan

This discussion has been closed.