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:
@@ -13,33 +13,51 @@ register_search_tab({ tag => 'port', label => 'Port', provides_csv => 1 });
|
|||||||
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;
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
my @results = $rs->hri->all;
|
||||||
|
return unless scalar @results;
|
||||||
|
|
||||||
if ( request->is_ajax ) {
|
if ( request->is_ajax ) {
|
||||||
template 'ajax/search/port.tt', {results => $set},
|
my $json = to_json( \@results );
|
||||||
|
template 'ajax/search/port.tt', { results => $json },
|
||||||
{ layout => undef };
|
{ 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;
|
||||||
|
|||||||
@@ -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>
|
||||||
|
|
||||||
|
|||||||
@@ -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 ] %]
|
||||||
|
|||||||
Reference in New Issue
Block a user