From 9df80c6caf154ef17a800db7eafd84d3878bbd23 Mon Sep 17 00:00:00 2001 From: Lars Dieckow Date: Wed, 26 Jun 2013 15:27:45 +0200 Subject: [PATCH] hash-based aaData for datatables via serialisation hook --- Build.PL | 3 +- lib/NGCP/Panel/Controller/Root.pm | 33 +++++++++++++-------- lib/NGCP/Panel/Field/Contract.pm | 2 +- lib/NGCP/Panel/Field/ResellerContract.pm | 2 +- share/templates/helpers/datatables.tt | 2 +- share/templates/helpers/datatables_field.tt | 4 +-- 6 files changed, 28 insertions(+), 18 deletions(-) diff --git a/Build.PL b/Build.PL index 6d1b2a20a6..577edc9ac9 100644 --- a/Build.PL +++ b/Build.PL @@ -41,7 +41,8 @@ my $builder = Local::Module::Build->new( 'Moose::Util::TypeConstraints' => 0, 'MooseX::Object::Pluggable' => 0, 'namespace::autoclean' => 0, - 'NGCP::Schema' => '1.002', + 'NGCP::Schema' => '1.003', + 'Scalar::Util' => 0, 'strict' => 0, 'Template' => 0, 'warnings' => 0, diff --git a/lib/NGCP/Panel/Controller/Root.pm b/lib/NGCP/Panel/Controller/Root.pm index d47b2d8ce6..6fc7db4aa0 100644 --- a/lib/NGCP/Panel/Controller/Root.pm +++ b/lib/NGCP/Panel/Controller/Root.pm @@ -5,6 +5,7 @@ use namespace::autoclean; BEGIN { extends 'Catalyst::Controller' } use NGCP::Panel::Widget; +use Scalar::Util qw(blessed); # # Sets the actions in this controller to be registered with no prefix @@ -114,15 +115,13 @@ sub ajax_process_resultset :Private { my $totalDisplayRecords = $rs->count; #potentially selected Id as first element - if (defined($iIdOnTop)) { - my $topRow = $rs->find($iIdOnTop); - if (defined($topRow)) { - my %tmpTopRow = $topRow->get_columns; - my @tmpTopRowArray = @tmpTopRow{@$columns}; - push @$aaData, \@tmpTopRowArray; - - $rs = $rs->search({ 'me.id' => { '!=', $iIdOnTop}}); - } + if (defined $iIdOnTop) { + if (defined(my $row = $rs->find($iIdOnTop))) { + push @{ $aaData }, _prune_row($columns, $row->get_inflated_columns); + $rs = $rs->search({ 'me.id' => { '!=', $iIdOnTop} }); + } else { + $c->log->error("iIdOnTop $iIdOnTop not found in resultset " . ref $rs); + }; } #Sorting @@ -145,9 +144,7 @@ sub ajax_process_resultset :Private { } for my $row ($rs->all) { - my %tmpRow = $row->get_inflated_columns; - my @aaRow = @tmpRow{@$columns}; - push @$aaData, \@aaRow; + push @{ $aaData }, _prune_row($columns, $row->get_inflated_columns); } $c->stash( @@ -159,6 +156,18 @@ sub ajax_process_resultset :Private { } +sub _prune_row { + my ($columns, %row) = @_; + while (my ($k,$v) = each %row) { + unless ($k ~~ $columns) { + delete $row{$k}; + next; + } + $row{$k} = $v->datetime if blessed($v) && $v->isa('DateTime'); + } + return { %row }; +} + sub error_page :Private { my ($self,$c) = @_; diff --git a/lib/NGCP/Panel/Field/Contract.pm b/lib/NGCP/Panel/Field/Contract.pm index f3a6c3019f..cf737df5ff 100644 --- a/lib/NGCP/Panel/Field/Contract.pm +++ b/lib/NGCP/Panel/Field/Contract.pm @@ -11,7 +11,7 @@ has_field 'id' => ( template => 'share/templates/helpers/datatables_field.tt', ajax_src => '/contract/peering/ajax', table_titles => ['#', 'Contact #', 'Billing Profile #', 'Status'], - table_fields => ['id', 'contact.id', 'billing_profile.id', 'status'], + table_fields => ['id', 'contact_id', 'billing_profile', 'status'], ); has_field 'create' => ( diff --git a/lib/NGCP/Panel/Field/ResellerContract.pm b/lib/NGCP/Panel/Field/ResellerContract.pm index 1a0dbc25f6..d70aa365d0 100644 --- a/lib/NGCP/Panel/Field/ResellerContract.pm +++ b/lib/NGCP/Panel/Field/ResellerContract.pm @@ -11,7 +11,7 @@ has_field 'id' => ( template => 'share/templates/helpers/datatables_field.tt', ajax_src => '/reseller/ajax_contract', table_titles => ['#', 'Contact #', 'External #', 'Status'], - table_fields => ['id', 'contact.id', 'external_id', 'status'], + table_fields => ['id', 'contact_id', 'external_id', 'status'], ); has_field 'create' => ( diff --git a/share/templates/helpers/datatables.tt b/share/templates/helpers/datatables.tt index 19e94ea12b..bb25bd28d6 100644 --- a/share/templates/helpers/datatables.tt +++ b/share/templates/helpers/datatables.tt @@ -22,7 +22,7 @@ $(document).ready(function() { "sAjaxSource": "[% helper.ajax_uri %]", "aoColumns": [ [% FOREACH f IN helper.column_fields -%] - { "sName": "[% f %]" }, + { "mData": "[% f %]" }, [% END -%] { "mRender": function ( data, type, full ) { return '' + diff --git a/share/templates/helpers/datatables_field.tt b/share/templates/helpers/datatables_field.tt index d66dba1773..3c74a2503e 100644 --- a/share/templates/helpers/datatables_field.tt +++ b/share/templates/helpers/datatables_field.tt @@ -27,10 +27,10 @@ $(document).ready(function() { "aoColumns": [ [% FOREACH f IN table_fields -%] - { "sName": "[% f %]" }, + { "mData": "[% f %]" }, [% END -%] { "mRender": function ( data, type, full ) { - return ''; + return ''; }, "mData": null, "bSortable": false