summaryrefslogtreecommitdiff
path: root/client/tables.js
blob: 6b00bbde32854d30157960ebea4ddbf80edede0a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import Tabulator from 'tabulator-tables'
import csv from 'parse-csv'

const datasetColumns = [
  { title: 'Title', field: 'title', sorter: 'string' },
  { title: 'Images', field: 'images', sorter: 'number' },
  { title: 'People', field: 'people', sorter: 'number' },
  { title: 'Year', field: 'year', sorter: 'number' },
  { title: 'Citations', field: 'citations', sorter: 'number' },
  { title: 'Influenced', field: 'influenced', sorter: 'number' },
  // { title: 'Origin', field: 'origin', sorter: 'string' },
]

function getColumns(payload) {
  if (payload.opt.match('datasets.csv')) {
    return datasetColumns
  }
  return (payload.fields || '').split(', ').map(field => {
    switch (field) {
      default:
        return { title: field, field: field.toLowerCase(), sorter: 'string' }
    }
  })
}

export default function append(el, payload) {
  const columns = getColumns(payload)
  const table = new Tabulator(el, {
    height: '311px',
    layout: 'fitDataFill',
    placeholder: 'No Data Set',
    columns,
  })
  // let path = payload.opt
  // console.log(path, columns)
  fetch(payload.opt, { mode: 'cors' })
    .then(r => r.text())
    .then(text => {
      const data = csv.toJSON(text, { headers: { included: true } })
      console.log(data)
      table.setData(data)
    })
}