Parent Child Blog how to update parent column value

Parent Child Blog how to update parent column value

cpshartcpshart Posts: 246Questions: 49Answers: 5

I have replicated the blog entry Parent / child editing with Editor, and I am applying the logic to my own system requirements.

https://datatables.net/blog/2016-03-25

I have added an additional column widgets to both parent table sites and child table users

For selected Parent Row
If users = 0, display sites.widgets
If users > 0, display SUM(users.widgets)

I have sent Allan a pm with access to my system,

Client File
https://dividendlook.co.uk/wp-admin/post.php?post=23571&action=edit

Server Files
/home/ukincome/public_html/Editor-PHP-1.9.0/controllers/sitesc.php
/home/ukincome/public_html/Editor-PHP-1.9.0/controllers/usersc.php

Test Page
https://dividendlook.co.uk/testparentchildc/

The following code in the client file summates the widgets in the child rows and displays the value in the parent table widgets column.

I need to include some logic to say
if site.users = 0
{ data: 'sites.widgets' }
else
// SUM child rows users.widgets for selected parent
{ data: 'users',
render: function (data, type, row) {
return data.reduce( function (accum, item) {
return parseFloat(accum) + parseFloat(item.widgets);
}, 0 );
}
}

    var siteTable = $('#sites').DataTable( {
        dom: "Bfrtip",
        ajax: "../../Editor-PHP-1.9.0/controllers/sitesc.php",
        columns: [
            { data: 'sites.name' },
        { data: 'country.name'},
            { data: 'users', render: function ( data ) {
                return data.length;
            } },
                  { data: 'users',
              render: function (data, type, row) {
                return data.reduce( function (accum, item) {
                  return parseFloat(accum) + parseFloat(item.widgets);
                }, 0 );
              }
            }   

extract from sitesc.php

    var siteTable = $('#sites').DataTable( {
        dom: "Bfrtip",
        ajax: "../../Editor-PHP-1.9.0/controllers/sitesc.php",
        columns: [
            { data: 'sites.name' },
            { data: 'country.name'},
            { data: 'users', render: function ( data ) {
                return data.length;
            } },
            { data: 'users',
              render: function (data, type, row) {
                return data.reduce( function (accum, item) {
                  return parseFloat(accum) + parseFloat(item.widgets);
                }, 0 );
              }
            }   

Many Thanks for your help.

Best Regards Colin

Answers

  • kthorngrenkthorngren Posts: 21,343Questions: 26Answers: 4,954
    edited July 2020 Answer ✓

    Are you looking for something like this?

            { data: 'users',
              render: function (data, type, row) {
                if ( users.length === 0 ) {
                  return row.sites.widgets;
                }
                return data.reduce( function (accum, item) {
                  return parseFloat(accum) + parseFloat(item.widgets);
                }, 0 );
              }
            }  
    

    Its not clear to me if your summation code works.

    Kevin

  • cpshartcpshart Posts: 246Questions: 49Answers: 5
    edited July 2020

    Hi Kevin

    Thanks for your very quick response.

    I have applied your code changes

    The parent datatable widgets column displays

    0 value for widgets where users.length = 0
    and
    correct summation value SUM(users.widgets) for highlighted parent where users.length > 0
    

    whereas I need

    sites.widgets value displayed where users.length = 0 
    (regardless of whether the row is highlighted.)
    and as above where users.length > 0
    

    I have sent you a PM with access details to my site.

    Thanks Colin

  • cpshartcpshart Posts: 246Questions: 49Answers: 5

    Kevin

    Silly question, but just for clarification, when prompted

    Did this answer the question? Yes · No
    

    Do I only answer Yes, when I no longer need help, i.e. the answer has 100% fixed my problem, as in lot of cases it may be a partial solution but not completed as in your last response, do I answer No in those cases or Yes as we are closer to a solution ?

    I looked in the help, FAQ for guidance but could not find any information.

    Thanks Colin

  • kthorngrenkthorngren Posts: 21,343Questions: 26Answers: 4,954
    Answer ✓

    Took a look and I see this data structure:

    Widgets looks to be 0.0000. Are you expecting to see 0.0000 or something else?

    Kevin

  • kthorngrenkthorngren Posts: 21,343Questions: 26Answers: 4,954
    Answer ✓

    Do I only answer Yes, when I no longer need help,

    Think of this as a way to indicate, to other users looking at your thread, answers that helped you. This does not close the thread nor stop further help.

    Kevin

  • cpshartcpshart Posts: 246Questions: 49Answers: 5

    Hi Kevin

    In the case of Edinburgh as it has child rows I would summate users.widgets = 0 in this case, so that is correct. In case of Plymouth, sites.widgets = 25.0000, with no child rows, so the datatable should display 25 for Plymouth.

    The sum of child rows widgets needs to take take precedence over the parent value, sites.widgets, but if there are no child rows, parent table display should show sites.widgets.

    At present where there are child rows for the parent the system correctly summates users.widgets in parent datatable, e.g. London 116 (65+12+25+14) otherwise the system is failing to display sites.widgets where there are no child rows e.g. Plymouth (sites.widgets=25).

    Many Thanks

    Colin

  • kthorngrenkthorngren Posts: 21,343Questions: 26Answers: 4,954
    Answer ✓

    I gave you the wrong if statement. Instead of if ( users.length === 0 ) { it should be if ( data.length === 0 ) {.

    Kevin

  • cpshartcpshart Posts: 246Questions: 49Answers: 5

    Hi Kevin

    Fantastic exactly what I needed all working, I can transfer the workings to my system.

    Many thanks for your help.

    Best Colin

This discussion has been closed.