Trying to select all rows with visible child nodes

Trying to select all rows with visible child nodes

jLinuxjLinux Posts: 981Questions: 73Answers: 75

I have a DT instance that uses the row().child() functionality, and when the child node is visible, then a looped AJAX request is initiated, causing another XHR request every 4 seconds.

Since this can be a bit resource intensive, I wanted to enforce a limitation of only one visible child at a time. Is there any row-selector that I can use to select the row with the visible child to close it?

I know I could just keep updating a variable every time a child node is shown, just close the last one and update the variable, but I figure that this would be a better way

Answers

  • jLinuxjLinux Posts: 981Questions: 73Answers: 75
    edited October 2015

    This is my current solution, which is rather messy..

    // Format child rows with session data
    function format ( assoc_id, data ) {
        return '<table cellpadding="5" cellspacing="0" border="0" id="'+assoc_id+'" class="table session-data width-full display compact table-condensed p-t-3"><thead><tr><th>Timestamp</th><th>Other columns</th></tr></thead><tbody></tbody></table>';
    }
    
    var $sessions_table = $('#sessions-table');
    var $sessions_dt = $sessions_table.DataTable( {
        ajax: {
            url: '/REST/account/active_session_data/format/json',
            dataSrc: 'sessions'
        },
        deferRender: true,
        columns: [ /* Cols.. */ ],
        initComplete: function(){
            // Monitor changes in the parent table as well
            DT.monitor_ajax(this);
        }
    } );
    
    var active_node, $session_activity_dt;
    
    // Add event listener for opening and closing details
    $sessions_table.find('tbody').on('click', 'td.details-control', function () {
        var tr = $(this).closest('tr');
        var row = $sessions_dt.row( tr );
    
        // Check if there was previously another child node shown and its 
        // not the node of the row that was just clicked
        if(typeof active_node !== 'undefined' && active_node !== row.index()){
            var prev_row    = $sessions_table.find( 'tbody > tr' ).eq( active_node );
            var prev_dt_row = $sessions_dt.row( prev_row );
    
            // If its still shown, close it.
            if(prev_dt_row.child.isShown()) {
                $session_activity_dt.destroy();
    
                prev_dt_row.child.hide();
    
                prev_row.removeClass('shown');
            }
        }
    
    
        // CLOSE the child row (Session data)
        if ( row.child.isShown() ) {
            // Destroy the DT instance when closing
            $session_activity_dt.destroy();
    
            // This row is already open - close it
            row.child.hide();
    
            tr.removeClass('shown');
        }
        // OPEN the child row w/ session data
        else {
            var $this            = $( this );
            var $row             = $this.closest( 'tr' );
            var sess_data        = row.data();
            
            // Reset the active node
            active_node = row.index();
            
            row.child( format(sess_data.account_sessions_assoc_id, sess_data) ).show();
    
            tr.addClass('shown');
    
            $session_activity_dt = $('#'+sess_data.account_sessions_assoc_id).DataTable({
                ajax: '/REST/account/session_data/assoc_id/' + sess_data.account_sessions_assoc_id + '/format/json',
                //dataSrc: 'result',
                deferRender:    true,
                columns: [ /* Columns... */ ],
                initComplete: function(){
                    // Launch a monitor for this DT instance
                    DT.monitor_ajax(this);
                }
            });
        }
    } );
    
  • jLinuxjLinux Posts: 981Questions: 73Answers: 75

    Bump? :-D Havent found a solution yet for this, might not be one

  • jLinuxjLinux Posts: 981Questions: 73Answers: 75
    edited October 2015

    I think I found the most decent way...

    http://live.datatables.net/pecuguza/1/edit

    function get_opened_nodes(){
        return table
                .rows()
                .indexes()
                .filter( function ( value, index ) {
                    return table.row( index ).child.isShown();
                } ) || false;
    }
    

    If its not, lmk

This discussion has been closed.