Skip to content

Instantly share code, notes, and snippets.

@dmfranko
Created October 26, 2015 19:16
Show Gist options
  • Save dmfranko/67db6ddfb5ed61d115e6 to your computer and use it in GitHub Desktop.
Save dmfranko/67db6ddfb5ed61d115e6 to your computer and use it in GitHub Desktop.
ServiceNow and datatables
<?xml version="1.0" encoding="UTF-8"?>
<j:jelly xmlns:j="jelly:core" xmlns:g="glide" xmlns:g2="null" xmlns:j2="null" trim="false">
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.0.6/angular.min.js" />
<!-- Don't include bootstrap or jquery-->
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/r/bs/pdfmake-0.1.18,dt-1.10.9,b-1.0.3,b-html5-1.0.3,b-print-1.0.3,r-1.0.7,sc-1.3.0/datatables.min.css" />
<script type="text/javascript" src="https://cdn.datatables.net/r/bs/pdfmake-0.1.18,dt-1.10.9,b-1.0.3,b-html5-1.0.3,b-print-1.0.3,r-1.0.7,sc-1.3.0/datatables.min.js" />
<div class="container" id="container" ng-app="myApp">
<h4>My Incidents</h4>
<table class="table display nowrap" id="incidents" ng-controller="IncidentController" cellspacing="0" width="100%">
<thead>
<tr>
<th>Number</th>
<th>Short Description</th>
<th>State</th>
<th class="desktop">Opened</th>
<th class="none">Category</th>
</tr>
</thead>
<tfoot>
<tr>
<th>Number</th>
<th>Short Description</th>
<th>State</th>
<th>Opened</th>
<th>Category</th>
</tr>
</tfoot>
<tr id="incident" ng-repeat="incident in incidents" on-last-repeat="">
<td>{{incident.number}}</td>
<td>{{incident.short_description}}</td>
<td>{{incident.state}}</td>
<td>{{incident.opened_at}}</td>
<td>{{incident.category}}</td>
</tr>
</table>
</div>
<script type="text/javascript">
var app = angular.module('myApp', []);
function IncidentController($scope, $http) {
// Make sure to escape any ampersands
$http.get('/api/now/table/incident?sysparm_display_value=true&amp;sysparm_query=ORDERBYDESCnumber^active=true^opened_by=' + "${gs.getUserID()}")
.success(function(data, status, headers, config) {
$scope.incidents = data.result;
});
}
// Apply datatables after last repeat, otherwise it won't render right
app.directive('onLastRepeat', function() {
return function(scope, element, attrs) {
if (scope.$last) setTimeout(function() {
var list = $j('#incidents').DataTable({
"processing": true,
"responsive": true,
buttons: [
'pdf'
],
"order": [
[0, "desc"]
]
});
list.buttons().container()
.appendTo($('.col-sm-6:eq(0)', list.table().container()));
}, 1);
};
});
</script>
</j:jelly>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment