I want to load the table.html
file, which contains a table with bootstrap classes, on the index page. The problem is that it does not get style after loading the table. What is the reason?
If I import bootstrap libraries in the table.html
file, my problem will be solved, but this solution is not suitable because for each bootstrap library call is loaded, I want to import the desired libraries only once in the index page, and each time my tables load in it.
here is my code: (you can also see https://github.com/yarandish/Challanges)
index.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
var cache = null;
</script>
<link rel="stylesheet" href="lib/bootstrap/css/bootstrap.rtl.min.css">
<link rel="stylesheet" href="lib/table/bootstrapTable/css/bootstrap-table.min.css">
<script src="lib/jquery/jquery-3.6.0.min.js"></script>
<script src="lib/bootstrap/js/bootstrap.min.js"></script>
<script src="lib/table/bootstrapTable/js/bootstrap-table.min.js"></script>
</head>
<body>
<input type="text" id="name">
<input type="text" id="price">
<input type="text" id="age">
<button onclick="cache = $('body'); $('body').load('table.html');">load table</button>
</body>
</html>
table.html :
<table
id="table"
data-toggle="table"
data-url="data.json"
data-buttons-class="outline-secondary"
data-classes="table table-striped table-hover table-bordered"
data-toolbar=".toolbar" data-maintain-meta-data="true" tabindex="-1"
data-click-to-select="true"
data-multiple-select-row="true"
>
<thead>
<tr>
<th data-checkbox="true"></th>
<th data-field="name">name</th>
<th data-field="price">price</th>
<th data-field="day">day</th>
</tr>
</thead>
</table>
You need to rerun the scripts when you add new contents. Just add following code at the end of table.html:
<script>
$('[data-toggle="table"]').bootstrapTable();
</script>
Here is the working demo on codepen.
If you don't want to target all the tables then you can also use:
$('#table').bootstrapTable();