Commit 87f6de5a authored by a001188's avatar a001188
Browse files

Sort implemented

parent 5e26d835
function search_ciTable(){
$('#cmdb-table').jtable(createSearchCiTable(search_ciColumnDef()));
$('#cmdb-table').jtable('load');
}
function search_ciColumnDef(){
var cd = {
next: {
title: 'Nästa',
width: '1%',
display: renderSearchCiSubTable
},
family_type_id: {
title: 'Familjetyp',
options: '/cmdb/api/v1/options/family_type',
width: '5%'
},
family_id: {
title: 'Familj',
options: '/cmdb/api/v1/options/families',
width: '5%'
},
id: {
title: 'id',
width: '1%',
list: false,
key: true
},
display_name: {
title: 'Namn',
width: '8%'
},
description: {
title: 'Beskrivning',
width: '10%'
},
kst: {
title: 'Kostnadsställe',
width: '3%'
},
product: {
title: 'Produkt',
width: '3%'
},
// sla: {
// title: 'SLA',
// width: '5%'
// },
class_information: {
title: 'Klasser',
width: '15%'
},
attribute_information: {
title: 'Attribut',
width: '15%'
},
// account: {
// title: 'Konto',
// options: function(data) {
// if(data.source==='list')
// return '/cmdb/api/v1/options/accounts/';
// else
// return data.record.id;
// },
// width: '5%'
// },
// group: {
// title: 'Grupp',
// options: function(data) {
// if(data.source==='list')
// return '/cmdb/api/v1/options/groups/';
// else
// return data.record.id;
// },
// width: '5%'
// },
contacts: {
title: 'Kontaktperson',
width: '5%'
}
};
return cd;
}
function createSearchCiTable(ciColumnDef){
var table = {
title: "Sök",
paging: true, //Enable paging
pageSize: 10, //Set page size (default: 10)
pageList: 'minimal',
sorting: true, //Enable sorting
multiSorting: true,
defaultSorting: 'display_name ASC', //Set default sorting
actions: {
listAction: function(postData, jtParams){
var search_string = document.getElementById("ci_search_field").value;
if(postData !== undefined)
postData['search_string']=search_string;
else
postData = {search_string:search_string};
return $.Deferred(function ($dfd) {
$.ajax({
url: '/cmdb/api/v1/ci/search/list?jtStartIndex=' + jtParams.jtStartIndex + '&jtPageSize=' + jtParams.jtPageSize + '&jtSorting=' + jtParams.jtSorting,
type: 'POST',
dataType: 'json',
data: postData,
success: function (data) {
$dfd.resolve(data);
},
error: function () {
$dfd.reject();
}
});
});
}
},
fields: ciColumnDef
};
return table;
}
function renderSearchCiSubTable (data) {
if(data.record.childs > 0){
return $('<img src="/images/structure.png" title="Nästa nivå"/>').click(function () {
createSearchCiSubTable(data, $(this).closest('tr'));
});
}
}
function createSearchCiSubTable(data, row){
$('#cmdb-table').jtable('openChildTable', row, {
title: 'Komponenter till ' + data.record.display_name,
actions: {
listAction: '/cmdb/api/v1/ci/child/list/' + data.record.id
},
fields: search_ciColumnDef()
},
function (data) { //opened handler
data.childTable.jtable('load');
});
}
function getSearchCiFamilyId(data){
return data.record.family_type_id;
}
\ No newline at end of file
......@@ -21,4 +21,7 @@ $(document).ready(function () {
accessNodesTable();
accessGraph(-1);
}
if(ref.includes('/search/')){
search_ciTable();
}
});
\ No newline at end of file
......@@ -9,7 +9,7 @@ const config = require('./config');
const winston = require('winston');
const connectionString = process.env.DATABASE_URL || config.db_url;
router.post('/enterprise_service/list', (req, res, next) => {
router.post('/enterprise_service/list/', (req, res, next) => {
var pool = new pg.Pool({
connectionString: connectionString,
});
......@@ -35,6 +35,74 @@ router.post('/enterprise_service/list', (req, res, next) => {
});
router.post('/search/list', (req, res, next) => {
var pool = new pg.Pool({
connectionString: connectionString,
});
pool.connect(function(err, client, done){
if(err) {
done();
return res.status(500).json({success: false, data: err});
}
var jtStartIndex = req.query['jtStartIndex'];
var jtPageSize = req.query['jtPageSize'];
var jtSorting = req.query['jtSorting'];
var ci_search = '%' + req.body.search_string.toUpperCase() + '%';
var sql_select = "select *, id as account, id as group, ";
sql_select+="(select count(*) from cmdb.ci ci2 where ci2.parent_id = ci1.id) as childs ";
var sql_count="select count(*) ";
var sql_from ="from cmdb.ci ci1 ";
var sql_where ="where id > 0 and (";
sql_where+="upper(ci1.display_name) like $1 ";
sql_where+="or upper(ci1.description) like $1 ";
sql_where+="or upper(ci1.class_information) like $1 ";
sql_where+="or upper(ci1.attribute_information) like $1 ";
sql_where+="or upper(ci1.contact_information) like $1 ";
sql_where+=") ";
var sql_order_by="order by $2 ";
var sql_limit ="LIMIT $3 OFFSET $4";
var sql = sql_select + sql_from + sql_where + sql_order_by + sql_limit;
var sql_cnt = sql_count + sql_from + sql_where;
var count;
var parameters = [ci_search, jtSorting, jtPageSize, jtStartIndex];
var parameters_cnt = [ci_search];
client.query(sql, parameters, function(err, result){
done();
if (err){
return res.json(JSON.parse('{"Result":"ERROR", "Message":"' +sql + '"}'));
}
else {
client.query(sql_cnt, parameters_cnt, function(err, result){
done();
if (err){
return res.json(JSON.parse('{"Result":"ERROR", "Message":"' +sql_cnt + '"}'));
}
else {
count = result.rows.length;
winston.log('info', 'ci/search', {
head: '************************************************************',
parameters: parameters,
count:count
});
}
});
var response = '{"Result":"OK", "Records":';
response+= JSON.stringify(result.rows);
response+= ',"TotalRecordCount":' + 21 + '}';
return res.json(JSON.parse(response));
}
});
});
});
router.post('/infrastructure/list', (req, res, next) => {
var pool = new pg.Pool({
connectionString: connectionString,
......@@ -44,7 +112,7 @@ router.post('/infrastructure/list', (req, res, next) => {
done();
return res.status(500).json({success: false , data: err});
}
var sql = "select * , (select count(*) from cmdb.ci c where c.parent_id = id) as childs from cmdb.ci where family_type_id = 5 and id > 0 order by family_type_id, family_id, display_name";
var sql = "select * , (select count(*) from cmdb.ci ci2 where ci2.parent_id = ci1.id) as childs from cmdb.ci ci1 where ci1.family_type_id = 5 and ci1.id > 0 order by ci1.family_type_id, ci1.family_id, ci1.display_name";
client.query(sql, [], function(err, result){
done();
if (err){
......@@ -61,7 +129,6 @@ router.post('/infrastructure/list', (req, res, next) => {
});
router.post('/child/list/:parent_id', (req, res, next) => {
var pool = new pg.Pool({
connectionString: connectionString,
......
......@@ -27,6 +27,8 @@ router.get('/view/:id/:i', function(req, res, next) {
return res.render('index_1', { title: id, index: i});
if(i==='2')
return res.render('index_2', { title: id, index: i});
if(i==='3')
return res.render('index_3', { title: id, index: i});
else
return res.render('index', { title: id, index: i});
});
......
......@@ -192,7 +192,7 @@ router.post('/classes/:class_type', (req, res, next) => {
});
router.post('/groups/:owner_id', (req, res, next) => {
router.post('/groups/', (req, res, next) => {
var pool = new pg.Pool({
connectionString: connectionString,
});
......@@ -206,15 +206,15 @@ router.post('/groups/:owner_id', (req, res, next) => {
// SQL Query > Select Data
// var sql = "select fa.*, c.* from cmdb.ci_family_account fa inner join cmdb.class c on fa.account_type_id = c.id where parent_id = $1;";
var sql = "select * from cmdb.ci_family_group where parent_id = $1;";
var parameters = req.params['owner_id'];
client.query(sql, [parameters], function(err, result){
var sql = "select * from cmdb.ci_family_group;";
var parameters = [];
client.query(sql, [], function(err, result){
done();
if (err){
return res.json(JSON.parse('{"Result":"ERROR", "Message":"' + sql + '"}'));
}
else {
return res.json(JSON.parse(formateAccountAndGroupResult(result, parameters)));
return res.json(JSON.parse(formateAccountAndGroupResult(result)));
}
});
});
......@@ -222,7 +222,7 @@ router.post('/groups/:owner_id', (req, res, next) => {
router.post('/accounts/:owner_id', (req, res, next) => {
router.post('/accounts/', (req, res, next) => {
var pool = new pg.Pool({
connectionString: connectionString,
});
......@@ -236,15 +236,15 @@ router.post('/accounts/:owner_id', (req, res, next) => {
// SQL Query > Select Data
// var sql = "select fa.*, c.* from cmdb.ci_family_account fa inner join cmdb.class c on fa.account_type_id = c.id where parent_id = $1;";
var sql = "select * from cmdb.ci_family_account where parent_id = $1;";
var parameters = req.params['owner_id'];
client.query(sql, [parameters], function(err, result){
var sql = "select * from cmdb.ci_family_account;";
var parameters = [];
client.query(sql, parameters, function(err, result){
done();
if (err){
return res.json(JSON.parse('{"Result":"ERROR", "Message":"' + sql + '"}'));
}
else {
return res.json(JSON.parse(formateAccountAndGroupResult(result, parameters)));
return res.json(JSON.parse(formateAccountAndGroupResult(result)));
}
});
});
......@@ -252,10 +252,11 @@ router.post('/accounts/:owner_id', (req, res, next) => {
function formateAccountAndGroupResult(result, parameters){
function formateAccountAndGroupResult(result){
// return '{"Result":"OK", "Options":[{"DisplayText":"test", "Value":' + parameters + '}]}';
var response = '{"Result":"OK", "Options":[';
if(result.rows.length === 0){
response+= '{}';
//response+= '{}';
response+= ']}';
}
else{
......@@ -264,14 +265,14 @@ function formateAccountAndGroupResult(result, parameters){
response+= result.rows[i].display_name + '<br>';
}
response+= result.rows[result.rows.length-1].display_name;
response+= '", "Value":' + parameters;
response+= '", "Value":' + result.rows[result.rows.length-1].id;
response+= '}]}';
}
winston.log('info', '/accounts/:owner_id', {
response: response,
rows: result.rows.length
})
});
return response;
}
......
extends menu
block content
p(class='cmdbguiSmallText') #{title}
input(type='text', id='ci_search_field', onkeyup='search_ciTable()')
#cmdb-table
extends menu
block content
p(class='cmdbguiSmallText') #{title}
form(method='GET', action='/view/search/3')
input(type='text', id='ci_search', oninput='eventFire(document.getElementByName("submittButton").item(0), "click")')
button(name='submittButton', type='submit') Sök!
#cmdb-table
......@@ -19,6 +19,7 @@ html
script(type='text/javascript', src='/javascripts/select.js')
script(type='text/javascript', src='/javascripts/dashlets/ec-ci-table.js')
script(type='text/javascript', src='/javascripts/dashlets/search-ci-table.js')
script(type='text/javascript', src='/javascripts/dashlets/tfo-ci-table.js')
script(type='text/javascript', src='/javascripts/dashlets/organisation-table.js')
script(type='text/javascript', src='/javascripts/dashlets/organisation-graph.js')
......
......@@ -51,5 +51,7 @@ block menu
a(href="/view/financial-account/1") Kontoplan
li
a(href="/view/site/1") Siter och lokaler
li
a(href="/view/search/3") Sök
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment