DataTables for port search results tab

Use device_port_vlan table to search for VLANs rather than the device_port table
This commit is contained in:
Eric A. Miller
2014-06-02 22:44:23 -04:00
parent a34094cc6c
commit 4f7a2097fc
3 changed files with 77 additions and 37 deletions

View File

@@ -7,39 +7,57 @@ use Dancer::Plugin::Auth::Extensible;
use App::Netdisco::Web::Plugin; use App::Netdisco::Web::Plugin;
use App::Netdisco::Util::Web 'sql_match'; use App::Netdisco::Util::Web 'sql_match';
register_search_tab({ tag => 'port', label => 'Port', provides_csv => 1 }); register_search_tab( { tag => 'port', label => 'Port', provides_csv => 1 } );
# device ports with a description (er, name) matching # device ports with a description (er, name) matching
get '/ajax/content/search/port' => require_login sub { get '/ajax/content/search/port' => require_login sub {
my $q = param('q'); my $q = param('q');
send_error('Missing query', 400) unless $q; send_error( 'Missing query', 400 ) unless $q;
my $set; my $rs;
if ($q =~ m/^\d+$/) { if ( $q =~ m/^\d+$/ ) {
$set = schema('netdisco')->resultset('DevicePort') $rs
->search({vlan => $q}); = schema('netdisco')->resultset('DevicePort')
->columns( [qw/ ip port name descr /] )->search(
{ "port_vlans.vlan" => $q },
{ '+columns' => [qw/ device.dns device.name port_vlans.vlan /],
join => [qw/ port_vlans device /]
}
);
} }
else { else {
my ($likeval, $likeclause) = sql_match($q); my ( $likeval, $likeclause ) = sql_match($q);
$set = schema('netdisco')->resultset('DevicePort') $rs
->search({-or => [ = schema('netdisco')->resultset('DevicePort')
{name => (param('partial') ? $likeclause : $q)}, ->columns( [qw/ ip port name descr /] )->search(
(length $q == 17 ? {mac => $q} { -or => [
: \['mac::text ILIKE ?', $likeval]), { "me.name" => ( param('partial') ? $likeclause : $q ) },
]}); ( length $q == 17
? { "me.mac" => $q }
: \[ 'me.mac::text ILIKE ?', $likeval ]
),
]
},
{ '+columns' => [qw/ device.dns device.name port_vlans.vlan /],
join => [qw/ port_vlans device /]
}
);
} }
return unless $set->count;
if (request->is_ajax) { my @results = $rs->hri->all;
template 'ajax/search/port.tt', {results => $set}, return unless scalar @results;
{ layout => undef };
if ( request->is_ajax ) {
my $json = to_json( \@results );
template 'ajax/search/port.tt', { results => $json },
{ layout => undef };
} }
else { else {
header( 'Content-Type' => 'text/comma-separated-values' ); header( 'Content-Type' => 'text/comma-separated-values' );
template 'ajax/search/port_csv.tt', {results => $set}, template 'ajax/search/port_csv.tt', { results => \@results },
{ layout => undef }; { layout => undef };
} }
}; };
true; 1;

View File

@@ -1,23 +1,45 @@
<table class="table table-bordered table-condensed table-striped nd_floatinghead"> <table id="data-table" class="table table-striped table-bordered" width="100%" cellspacing="0">
<thead> <thead>
<tr> <tr>
<th>Description</th>
<th>Port</th>
<th>Name</th> <th>Name</th>
<th>Port</th>
<th>Description</th>
<th>Vlan</th> <th>Vlan</th>
</tr> </tr>
</thead> </thead>
</tbody>
[% WHILE (row = results.next) %]
<tr>
<td>[% row.name | html_entity %]</td>
<td><a href="[% device_ports %]&q=[% row.ip | uri %]&f=[% row.port | uri %]">
[% row.ip | html_entity %] [ [% row.port | html_entity %] ]</a>
[% ' (' _ row.device.dns _ ')' IF row.device.dns %]
</td>
<td>[% row.descr | html_entity %]</td>
<td>[% row.vlan | html_entity %]</td>
</tr>
[% END %]
</tbody>
</table> </table>
<script type="text/javascript">
$(document).ready(function() {
var table = $('#data-table').dataTable({
"deferRender": true,
"data": [% results %],
"columns": [{
"data": 'name',
"render": function(data, type, row, meta) {
return he.encode(data || '');
}
}, {
"data": 'ip',
"render": function(data, type, row, meta) {
var ddns = '';
if (row.device.dns || row.device.name) {
ddns = ' (' + he.encode(row.device.dns || row.device.name) + ')';
}
return '<a href="[% device_ports %]&q=' + encodeURIComponent(data) + '&f=' + encodeURIComponent(row.port) + '">' + he.encode(data) + '[' + he.encode(row.port) + ']</a>' + ddns;
}
}, {
"data": 'descr',
"render": function(data, type, row, meta) {
return he.encode(data || '');
}
}, {
"data": 'port_vlans.vlan',
"render": function(data, type, row, meta) {
return data;
}
}
]
});
});</script>

View File

@@ -1,7 +1,7 @@
[% USE CSV -%] [% USE CSV -%]
[% CSV.dump([ 'Description' 'Port' 'Name' 'Vlan' ]) %] [% CSV.dump([ 'Description' 'Port' 'Name' 'Vlan' ]) %]
[% WHILE (row = results.next) %] [% FOREACH row IN results %]
[% mylist = [] %] [% mylist = [] %]
[% myport = "$row.ip [ $row.port ] (" _ row.device.dns _ ")" IF row.device.dns %] [% myport = "$row.ip [ $row.port ] (" _ row.device.dns _ ")" IF row.device.dns %]
[% FOREACH col IN [ row.name myport row.descr row.vlan ] %] [% FOREACH col IN [ row.name myport row.descr row.vlan ] %]