Parent Child Blog how to update parent column value
Parent Child Blog how to update parent column value
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
This question has accepted answers - jump to:
Answers
Are you looking for something like this?
Its not clear to me if your summation code works.
Kevin
Hi Kevin
Thanks for your very quick response.
I have applied your code changes
The parent datatable widgets column displays
whereas I need
I have sent you a PM with access details to my site.
Thanks Colin
Kevin
Silly question, but just for clarification, when prompted
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
Took a look and I see this data structure:
Widgets looks to be
0.0000
. Are you expecting to see0.0000
or something else?Kevin
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
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
I gave you the wrong if statement. Instead of
if ( users.length === 0 ) {
it should beif ( data.length === 0 ) {
.Kevin
Hi Kevin
Fantastic exactly what I needed all working, I can transfer the workings to my system.
Many thanks for your help.
Best Colin