Feature Request - Events for row().child.show() and row().child.hide()

Feature Request - Events for row().child.show() and row().child.hide()

jLinuxjLinux Posts: 981Questions: 73Answers: 75

I was looking through the DT reference documents, and I notice theres some API methods for interacting with child rows (row().child.show(), row().child.hide(), row().child.isShown(), etc), but there aren't any events related to the child rows. Im writing a plugin that needs to be able to launch custom functions whenever child rows were hidden or shown, but I'm not quite sure how to go about doing that now.

Just a thought :)

If theres a way I can have functions execute when child rows are shown/hidden, without having to know anything that may be specific to unique datatables instances, that would work for now

Thanks!

Replies

  • allanallan Posts: 63,352Questions: 1Answers: 10,444 Site admin

    DataTables itself doesn't trigger these methods, it just provides them, which is why it doesn't trigger any events for them (i.e. you know when you trigger them). You could potentially trigger your own event when you call those methods if you which to decouple the code that handles that event.

    Having said that, this isn't the first time this has been brought up. As I try to improve the events for the next major version it is something I will look into.

    Regards,
    Allan

  • jLinuxjLinux Posts: 981Questions: 73Answers: 75

    You could potentially trigger your own event when you call those methods if you which to decouple the code that handles that event.

    This is actually a route I'm thinking about taking. After looking at the code for some time, it looks like the main functions I would need to copy are _fnCallbackFire, and _fnCallbackReg, and any other functions they're dependent on.

    I don't really mind having to write/register/trigger my own callback functions and events, and I think the best way/time to fire the events for opening/closing the child row, would be right inside these API methods:

    What my original plan was to override the API methods with a custom method that would execute the callback, then execute the "original" version of that API method, so something like this:

    // Override the existing row().child().hide() API method to inject some 
    // of my own logic right before the requested row gets hidden
    $.fn.dataTable.Api.register( 'row().child().hide()', function () {
        // Execute the row-child-hide.dt event..
        _fnCallbackFire( oSettings, null, 'row-child-hide', [oSettings] )
    
        // Then how do I execute the original row().child().hide()? Thats what im stuck on now
        //row().child().hide()
    } )
    

    @allan, I think this is something we may have talked about a while ago, or at least very similar question, where I was trying to "inject" some of my own logic into an API method, and I dont think either of us found a solution back then either.

    If you know of a way I can accomplish this, then that would be awesome! thanks!

  • allanallan Posts: 63,352Questions: 1Answers: 10,444 Site admin
    edited August 2016

    I would suggest you just call $('#myTable').trigger('myEvent');. edit After you call the row().child().hide() method.

    Allan

  • jLinuxjLinux Posts: 981Questions: 73Answers: 75

    @allan, Im also trying to add another feature to my Keep-Conditions plugin, to keep child rows open if they were opened when the URL was copied. The only way I know how to do that is to listen for an event when child rows are opened/closed.

    For example look at like 1164 in the section for the colReorder feature.

    Sooo... any way you can add some events for when child cols are opened/closed? I would think its simple enough to do so... I would do it myself, but then anyone who implemented the plugin would have your version of row().child.hide() and row().child.show() overridden (and probably even row().child.remove()), which I hate doing, as I would have to make sure to keep them up to date

  • allanallan Posts: 63,352Questions: 1Answers: 10,444 Site admin

    The approach that Select takes for this is to listen for the preXhr event, gather the details of the selected row, then when the reload happens, reselect them. It might be possible to do the same for your use case, just with child rows rather than selected rows.

    The child events are likely to be added in future, but probably not until the next major release.

    Regards,
    Allan

This discussion has been closed.