如何使用排序,搜索和显示Bootstrap 5表中的每个页面

发布于 2025-02-13 09:12:56 字数 1031 浏览 0 评论 0原文

我有一个非常简单的表:

”在此处输入图像描述”

,我包括Bootstrap 5:

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js" integrity="sha384-QJHtvGhmr9XOIpI6YVutG+2QOK9T+ZnN4kzFN1RtK3zEFEIsxhlmWl5/YESvpZ13" crossorigin="anonymous"></script>

我想了解如何按值实现搜索(在特定列中寻找一个值),选择当前显示的行数并按列进行排序,

我知道过去可以使用$('#table')。BootTraptable ,但我没有找到如何在版本5中执行此操作的示例

I have a very simple table:

enter image description here

And I include Bootstrap 5:

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js" integrity="sha384-QJHtvGhmr9XOIpI6YVutG+2QOK9T+ZnN4kzFN1RtK3zEFEIsxhlmWl5/YESvpZ13" crossorigin="anonymous"></script>

I want to understand how can I implement searching by values (looking for a value in specific columns), choosing the number of rows that are currently shown and sorting by columns

I know that in the past this could be done with $('#Table').bootstrapTable, but I did not find examples of how to do this in version 5

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乖乖兔^ω^ 2025-02-20 09:12:56

我意识到现成的解决方案需要一系列进口(包括jQuery)。编写自己的代码要容易得多,这就是我

在这里做的是一个基本示例。为了简单起见,我已经剥离了所有类属性。我还为&lt; td&gt;标签添加了一个value,但是如果您没有复杂的嵌套结构,则不必要>&lt; td&gt; value&lt;/td&gt; )

<form>
    <input placeholder="Search" id="search-in-table" onkeypress="return event.keyCode != 13;">
</form>

<p>Show per page:</p>
<select id="form-select-coins">
    <option value="10" selected>10</option>
    <option value="20">20</option>
    <option value="30">30</option>
</select>

<table id="coins-table">

    <thead>

        <tr>
            <th>Name</th>
            <th>Holdings</th>
            <th>Price</th>
            <th>Avg. Buy Price</th>
            <th>Wallet Balance</th>
            <th>Profit/Loss</th>
        </tr>

    </thead>

    <tbody>

        <tr coin="COIN">
            <td value="COIN">COIN</td>
            <td value="TOTAL_COINS">TOTAL_COINS</td>
            <td value="CURRENT_PRICE">CURRENT_PRICE</td>
            <td value="AVG_BUY_PRICE">AVG_BUY_PRICE</td>
            <td value="WALLET_BALANCE">WALLET_BALANCE</td>
            <td value="PROFIT_LOSS">PROFIT_LOSS</td>
        </tr>

    </tbody>

</table>
function hide_table_elements(table_id, visible) {
    // Shows only the first `visible` table elements
    table_elements = document.getElementById(table_id).children[1].children

    for (const element of table_elements) {
        if (visible == 0) {
            element.style.display = 'none'
        }
        else {
            element.style.display = 'table-row'
            visible -= 1
        }
    }
}

// Use below solution for <td> without `value` attribute
// const getCellValue = (tr, idx) => tr.children[idx].innerText.replace('

它便宜且快速,但是如果您想使用现成的解决方案,请查看在这里

, '') || tr.children[idx].textContent.replace('

它便宜且快速,但是如果您想使用现成的解决方案,请查看在这里

, ''); const getCellValue = (tr, idx) => tr.children[idx].getAttribute('value') const comparer = (idx, asc) => (a, b) => ((v1, v2) => v1 !== '' && v2 !== '' && !isNaN(v1) && !isNaN(v2) ? v1 - v2 : v1.toString().localeCompare(v2)) (getCellValue(asc ? a : b, idx), getCellValue(asc ? b : a, idx)) const reload_table = () => hide_table_elements('coins-table', document.getElementById('form-select-coins').value) window.addEventListener('load', function () { reload_table() // Show per page document.getElementById('form-select-coins').addEventListener('change', function() { counter = this.value hide_table_elements('coins-table', this.value) }); // Search in table document.getElementById('search-in-table').addEventListener('input', function() { rows = document.getElementById('coins-table').children[1].querySelectorAll('tr:nth-child(n)') value = this.value.toLowerCase() if (value == '') return reload_table() for (const row of rows) { if (row.getAttribute('coin').toLowerCase().includes(value)) { row.style.display = 'table-row' } else { row.style.display = 'none' } } }); // Sort table document.querySelectorAll('th').forEach(th => th.addEventListener('click', (() => { const table = document.getElementById('coins-table').children[1] Array.from(table.querySelectorAll('tr:nth-child(n)')) .sort(comparer(Array.from(th.parentNode.children).indexOf(th), this.asc = !this.asc)) .forEach(tr => table.appendChild(tr)); reload_table() }))); });

它便宜且快速,但是如果您想使用现成的解决方案,请查看在这里

I realized that ready-made solutions require a cascade of imports (including jquery). It's much easier to write your own code, which is what I did

Here is a basic example. I've stripped out all class attributes for simplicity. I also added a value for the <td> tags, but you don't have to if you don't have a complex nested structure (i.e. just a <td>value</td>)

<form>
    <input placeholder="Search" id="search-in-table" onkeypress="return event.keyCode != 13;">
</form>

<p>Show per page:</p>
<select id="form-select-coins">
    <option value="10" selected>10</option>
    <option value="20">20</option>
    <option value="30">30</option>
</select>

<table id="coins-table">

    <thead>

        <tr>
            <th>Name</th>
            <th>Holdings</th>
            <th>Price</th>
            <th>Avg. Buy Price</th>
            <th>Wallet Balance</th>
            <th>Profit/Loss</th>
        </tr>

    </thead>

    <tbody>

        <tr coin="COIN">
            <td value="COIN">COIN</td>
            <td value="TOTAL_COINS">TOTAL_COINS</td>
            <td value="CURRENT_PRICE">CURRENT_PRICE</td>
            <td value="AVG_BUY_PRICE">AVG_BUY_PRICE</td>
            <td value="WALLET_BALANCE">WALLET_BALANCE</td>
            <td value="PROFIT_LOSS">PROFIT_LOSS</td>
        </tr>

    </tbody>

</table>
function hide_table_elements(table_id, visible) {
    // Shows only the first `visible` table elements
    table_elements = document.getElementById(table_id).children[1].children

    for (const element of table_elements) {
        if (visible == 0) {
            element.style.display = 'none'
        }
        else {
            element.style.display = 'table-row'
            visible -= 1
        }
    }
}

// Use below solution for <td> without `value` attribute
// const getCellValue = (tr, idx) => tr.children[idx].innerText.replace('

It's cheap and fast, but if you want to use ready-made solutions, look here

, '') || tr.children[idx].textContent.replace('

It's cheap and fast, but if you want to use ready-made solutions, look here

, ''); const getCellValue = (tr, idx) => tr.children[idx].getAttribute('value') const comparer = (idx, asc) => (a, b) => ((v1, v2) => v1 !== '' && v2 !== '' && !isNaN(v1) && !isNaN(v2) ? v1 - v2 : v1.toString().localeCompare(v2)) (getCellValue(asc ? a : b, idx), getCellValue(asc ? b : a, idx)) const reload_table = () => hide_table_elements('coins-table', document.getElementById('form-select-coins').value) window.addEventListener('load', function () { reload_table() // Show per page document.getElementById('form-select-coins').addEventListener('change', function() { counter = this.value hide_table_elements('coins-table', this.value) }); // Search in table document.getElementById('search-in-table').addEventListener('input', function() { rows = document.getElementById('coins-table').children[1].querySelectorAll('tr:nth-child(n)') value = this.value.toLowerCase() if (value == '') return reload_table() for (const row of rows) { if (row.getAttribute('coin').toLowerCase().includes(value)) { row.style.display = 'table-row' } else { row.style.display = 'none' } } }); // Sort table document.querySelectorAll('th').forEach(th => th.addEventListener('click', (() => { const table = document.getElementById('coins-table').children[1] Array.from(table.querySelectorAll('tr:nth-child(n)')) .sort(comparer(Array.from(th.parentNode.children).indexOf(th), this.asc = !this.asc)) .forEach(tr => table.appendChild(tr)); reload_table() }))); });

It's cheap and fast, but if you want to use ready-made solutions, look here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文