简体   繁体   中英

Load bootstrap table from external file

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();

Due to the widespread use of tables across third-party widgets like calendars and date pickers, we've designed our tables to be opt-in. Just add the base class.table to any, then extend with custom styles or our various included modifier classes. ref

So you need manually those classes to the HTML markup:

  • table class to the table element
  • col class to each td/th elements
  • row class to each tr elements.

Working demo on replit

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM