diff --git a/src/views/tableView.js b/src/views/tableView.js index 183bc65cc3e3b6d48d484bbfd4e3efa2c6068faf..e501c1e3d1e53e7f510a07beeddd0f52cc2a76ce 100644 --- a/src/views/tableView.js +++ b/src/views/tableView.js @@ -17,6 +17,7 @@ class TableRow { } } + export default class TableView { constructor(vnode) { this.items = []; @@ -24,17 +25,55 @@ export default class TableView { this.titles = vnode.attrs.titles || this.show_keys; this.resource = vnode.attrs.resource; // the querystring is either given or will be parsed from the url - if (vnode.attrs.querystring) { - this.querystring = vnode.attrs.querystring; + if (vnode.attrs.query) { + this.query = vnode.attrs.query; } else { - this.querystring = m.buildQueryString(m.route.param()); + this.query = m.route.param(); } } - oninit() { + // definitions of query parameters in addition to API go here + buildQuerystring() { + const queryKeys = Object.keys(this.query); + + if (queryKeys.length === 0) return ''; + + const query = {}; + + if ('search' in this.query && this.query.search.length > 0) { + // translate search into where, we just look if any field contains search + const searchQuery = { + $or: this.show_keys.map((key) => { + const fieldQuery = {}; + fieldQuery[key] = this.query.search; + return fieldQuery; + }), + }; + + // if there exists already a where-filter, AND them together + if ('where' in this.query) { + query.where = JSON.stringify({ $and: [searchQuery, this.query.where] }); + } else { + query.where = JSON.stringify(searchQuery); + } + } else { + query.where = JSON.stringify(this.query.where); + } + + // add all other keys + queryKeys.filter(key => (key !== 'where' && key !== 'search')) + .forEach((key) => { query[key] = JSON.stringify(this.query[key]); }); + + console.log(query); + + // now we can acutally build the query string + return `?${m.buildQueryString(query)}`; + } + + buildList() { getSession().then((apiSession) => { let url = this.resource; - if (this.querystring.length > 0) url += `?${this.querystring}`; + if (Object.keys(this.query).length > 0) url += this.buildQuerystring(); apiSession.get(url).then((response) => { this.items = response.data._items; console.log(this.items); @@ -45,8 +84,21 @@ export default class TableView { }); } + oninit() { + this.buildList(); + } + view() { return m('div', [ + m('div.row', m('div.col-xs-4.input-group', [ + m('input[name=search].form-control', { + value: this.query.search, + onchange: m.withAttr('value', (value) => { this.query.search = value; }), + }), + m('span.input-group-btn', m('button.btn.btn-default', { + onclick: () => { this.buildList(); }, + }, 'Search')), + ])), m('table.table.table-hover', [ m('thead', m('tr', this.titles.map(title => m('th', title)))), m('tbody', this.items.map(item =>