diff --git a/client/pages/admin.js b/client/pages/admin.js index ebb03c2..a29bd16 100644 --- a/client/pages/admin.js +++ b/client/pages/admin.js @@ -14,18 +14,33 @@ function printDate(d) { else return "" } +function sorts(list) { + return { + onclick: function(e) { + var prop = e.target.getAttribute("data-sort-by") + if (prop) { + var first = list[0] + list.sort(function(a, b) { + return a[prop] > b[prop] ? 1 : a[prop] < b[prop] ? -1 : 0 + }) + if (first === list[0]) list.reverse() + } + } + } +} + /********** QUOTE TABLE **********/ function tableWithQuotes(quotes, editCallback, deleteCallback, reviewCallback) { var header = [ - m('tr', [ + m('tr', sorts(quotes), [ m('th', 'ID'), - m('th.client', 'Client'), + m('th[data-sort-by=name].client', 'Client'), m('th.description', 'Description'), - m('th.email', 'Email'), - m('th.date', 'Date'), + m('th[data-sort-by=email].email', 'Email'), + m('th[data-sort-by=date].date', 'Date'), m('th.edit', 'Edit'), m('th.delete', 'Delete'), m('th.preview', 'Review') @@ -80,7 +95,7 @@ var AdminPage = { const app = window.app; vm.quotes = m.prop([]); this.reloadQuotes = () => { - app.service('quotes').find().then(quotes => { + app.service('quotes').find({$sort: { name: 1}}).then(quotes => { vm.quotes(quotes.data); m.redraw(); });