Parent Child question

Parent Child question

JanNL_FRJanNL_FR Posts: 47Questions: 15Answers: 2

Hi,
https://editor.datatables.net/examples/advanced/parentChild.html
In the Parent / Child example there is mentioned '.id'
Is this related to the sites.id field or to the users.id field?
Can I use another field in the table or has it to be the primary 'id' field?

var usersTable = $('#users').DataTable({
    ajax: {
        url: '../php/users.php',
        type: 'post',
        data: function (d) {
            var selected = siteTable.row({ selected: true });
            if (selected.any()) {
                d.site = selected.data().id;  /*  this  'id' field */


var targetsTable = $('#targets').DataTable({
    ajax: {
        url: '../php/targets.php',
        type: 'post',
        data: function (d) {
            var selected = portfolioTable.row({ selected: true });
            if (selected.any()) {
                d.company_t_id = selected.data().company_p_id;  /* Can I replace it with this */
            }
        }
    },

Jan

This question has accepted answers - jump to:

Answers

  • kthorngrenkthorngren Posts: 21,083Questions: 26Answers: 4,908

    You may want to refer to this blog which describes the solution in the example you linked.

    selected.data().id; /* this 'id' field */

    This is getting the parent's selected row id object. In the example id not displayed in the table but is part of the parent's row data. You can see this using the browser's network inspector tool. It then assigns the id value to the site parameter sent to the server.

    d.company_t_id = selected.data().company_p_id; /* Can I replace it with this */

    Yes, you can use any valid parent row object. Your server script will need to parse the sent POST parameters to get company_t_id parameter. This value is used as part of the where clause to fetch the data for the child table data.

    Kevin

  • JanNL_FRJanNL_FR Posts: 47Questions: 15Answers: 2

    Thanks Kevin.

    When I don't comment out these lines, I get no data in the Child Table.

    <?php
    
    include( "../lib/DataTables.php" );
    
    use
        DataTables\Editor,
        DataTables\Editor\Field,
        DataTables\Editor\Format,
        DataTables\Editor\Mjoin,
        DataTables\Editor\Options,
        DataTables\Editor\Upload,
        DataTables\Editor\Validate,
        DataTables\Editor\ValidateOptions;
    
    //  if ( ! isset($_POST['company_t_id']) || ! is_numeric($_POST['company_t_id']) ) {
    //  echo json_encode( [ "data" => [] ] );
    //  }
    //  else {
            Editor::inst($db, 'esg_targets')
                ->field(
                    Field::inst('esg_targets.company_t_id')
                        ->options( 'esg_portfolio', 'id', 'company_p_id' )
                        ->validator( 'Validate::dbValues' ),
                    Field::inst('esg_targets.sourcelink'),
                    Field::inst('esg_targets.targetyears'),
                    Field::inst('esg_targets.targetsentence'),
                    Field::inst('esg_targets.sourcedate'),
                    Field::inst('esg_targets.uploaddate'),
                    
                    Field::inst('esg_portfolio.company_p_id'),
                    Field::inst('esg_portfolio.member_id')
                                    
                )
                
                ->leftJoin('esg_portfolio', 'esg_portfolio.id', '=', 'esg_targets.company_t_id')
    //          ->where( 'company_t_id', $_POST['company_t_id'] )
                
                ->process($_POST)
                ->json();
    //      }   
    
    
    

    Jan

  • kthorngrenkthorngren Posts: 21,083Questions: 26Answers: 4,908
    edited January 3

    Use the browser's network inspector to see what is sent in the company_t_id parameter. Is it a number? Maybe this part of the if statement is failing ! is_numeric($_POST['company_t_id'].

    Or it could be the where clause isn't working. Its hard to say without seeing the data and your solution in action.

    I would do a bit of debugging to see what is being sent and how its being handled in the PHP script.

    If you still need help then start by using the browser's Network Inspector to collect the POST parameters sent to the server. Post it here.

    Kevin

  • JanNL_FRJanNL_FR Posts: 47Questions: 15Answers: 2

    The Parent table works fine.
    For the Child table I get

    {data: []}
    data: []

  • kthorngrenkthorngren Posts: 21,083Questions: 26Answers: 4,908

    I understand that is what you are getting.

    Have you performed any of the debugging steps I suggested?

    What is being sent? Is it numeric? Does the value match the company_t_id field for the where clause to return rows? Code snippets don't give us this infomration.

    Ata a minimum use the browser's network inspector tool to get the POST parameters sent to the server and post here so we can take a look.

    Kevin

  • JanNL_FRJanNL_FR Posts: 47Questions: 15Answers: 2

    Hi Kevin,
    I haven't spent much time exploring JS and DataTables (I like it), nor debugging. I can find the BrowserTools but I don't really know where I can find, what you are asking for.
    Maybe this helps?
    campcare.eu/2/PortfolioTargets/pc.php

    Jan

  • allanallan Posts: 62,990Questions: 1Answers: 10,367 Site admin
    Answer ✓

    A link always provides the answers - thank you for that.

    You have:

    d.company_t_id = selected.data().id;
    

    in the ajax.data function for targetsTable. But there is no id property in the row.

    There is DT_RowId, but you might find it easier if you just added an id parameter to the object. Or you could use DT_RowId and remove the row_ prefix.

    Allan

  • JanNL_FRJanNL_FR Posts: 47Questions: 15Answers: 2

    company_p_id and company_t_id are the matching fields.
    can i use this or what is a good way to do?

    With this code there are still no records.

    var targetsTable = $('#targets').DataTable({
            ajax: {
                url: 's_targets_edit.php',
                type: 'post',
                data: function (d) {
                    var selected = portfolioTable.row({ selected: true });
                    if (selected.any()) {
                        d.company_t_id = selected.data().company_p_id;
                    }
                }
            },
    

    Jan

  • kthorngrenkthorngren Posts: 21,083Questions: 26Answers: 4,908
    Answer ✓

    The problem is that you have nested objects for your parent table, for example:

        var portfolioTable = $('#portfolio').DataTable({
            ajax: 's_portfolio_edit.php',
            columns: [
                { data: 'esg_portfolio.company_p_id' },
                { data: 'esg_portfolio.member_id' }
            ],
    

    But you are not accessing the nested data with this:

    d.company_t_id = selected.data().company_p_id;
    

    Try this:

    d.company_t_id = selected.data().esg_portfolio.company_p_id;
    

    Kevin

  • JanNL_FRJanNL_FR Posts: 47Questions: 15Answers: 2

    Thank you, that's it !

    Jan

Sign In or Register to comment.