Can parent datatable detect if the child datatable is empty?

Can parent datatable detect if the child datatable is empty?

dt_userdt_user Posts: 51Questions: 16Answers: 0

Hi Good morning,
I am getting information from a database where the result set can be empty in the child datatable (The parent row gets information from a different ajax source). When the page loads I would like the details-control to be removed from the parent row to reflect the empty child datatable.

Please let me know if the above question is possible?
Thank you.

This question has an accepted answers - jump to answer

Answers

  • tangerinetangerine Posts: 3,350Questions: 37Answers: 394

    You already asked this question, and received answers.
    https://datatables.net/forums/discussion/57153
    Did you try those solutions?

  • dt_userdt_user Posts: 51Questions: 16Answers: 0

    I would like my program to run like this example: http://live.datatables.net/pifugoki/35/edit. Without having to click on the button. My program is not user friendly because persons will click the button thinking that their is something else for them to see.
    This is why I need either the parent row to know if the child datatable(has a different data source) is empty OR I need the child datatable to be able to tell the parent row its empty so remove the details-control class. OR I need to know if there is a way for the child datatable to remove the details-control class in the parent row?
    Please let me know if the questions above have any answers.
    Thank you.

  • kthorngrenkthorngren Posts: 20,302Questions: 26Answers: 4,769

    Its not clear if you still have a question but, I think, that example is doing what you want. You will need to code the createdRow callback to examine your child data to determine if the detail-control should be displayed:

              createdRow: function ( row, data, index ) {
                if (data.extn === '') {
                  var td = $(row).find("td:first");
                  td.removeClass( 'details-control' );
                }
               },
    

    This is dependent on your specific data.

    Kevin

  • dt_userdt_user Posts: 51Questions: 16Answers: 0

    Ok I placed the above code in my parent and child datatable but the class doesn't get removed.
    Link to my code: http://live.datatables.net/jaziqiya/18/

  • kthorngrenkthorngren Posts: 20,302Questions: 26Answers: 4,769
    edited July 2019

    The createdRow is to be in your parent table so it will run when Databales initially creates the row. Placing it in the child Datatable won't work because it doesn't run until the child is opened which is too late.

    Note that when the row is created the data for the child rows needs to be accessible so a check can be made to determine if the details-control should be shown. Typically the child data will be apart of the original row data returned in the ajax response.

    Kevin

  • dt_userdt_user Posts: 51Questions: 16Answers: 0

    Ok understood. In my program the child datatable does not have data that was apart of the original row data. The parent and child get data from two different ajax sources. This is why I am having problems removing the details-control class if the child datatable is empty.
    Is there a way I can let the parent know that the child datatable has no data?

  • kthorngrenkthorngren Posts: 20,302Questions: 26Answers: 4,769
    Answer ✓

    If the data is available in the client, ie fetching all the child data for all rows on page load, when the parent Datatable initializes then you should be able to access that data to determine whether to display the details-control or not.

    But if your child Datatable is fetching the data via ajax each time its open then there isn't a way for the parent to query anything to determine what to do.

    Kevin

  • dt_userdt_user Posts: 51Questions: 16Answers: 0

    Ok. This is the link to my program: http://live.datatables.net/jaziqiya/18/.
    I want to be able to show any amount of child datatables right now if I try to open a second child datatable with another one already open the second datatable only shows the column headers.
    At first I thought the [.on( click) ] was accumulating so i tried .off('click') to fix it but that didnt work.
    Please let me know how I can fix this problem.
    Thank you.

  • kthorngrenkthorngren Posts: 20,302Questions: 26Answers: 4,769

    You are using the same HTML id for the child Datatable:

    function sub_DataTable() {
       subtable = $('#child').DataTable({
    

    You will need to decide on a way to have different id's for each child Datatable. Otherwise you are just reinitializing the same child Datatable which is why the header shows for the second table. I think this is the example you wanted to show us:
    http://live.datatables.net/hecupufi/1/edit

    Here is an example I posted in another thread. It uses a unique data column in the parent table to create the id of the child table. You could do something similar.

    Kevin

  • dt_userdt_user Posts: 51Questions: 16Answers: 0

    Ok understood. I thought using the destroy:true option would allow me to destroy the datatable and recreate it so the reinitializing datatables error would be fixed.
    Can I clear the tbody of the child datatable and put new data in it. Would that fix my problem?
    There isnt a link provided to the example pertaining to how to create unique id for each child datatable.

  • colincolin Posts: 15,143Questions: 1Answers: 2,586

    This might be useful here, it's another example where children contain another table, each with a unique ID.

  • kthorngrenkthorngren Posts: 20,302Questions: 26Answers: 4,769
    edited July 2019

    There isnt a link provided to the example pertaining to how to create unique id for each child datatable.

    Sorry copied the wrong link:
    http://live.datatables.net/gohefoki/1/edit

    Can I clear the tbody of the child datatable and put new data in it.

    In HTML all id's need to be unique. If you want to use the same id for the child table then you would need to make sure only one is open at a time. Also the previous needs to be removed from HTML using something like jQuery remove() before creating the new.

    Kevin

This discussion has been closed.