.= 'tag.htm'; break; case 'flag': $pre .= $default_pre .= 'flag.htm'; break; case 'my': $pre .= $default_pre .= 'my.htm'; break; case 'my_password': $pre .= $default_pre .= 'my_password.htm'; break; case 'my_bind': $pre .= $default_pre .= 'my_bind.htm'; break; case 'my_avatar': $pre .= $default_pre .= 'my_avatar.htm'; break; case 'home_article': $pre .= $default_pre .= 'home_article.htm'; break; case 'home_comment': $pre .= $default_pre .= 'home_comment.htm'; break; case 'user': $pre .= $default_pre .= 'user.htm'; break; case 'user_login': $pre .= $default_pre .= 'user_login.htm'; break; case 'user_create': $pre .= $default_pre .= 'user_create.htm'; break; case 'user_resetpw': $pre .= $default_pre .= 'user_resetpw.htm'; break; case 'user_resetpw_complete': $pre .= $default_pre .= 'user_resetpw_complete.htm'; break; case 'user_comment': $pre .= $default_pre .= 'user_comment.htm'; break; case 'single_page': $pre .= $default_pre .= 'single_page.htm'; break; case 'search': $pre .= $default_pre .= 'search.htm'; break; case 'operate_sticky': $pre .= $default_pre .= 'operate_sticky.htm'; break; case 'operate_close': $pre .= $default_pre .= 'operate_close.htm'; break; case 'operate_delete': $pre .= $default_pre .= 'operate_delete.htm'; break; case 'operate_move': $pre .= $default_pre .= 'operate_move.htm'; break; case '404': $pre .= $default_pre .= '404.htm'; break; case 'read_404': $pre .= $default_pre .= 'read_404.htm'; break; case 'list_404': $pre .= $default_pre .= 'list_404.htm'; break; default: $pre .= $default_pre .= theme_mode_pre(); break; } if ($config['theme']) { $conffile = APP_PATH . 'view/template/' . $config['theme'] . '/conf.json'; $json = is_file($conffile) ? xn_json_decode(file_get_contents($conffile)) : array(); } !empty($json['installed']) and $path_file = APP_PATH . 'view/template/' . $config['theme'] . '/htm/' . ($id ? $id . '_' : '') . $pre; (empty($path_file) || !is_file($path_file)) and $path_file = APP_PATH . 'view/template/' . $config['theme'] . '/htm/' . $pre; if (!empty($config['theme_child']) && is_array($config['theme_child'])) { foreach ($config['theme_child'] as $theme) { if (empty($theme) || is_array($theme)) continue; $path_file = APP_PATH . 'view/template/' . $theme . '/htm/' . ($id ? $id . '_' : '') . $pre; !is_file($path_file) and $path_file = APP_PATH . 'view/template/' . $theme . '/htm/' . $pre; } } !is_file($path_file) and $path_file = APP_PATH . ($dir ? 'plugin/' . $dir . '/view/htm/' : 'view/htm/') . $default_pre; return $path_file; } function theme_mode_pre($type = 0) { global $config; $mode = $config['setting']['website_mode']; $pre = ''; if (1 == $mode) { $pre .= 2 == $type ? 'portal_category.htm' : 'portal.htm'; } elseif (2 == $mode) { $pre .= 2 == $type ? 'flat_category.htm' : 'flat.htm'; } else { $pre .= 2 == $type ? 'index_category.htm' : 'index.htm'; } return $pre; } ?>javascript - How to use sort, search and show per page in Bootstrap 5 table - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to use sort, search and show per page in Bootstrap 5 table - Stack Overflow

programmeradmin2浏览0评论

I have a very simple table:

And I include Bootstrap 5:

<link rel="stylesheet" href="/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<script src="/[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

I have a very simple table:

And I include Bootstrap 5:

<link rel="stylesheet" href="https://cdn.jsdelivr/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<script src="https://cdn.jsdelivr/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

Share Improve this question asked Jul 4, 2022 at 15:02 optic1optic1 1551 gold badge1 silver badge9 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 3

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 plex 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('$', '') || tr.children[idx].textContent.replace('$', '');
const getCellValue = (tr, idx) => tr.children[idx].getAttribute('value')

const parer = (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(parer(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

发布评论

评论列表(0)

  1. 暂无评论