Skip to content

Commit

Permalink
timwis#173 added rowDetails option to expand row and see details
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark Silverberg committed Apr 11, 2016
1 parent 5c25f49 commit 0e134af
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/data/sample.json
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,8 @@
"chartType": "table",
"provider": "socrata",
"domain": "data.cityofchicago.org",
"dataset": "xqx5-8hwx"
"dataset": "xqx5-8hwx",
"rowDetails": true
}
}
]
Expand Down
58 changes: 58 additions & 0 deletions src/scripts/views/table.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var _ = require('underscore')
var $ = require('jquery')
var Card = require('./card')
var LoaderOn = require('../util/loader').on
var LoaderOff = require('../util/loader').off
Expand Down Expand Up @@ -26,6 +27,31 @@ var addDescriptionToTitle = function (column) {
return column
}

var rowDetails = function (rowData, columns) {
// create columnLookup so we get column names and tooltips
var columnLookup = {}
_.each(columns, function (column) {
if (column.data != null) {
columnLookup[column.data] = column.title || column.data
}
})

var s = '<div class="table-responsive"><table class="table row-detail">'
s += '<thead><tr><th style="width: 15%">Attribute</th><th>Value</th></tr></thead>'
s += '<tbody>'

_.each(columnLookup, function (title, key) {
s += '<tr><td>' + title + '</td><td>'
if (!_.isUndefined(rowData[key])) {
s += rowData[key]
}
s += '</td></tr>'
})

s += '</tbody></table></div>'
return s
}

module.exports = Card.extend({
initialize: function (options) {
Card.prototype.initialize.apply(this, arguments)
Expand Down Expand Up @@ -56,6 +82,16 @@ module.exports = Card.extend({

columns = columns.map(addDescriptionToTitle)

if (this.config.rowDetails) {
// Add row detail control column
columns.unshift({
class: 'row-detail-control fa fa-plus-square-o',
orderable: false,
data: null,
defaultContent: ''
})
}

// Initialize the table
var container = this.$('.card-content table')
this.table = container.DataTable({
Expand All @@ -66,6 +102,28 @@ module.exports = Card.extend({
ajax: _.bind(this.dataTablesAjax, this)
})

if (this.config.rowDetails) {
// store this.table as table, which is used in the following callback
var table = this.table
// listen for row detail icon click and act apropriately
container.on('click', 'tr td.row-detail-control', function () {
var clicked = $(this)
var tr = clicked.closest('tr')
var row = table.row(tr)
if (row.child.isShown()) {
clicked.removeClass('fa-minus-square-o')
clicked.addClass('fa-plus-square-o')
tr.removeClass('details')
row.child.hide()
} else {
clicked.addClass('fa-minus-square-o')
clicked.removeClass('fa-plus-square-o')
tr.addClass('details')
row.child(rowDetails(row.data(), columns)).show()
}
})
}

this.activateTooltips(container)
}, this))
}
Expand Down
9 changes: 9 additions & 0 deletions src/styles/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ body {
white-space: nowrap;
}

.card-content .dataTables_scrollBody table thead .row-detail-control {
visibility: hidden;
}

.card-content table.row-detail{
margin-left: 40px;
width: inherit;
}

/*.callout .card-content {
min-height: 0;
}*/
Expand Down

0 comments on commit 0e134af

Please sign in to comment.