Advertisement
Guest User

Untitled

a guest
Apr 30th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.09 KB | None | 0 0
  1. # Autotable
  2.  
  3. ## Functionality
  4. Displays a table based on an array of objects. Each object is interpreted as a table row, and its properties as the table’s columns. Columns can be configured individually. The table supports:
  5.  
  6. - Sortable columns
  7. - Filterable rows
  8. - Render hyperlink based on cell value
  9. - Pagination in the form of “load more” button
  10.  
  11. **Note**: the table assumes it can load _all_ required data at once on initialization, and rerenders itself after each user interaction. No data is retrieved from the server as long as the page is not reloaded.
  12.  
  13. ## Usage in Nunjucks template
  14. Import and call the macro:
  15.  
  16. ``` nunjucks
  17. {% import "components/core/auto-table/autotable.html" as autoTable %}
  18. {{ autoTable.autoTable(id, url, sortby, config) }}
  19. {{ autoTable.autoTableTemplates() }}
  20. ```
  21.  
  22. **Note**: The autotable templates need to be loaded _just once_ on the page, even if you have multiple auto tables:
  23.  
  24. ## Usage in Python
  25. The front-end server should return JSON for the configured endpoint. The autotable’s fetch method sends an `'Accept': 'application/json'` header that can be used in Python as follows:
  26.  
  27. ``` python
  28. from dashboard.lib.ajax import prefers_json
  29.  
  30. if (prefers_json(request)):
  31. return jsonify({'rows': rows})
  32. ```
  33.  
  34. The API endpoint payload should be an object containing the property `rows` with a list of items that all have the same properties:
  35.  
  36. ``` json
  37. {
  38. "rows": [
  39. {
  40. "id": 1,
  41. "name": "Jesse",
  42. "has_kids": False,
  43. "money": 9000
  44. },
  45. {
  46. "id": 2,
  47. "name": "Anne",
  48. "has_kids": True,
  49. "money": 0
  50. },
  51. ...
  52. ]
  53. }
  54. ```
  55.  
  56. ## Configuration
  57.  
  58. The Nunjucks macro supports the following parameters:
  59.  
  60. | Parameter | Type | Default | Description |
  61. | ------------ | ------ | -------- | ----------- |
  62. | id | String | _(required)_ | The unique ID of the table, to make sure it doesn’t interfere with other `auto-table` on the page |
  63. | url | String | _(required)_ | The API endpoint where the table should get its data from (formatted as JSON object; see more information below) |
  64. | sortby | String | _(required)_ | The column that is sorted on by default |
  65. | config | Object | _(required)_ | The column configuration for the table. This is where you can define what columns are displayed, whether they are sortable, filterable, etc. (see more below) |
  66.  
  67. The configuration is passed like this (for `config` contents, see below):
  68.  
  69. ``` nunjucks
  70. {{ autoTable.autoTable(
  71. id = 'users',
  72. url = '/users/',
  73. sortby = 'name',
  74. config) }}
  75. ```
  76.  
  77. ### Configuring the table
  78. The table’s config is an object set in Nunjucks that contains all info on how to display each of the columns in the autotable.
  79.  
  80. | Property | Type | Description |
  81. | ------------ | ------ | ----------- |
  82. | column | String | Corresponds to the property in the rows received from the API |
  83. | title | String | Column’s title to be displayed in the table header |
  84. | filterable | Boolean | Whether the column can be filtered. Renders a dropdown with all unique values of the data set for this column |
  85. | sortable | Boolean | Whether the column can be sorted (A-Z). Ascending by default, user can switch between asc/desc |
  86. | link | Object | Will display a hyperlink instead of the column’s value. Uses `text` as link text, and `path` as base path appended by the `column` property (Ex: `/user/1` for Jesse in the examples above)
  87.  
  88. Example:
  89.  
  90. ``` nunjucks
  91. {% set config = [
  92. {
  93. "column": "name",
  94. "title": "Name"
  95. },
  96. {
  97. "column": "has_kids",
  98. "title": "Has kids?",
  99. "filterable": true
  100. },
  101. {
  102. "column": "money",
  103. "title": "Happiness level",
  104. "sortable": true
  105. },
  106. {
  107. "column": "id",
  108. "title": "",
  109. "link": {
  110. "text": "Details",
  111. "path": "/user/"
  112. }
  113. }
  114. ] %}
  115. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement