Enable DataTables for Markdown tables
Add a jQuery snippet to decorate any tables between a pair of "marker" divs with `class=display`. Credit [here][jq-hack] Now we can create DataTables using Markdown. Extend the explanations to include this info. [jq-hack]: http://www.beardedhacker.com/blog/2015/08/28/add-class-attribute-to-markdown-table/
This commit is contained in:
parent
0eee5ad0f1
commit
c1baee6f4a
@ -38,17 +38,24 @@
|
|||||||
$('[data-toggle="tooltip"]').tooltip()
|
$('[data-toggle="tooltip"]').tooltip()
|
||||||
})
|
})
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
{% if page.datatable == true %}
|
{% if page.datatable == true %}
|
||||||
|
<!-- Include the standard DataTables bits -->
|
||||||
<link rel="stylesheet" type="text/css" href="//cdn.datatables.net/1.10.13/css/jquery.dataTables.css">
|
<link rel="stylesheet" type="text/css" href="//cdn.datatables.net/1.10.13/css/jquery.dataTables.css">
|
||||||
<script type="text/javascript" charset="utf8" src="//cdn.datatables.net/1.10.13/js/jquery.dataTables.js"></script>
|
<script type="text/javascript" charset="utf8" src="//cdn.datatables.net/1.10.13/js/jquery.dataTables.js"></script>
|
||||||
|
<!-- First, this walks through the tables that occur between ...-begin
|
||||||
|
and ...-end and add the "datatable" class to them.
|
||||||
|
Then it invokes DataTable's standard initializer
|
||||||
|
Credit here: http://www.beardedhacker.com/blog/2015/08/28/add-class-attribute-to-markdown-table/
|
||||||
|
-->
|
||||||
<script>
|
<script>
|
||||||
$(document).ready(function(){
|
$(document).ready(function(){
|
||||||
|
$('div.datatable-begin').nextUntil('div.datatable-end', 'table').addClass('display');
|
||||||
$('table.display').DataTable( {
|
$('table.display').DataTable( {
|
||||||
paging: true,
|
paging: true,
|
||||||
stateSave: true,
|
stateSave: true,
|
||||||
searching: true
|
searching: true
|
||||||
}
|
});
|
||||||
);
|
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
@ -97,93 +97,36 @@ You also must add a class of `display` to your tables. You can change the class
|
|||||||
|
|
||||||
You can also add page-specific triggers (by copying the `<script></script>` block from the default layout into the page) and classes, which lets you use different options on different tables.
|
You can also add page-specific triggers (by copying the `<script></script>` block from the default layout into the page) and classes, which lets you use different options on different tables.
|
||||||
|
|
||||||
Markdown doesn't allow you to add classes to tables, so you'll need to use HTML for any DataTables. Here's an example:
|
If you use an HTML table, adding `class="display"` to the `<table>` tag is sufficient.
|
||||||
|
|
||||||
```html
|
Markdown, however, doesn't allow you to add classes to tables, so you'll need to use a trick: add `<div class="datatable-begin"></div>` before the table and `<div class="datatable-end"></div>` after the table. The default layout includes a jQuery snippet that automagically adds the `display` class to any table it finds between those two markers. So you can start with this (we've trimmed the descriptions for display):
|
||||||
<table id="sampleTable" class="display">
|
|
||||||
<thead>
|
```markdown
|
||||||
<tr>
|
<div class="datatable-begin"></div>
|
||||||
<th>Parameter</th>
|
|
||||||
<th>Description</th>
|
Food | Description | Category | Sample type
|
||||||
<th>Type</th>
|
------- | ------------------------------------- | -------- | -----------
|
||||||
<th>Default Value</th>
|
Apples | A small, somewhat round ... | Fruit | Fuji
|
||||||
</tr>
|
Bananas | A long and curved, often-yellow ... | Fruit | Snow
|
||||||
</thead>
|
Kiwis | A small, hairy-skinned sweet ... | Fruit | Golden
|
||||||
<tbody>
|
Oranges | A spherical, orange-colored sweet ... | Fruit | Navel
|
||||||
<tr>
|
|
||||||
<td>Parameter 1</td>
|
<div class="datatable-end"></div>
|
||||||
<td>Sample description
|
|
||||||
</td>
|
|
||||||
<td>Sample type</td>
|
|
||||||
<td>Sample default value</td>
|
|
||||||
</tr>
|
|
||||||
<tr>
|
|
||||||
<td>Parameter 2</td>
|
|
||||||
<td>Sample description
|
|
||||||
</td>
|
|
||||||
<td>Sample type</td>
|
|
||||||
<td>Sample default value</td>
|
|
||||||
</tr>
|
|
||||||
<tr>
|
|
||||||
<td>Parameter 3</td>
|
|
||||||
<td>Sample description
|
|
||||||
</td>
|
|
||||||
<td>Sample type</td>
|
|
||||||
<td>Sample default value</td>
|
|
||||||
</tr>
|
|
||||||
<tr>
|
|
||||||
<td>Parameter 4</td>
|
|
||||||
<td>Sample description
|
|
||||||
</td>
|
|
||||||
<td>Sample type</td>
|
|
||||||
<td>Sample default value</td>
|
|
||||||
</tr>
|
|
||||||
</tbody>
|
|
||||||
</table>
|
|
||||||
```
|
```
|
||||||
|
|
||||||
This renders to the following:
|
and get this:
|
||||||
|
|
||||||
|
<div class="datatable-begin"></div>
|
||||||
|
|
||||||
|
Food | Description | Category | Sample type
|
||||||
|
------- | ------------------------------------------------------------------------------------------------- | -------- | -----------
|
||||||
|
Apples | A small, somewhat round and often red-colored, crispy fruit grown on trees. | Fruit | Fuji
|
||||||
|
Bananas | A long and curved, often-yellow, sweet and soft fruit that grows in bunches in tropical climates. | Fruit | Snow
|
||||||
|
Kiwis | A small, hairy-skinned sweet fruit with green-colored insides and seeds. | Fruit | Golden
|
||||||
|
Oranges | A spherical, orange-colored sweet fruit commonly grown in Florida and California. | Fruit | Navel
|
||||||
|
|
||||||
|
<div class="datatable-end"></div>
|
||||||
|
|
||||||
<table id="sampleTable" class="display">
|
|
||||||
<thead>
|
|
||||||
<tr>
|
|
||||||
<th>Food</th>
|
|
||||||
<th>Description</th>
|
|
||||||
<th>Category</th>
|
|
||||||
<th>Sample type</th>
|
|
||||||
</tr>
|
|
||||||
</thead>
|
|
||||||
<tbody>
|
|
||||||
<tr>
|
|
||||||
<td>Apples</td>
|
|
||||||
<td>A small, somewhat round and often red-colored, crispy fruit grown on trees.
|
|
||||||
</td>
|
|
||||||
<td>Fruit</td>
|
|
||||||
<td>Fuji</td>
|
|
||||||
</tr>
|
|
||||||
<tr>
|
|
||||||
<td>Bananas</td>
|
|
||||||
<td>A long and curved, often-yellow, sweet and soft fruit that grows in bunches in tropical climates.
|
|
||||||
</td>
|
|
||||||
<td>Fruit</td>
|
|
||||||
<td>Snow</td>
|
|
||||||
</tr>
|
|
||||||
<tr>
|
|
||||||
<td>Kiwis</td>
|
|
||||||
<td>A small, hairy-skinned sweet fruit with green-colored insides and seeds.
|
|
||||||
</td>
|
|
||||||
<td>Fruit</td>
|
|
||||||
<td>Golden</td>
|
|
||||||
</tr>
|
|
||||||
<tr>
|
|
||||||
<td>Oranges</td>
|
|
||||||
<td>A spherical, orange-colored sweet fruit commonly grown in Florida and California.
|
|
||||||
</td>
|
|
||||||
<td>Fruit</td>
|
|
||||||
<td>Navel</td>
|
|
||||||
</tr>
|
|
||||||
</tbody>
|
|
||||||
</table>
|
|
||||||
|
|
||||||
Notice a few features:
|
Notice a few features:
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user