From d8f05dc1a31d853666ff9df189ffab6c28d9f793 Mon Sep 17 00:00:00 2001 From: Gerhard Jungwirth Date: Fri, 8 Mar 2013 10:57:47 +0100 Subject: [PATCH] make /contacts use datatables --- lib/NGCP/Panel/Controller/Contact.pm | 49 +++++++++++++++++++++++++++ lib/NGCP/Panel/Controller/Reseller.pm | 1 - share/templates/contact/list.tt | 2 +- 3 files changed, 50 insertions(+), 2 deletions(-) diff --git a/lib/NGCP/Panel/Controller/Contact.pm b/lib/NGCP/Panel/Controller/Contact.pm index 6b9ff53139..7bc467e882 100644 --- a/lib/NGCP/Panel/Controller/Contact.pm +++ b/lib/NGCP/Panel/Controller/Contact.pm @@ -117,6 +117,55 @@ sub delete :Chained('base') :PathPart('delete') :Args(0) { $c->response->redirect($c->uri_for()); } +sub ajax :Chained('list') :PathPart('ajax') :Args(0) { + my ($self, $c) = @_; + + #TODO: when user is not logged in, this gets forwarded to login page + + #Process Arguments + my $sEcho = $c->request->params->{sEcho}; + my $sSearch = $c->request->params->{sSearch}; + my $iDisplayStart = $c->request->params->{iDisplayStart}; + my $iDisplayLength = $c->request->params->{iDisplayLength}; + + if(! $sEcho ) { + $sEcho = "1"; + } + if(! $sSearch ) { + $sSearch = ""; + } + + $c->stash(sEcho => $sEcho); + + #Parse contacts into aaData (for datatables) + my $data = $c->stash->{contacts}; + my $aaData = []; + + for my $row (@$data) { + if (index($row->{firstname}, $sSearch) >= 0 || + index($row->{lastname}, $sSearch) >= 0 || + index($row->{email}, $sSearch) >= 0) { + push @$aaData, [$row->{id}, + $row->{firstname}, + $row->{lastname}, + $row->{email}]; + } + } + my $totalRecords = scalar(@$aaData); + #Pagination + if($iDisplayStart || $iDisplayLength ) { + my $endIndex = $iDisplayLength+$iDisplayStart-1; + $endIndex = $#$aaData if $endIndex > $#$aaData; + @$aaData = @$aaData[$iDisplayStart .. $endIndex]; + } + + $c->stash(aaData => $aaData, + iTotalRecords => $totalRecords, + iTotalDisplayRecords => $totalRecords); + + $c->detach( $c->view("JSON") ); +} + =head1 AUTHOR diff --git a/lib/NGCP/Panel/Controller/Reseller.pm b/lib/NGCP/Panel/Controller/Reseller.pm index 6c0f7cd120..3c089eda77 100644 --- a/lib/NGCP/Panel/Controller/Reseller.pm +++ b/lib/NGCP/Panel/Controller/Reseller.pm @@ -131,7 +131,6 @@ sub delete :Chained('base') :PathPart('delete') :Args(0) { sub ajax :Chained('list') :PathPart('ajax') :Args(0) { my ($self, $c) = @_; - #TODO: pagination #TODO: when user is not logged in, this gets forwarded to login page #Process Arguments diff --git a/share/templates/contact/list.tt b/share/templates/contact/list.tt index 4e261aa819..7e337c5a1b 100644 --- a/share/templates/contact/list.tt +++ b/share/templates/contact/list.tt @@ -11,6 +11,6 @@ helper.edit_object = contact; helper.form_object = form; - PROCESS 'helpers/table_form.tt'; + PROCESS 'helpers/datatables.tt'; -%] [% # vim: set tabstop=4 syntax=html expandtab: -%]