【问题标题】:DataTables.js Select FiltersDataTables.js 选择过滤器
【发布时间】:2020-01-29 20:17:05
【问题描述】:

我正在使用 DataTables.js 构建一个非常简单的表格,如下所示:

<table id="table">
    <thead>
        <tr>
            <th>Type</th>
            <th>Name</th>
            <th>Category</th>
        </tr>
    </thead>
    <tbody>

        <?php $query = 'SELECT * FROM tablename'; $total_query = "SELECT COUNT(1) FROM (${query}) AS combined_table"; $total = $wpdb->get_var( $total_query ); $results = $wpdb->get_results( $query.' ORDER BY name ASC ', OBJECT ); foreach ($results as $result) {; ?>

        <tr>
            <td><?php echo $result->type; ?></td>
            <td><?php echo $result->name; ?></td>
            <td><?php echo $result->category; ?></td>
        </tr>

        <?php };?>

    </tbody>
</table>

效果很好,但我需要在页面上创建两个独立的选择下拉列表,用于过滤列 1(类型)和 3(类别)。像这样...

<select id="willfiltertype">
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
</select>

<select id="willfiltercategory">
    <option value="A">A</option>
    <option value="B">B</option>
    <option value="C">C</option>
</select>

这是我的 JS

$(document).ready(function () {
    $('#table').DataTable();
});

我真的在代码笔和 DataTables.js 网站上苦苦挣扎。我认为任何建议或示例都会在这里真正有所帮助和有用,因为对于中间人来说并不多。

【问题讨论】:

    标签: javascript php jquery datatables


    【解决方案1】:

    您可以通过 PHP 创建这些选择:

    <?php 
        $query = 'SELECT * FROM tablename'; 
        $total_query = "SELECT COUNT(1) FROM (${query}) AS combined_table"; 
        $total = $wpdb->get_var( $total_query ); 
        $results = $wpdb->get_results( $query.' ORDER BY name ASC ', OBJECT ); 
    ?>
    
    <select id="willfiltertype">
            <option value=" "> </option> 
        <?php foreach ($results as $result): ?>
            <option value="<?= $result->type; ?>"><?= $result->type; ?></option> 
        <?php endforeach; ?>
    </select>
    
    <select id="willfiltercategory">
            <option value=" "> </option> 
        <?php foreach ($results as $result): ?>
            <option value="<?= $result->category; ?>"><?= $result->category; ?></option> 
        <?php endforeach; ?>
    </select>
    
    <table id="table">
        <thead>
            <tr>
                <th>Type</th>
                <th>Name</th>
                <th>Category</th>
            </tr>
        </thead>
        <tbody>
    
            <?php foreach ($results as $result): ?>
    
            <tr>
                <td><?= $result->type; ?></td>
                <td><?= $result->name; ?></td>
                <td><?= $result->category; ?></td>
            </tr>
    
            <?php endforeach; ?>
    
        </tbody>
    </table>
    

    然后你就可以创建JS where selects would filter your DataTable:

    $(()=>{
        var table = $('#table').DataTable();
    
        $('#willfiltertype').on('change', function(){
           table.search(this.value).draw();   
        });
    
        $('#willfiltercategory').on('change', function(){
           table.search(this.value).draw();   
        });
    });
    

    $(()=>{
        var table = $('#table').DataTable();
    
        $('#willfiltertype').on('change', function(){
           table.search(this.value).draw();   
        });
    
        $('#willfiltercategory').on('change', function(){
           table.search(this.value).draw();   
        });
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> 
    <script src="https://cdn.datatables.net/1.10.20/js/jquery.dataTables.min.js"></script>
    <script src="https://cdn.datatables.net/1.10.20/js/dataTables.bootstrap4.min.js"></script> 
    
    <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.css"> 
    <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.20/css/dataTables.bootstrap4.min.css"> 
    
    <select id="willfiltertype">
        <option value=" "> </option> 
        <option value="1">1</option> 
        <option value="2">2</option> 
        <option value="3">3</option> 
    </select>
    
    <select id="willfiltercategory">
            <option value=" "> </option> 
            <option value="A">A</option> 
            <option value="B">B</option> 
            <option value="C">C</option>  
    </select>
    
    <table id="table">
        <thead>
            <tr>
                <th>Type</th>
                <th>Name</th>
                <th>Category</th>
            </tr>
        </thead>
        <tbody>
    
            <tr>
                <td>1</td>
                <td>rwerwer</td>
                <td>A</td>
            </tr>
            <tr>
                <td>3</td>
                <td>rwerwer</td>
                <td>B</td>
            </tr>
            <tr>
                <td>3</td>
                <td>rwerwer</td>
                <td>B</td>
            </tr>
            <tr>
                <td>2</td>
                <td>rwerwer</td>
                <td>C</td>
            </tr>
    
        </tbody>
    </table>

    【讨论】:

    • 太棒了!谢谢你。有没有办法将它们链接起来,以便它们被链接,所以它只显示具有每个选择的选定值的结果?
    • @onesixty,当然,这是可能的。解释这两个选择的行为。他们的价值观总是成对的吗?如果是这样,那你为什么需要这两个选择?最好创建一个新问题并完成这个问题。另外,您可以尝试找到chained selects
    • @onesixty,感谢您对我说一句好话)另外,不要忘记完全解释您希望的行为。应该从另一个选择中选择什么值,在这种情况下应该在 DataTable 中搜索哪个值。提出一个像这个一样清晰的新问题
    • 想想它背后的逻辑,你是对的,它要么 1. 不必要,要么我可以 2. 使用类似 appelsiini.net/projects/chained 的东西。再次感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-31
    • 2020-07-21
    • 2011-10-27
    • 2011-03-28
    • 1970-01-01
    • 2021-04-09
    • 1970-01-01
    相关资源
    最近更新 更多