Skip to content

Commit

Permalink
Added support for Bootstrap table (alshedivat#1283)
Browse files Browse the repository at this point in the history
Added support for [Bootstrap Table](https://bootstrap-table.com/).
Haven't checked the impact on website loading, but I believe [this kind
of table](https://examples.bootstrap-table.com/#welcomes/from-data.html)
is way more useful for blog posts and projects pages.
  • Loading branch information
george-gca authored and adityarauniyar committed Jan 17, 2024
1 parent 78b3a9b commit bf7e519
Show file tree
Hide file tree
Showing 8 changed files with 330 additions and 9 deletions.
6 changes: 6 additions & 0 deletions _includes/scripts/misc.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
$(function () {$('[data-toggle="tooltip"]').tooltip()})
</script>
{%- endif %}

{%- if site.enable_medium_zoom %}
<!-- Medium Zoom JS -->
<script defer src="https://cdn.jsdelivr.net/npm/medium-zoom@{{ site.medium_zoom.version }}/dist/medium-zoom.min.js" integrity="{{ site.medium_zoom.integrity }}" crossorigin="anonymous"></script>
Expand All @@ -15,6 +16,11 @@
<script defer src="https://cdn.rawgit.com/afeld/bootstrap-toc/v1.0.1/dist/bootstrap-toc.min.js"></script>
{% endif %}

<!-- Bootstrap Table -->
<link defer rel="stylesheet" href="https://unpkg.com/bootstrap-table@1.21.3/dist/bootstrap-table.min.css">
<script defer src="https://unpkg.com/bootstrap-table@1.21.3/dist/bootstrap-table.min.js"></script>

<!-- Load Common JS -->
<script src="{{ '/assets/js/no_defer.js' | relative_url }}"></script>
<script defer src="{{ '/assets/js/common.js' | relative_url }}"></script>
<script defer src="{{ '/assets/js/copy_code.js' | relative_url }}" type="text/javascript"></script>
103 changes: 103 additions & 0 deletions _posts/2023-03-21-tables.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
---
layout: post
title: displaying beatiful tables with Bootstrap Tables
date: 2023-03-20 14:37:00-0400
description: an example of how to use Bootstrap Tables
categories: sample-posts
giscus_comments: true
related_posts: true
datatable: true
---

Using markdown to display tables is easy. Just use the following syntax:

```markdown
| Left aligned | Center aligned | Right aligned |
| :----------- | :------------: | ------------: |
| Left 1 | center 1 | right 1 |
| Left 2 | center 2 | right 2 |
| Left 3 | center 3 | right 3 |
```

That will generate:

| Left aligned | Center aligned | Right aligned |
| :----------- | :------------: | ------------: |
| Left 1 | center 1 | right 1 |
| Left 2 | center 2 | right 2 |
| Left 3 | center 3 | right 3 |

<p></p>

It is also possible to use HTML to display tables. For example, the following HTML code will display a table with [Bootstrap Table](https://bootstrap-table.com/), loaded from a JSON file:

{% raw %}
```html
<table
id="table"
data-toggle="table"
data-url="{{ '/assets/json/table_data.json' | relative_url }}">
<thead>
<tr>
<th data-field="id">ID</th>
<th data-field="name">Item Name</th>
<th data-field="price">Item Price</th>
</tr>
</thead>
</table>
```
{% endraw %}

<table
data-toggle="table"
data-url="{{ '/assets/json/table_data.json' | relative_url }}">
<thead>
<tr>
<th data-field="id">ID</th>
<th data-field="name">Item Name</th>
<th data-field="price">Item Price</th>
</tr>
</thead>
</table>

<p></p>

By using [Bootstrap Table](https://bootstrap-table.com/) it is possible to create pretty complex tables, with pagination, search, and more. For example, the following HTML code will display a table, loaded from a JSON file, with pagination, search, checkboxes, and header/content alignment. For more information, check the [documentation](https://examples.bootstrap-table.com/index.html).

{% raw %}
```html
<table
data-click-to-select="true"
data-height="460"
data-pagination="true"
data-search="true"
data-toggle="table"
data-url="{{ '/assets/json/table_data.json' | relative_url }}">
<thead>
<tr>
<th data-checkbox="true"></th>
<th data-field="id" data-halign="left" data-align="center" data-sortable="true">ID</th>
<th data-field="name" data-halign="center" data-align="right" data-sortable="true">Item Name</th>
<th data-field="price" data-halign="right" data-align="left" data-sortable="true">Item Price</th>
</tr>
</thead>
</table>
```
{% endraw %}

<table
data-click-to-select="true"
data-height="460"
data-pagination="true"
data-search="true"
data-toggle="table"
data-url="{{ '/assets/json/table_data.json' | relative_url }}">
<thead>
<tr>
<th data-checkbox="true"></th>
<th data-field="id" data-halign="left" data-align="center" data-sortable="true">ID</th>
<th data-field="name" data-halign="center" data-align="right" data-sortable="true">Item Name</th>
<th data-field="price" data-halign="right" data-align="left" data-sortable="true">Item Price</th>
</tr>
</thead>
</table>
57 changes: 53 additions & 4 deletions _sass/_base.scss
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ hr {
}

table {

td,
th {
color: var(--global-text-color);
Expand Down Expand Up @@ -849,8 +848,8 @@ progress::-moz-progress-bar {
font-size: medium;
opacity: 0;
position: absolute;
right: .15rem;
top: .15rem;
right: .5rem;
top: .5rem;
}

&:active .copy,
Expand All @@ -861,6 +860,55 @@ progress::-moz-progress-bar {
}
}

.btn-group.dropdown {
.btn {
box-shadow: none;
-webkit-box-shadow: none;
}

.btn-secondary.dropdown-toggle {
border: 1px solid var(--global-divider-color);

.page-size {
color: inherit;
}

&:not(.active) {
background-color: var(--global-bg-color) !important;
color: var(--global-text-color);
}

&:hover {
background-color: var(--global-hover-color) !important;
color: var(--global-hover-text-color) !important;
}
}

.dropdown-menu {
background-color: var(--global-bg-color);
}

.dropdown-item {
background-color: var(--global-bg-color);
color: var(--global-text-color);

&:hover {
color: var(--global-hover-color);
}
}

.dropdown-item.active, .dropdown-item:active {
background-color: var(--global-hover-color);
color: var(--global-hover-text-color) !important;

&:hover {
color: var(--global-hover-text-color);
}
}
}

// Table of Contents

nav[data-toggle="toc"] {
top: 5rem;

Expand Down Expand Up @@ -898,4 +946,5 @@ nav[data-toggle="toc"] {
height: 0;
top: 0;
}
}
}

4 changes: 3 additions & 1 deletion _sass/_themes.scss
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
/*******************************************************************************
* Themes
******************************************************************************/

:root {
--global-bg-color: #{$white-color};
--global-code-bg-color: #{$code-bg-color-light};
--global-text-color: #{$black-color};
--global-text-color-light: #{$grey-color};
--global-theme-color: #{$purple-color};
--global-hover-color: #{$purple-color};
--global-hover-text-color: #{$white-color};
--global-footer-bg-color: #{$grey-color-dark};
--global-footer-text-color: #{$grey-color-light};
--global-footer-link-color: #{$white-color};
Expand Down Expand Up @@ -40,6 +41,7 @@ html[data-theme='dark'] {
--global-text-color-light: #{$grey-color-light};
--global-theme-color: #{$cyan-color};
--global-hover-color: #{$cyan-color};
--global-hover-text-color: #{$white-color};
--global-footer-bg-color: #{$grey-color-light};
--global-footer-text-color: #{$grey-color-dark};
--global-footer-link-color: #{$black-color};
Expand Down
6 changes: 4 additions & 2 deletions assets/js/common.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// add toggle functionality to abstract and bibtex buttons
$(document).ready(function() {
$('a.abstract').click(function() {
$(this).parent().parent().find(".abstract.hidden").toggleClass('open');
Expand All @@ -11,7 +12,7 @@ $(document).ready(function() {
});

// bootstrap-toc
$(function () {
$(document).ready(function () {
if($('#toc-sidebar').length){
var navSelector = "#toc-sidebar";
var $myNav = $(navSelector);
Expand All @@ -20,4 +21,5 @@ $(function () {
target: navSelector,
});
}
});
});

22 changes: 22 additions & 0 deletions assets/js/no_defer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// add bootstrap classes to tables
$(document).ready(function() {
$('table').each(function() {
// only select tables that are not inside an element with "news" (about page) or "card" (cv page) class
if($(this).parents('[class*="news"]').length==0 &&
$(this).parents('[class*="card"]').length==0 &&
$(this).parents('code').length == 0) {
// make table use bootstrap-table
$(this).attr('data-toggle','table');
// add some classes to make the table look better
// $(this).addClass('table-sm');
$(this).addClass('table-hover');

if (document.documentElement.getAttribute("data-theme") == "dark") {
$(this).addClass('table-dark');
} else {
$(this).removeClass('table-dark');
}
}
})
});

13 changes: 11 additions & 2 deletions assets/js/theme.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,17 @@ let setTheme = (theme) => {

if (theme) {
document.documentElement.setAttribute("data-theme", theme);
}
else {

// Add class to tables.
let tables = document.getElementsByTagName('table');
for(let i = 0; i < tables.length; i++) {
if (theme == "dark") {
tables[i].classList.add('table-dark');
} else {
tables[i].classList.remove('table-dark');
}
}
} else {
document.documentElement.removeAttribute("data-theme");
}
localStorage.setItem("theme", theme);
Expand Down
Loading

0 comments on commit bf7e519

Please sign in to comment.