【问题标题】:DataTables Search "Male" and Female"DataTables 搜索“男性”和“女性”
【发布时间】:2016-09-02 09:03:36
【问题描述】:

我正在使用数据表,它有一个内置搜索(如果你这么称呼它的话),现在我在我的表列表中搜索性别时遇到问题,因为每当我搜索“男性”时,男性和女性出现在列表中。我会怎么做,如果我搜索男性,它将只过滤性别“男性”。但是如果我搜索女性,女性呢?如果我没有尝试任何东西,我很抱歉,因为我真的不知道。虽然我尝试搜索,但是那些与我有同样问题的人没有我理解的答案,所以我只是在尝试也许你们可以帮助我。如果你不能因为我没有尝试过任何东西,我理解。但我仍然希望。提前谢谢你!

数据库名称 - db_seq

表名 - 配置文件

表格列 - 姓名、性别、用户名、密码

编辑:我的代码

<?php include('dbcontroller.php');
    $sql = "SELECT name, gender FROM profile ORDER by name ASC";
    $res = mysqli_query($conn,$sql)or die(mysqli_error());
    ?>
    <table id="batchList" class="table table-striped table-bordered" cellspacing="0" width="100%">
        <thead>
            <tr>
                <th>Name</th>
                <th>Gender</th>
            </tr>
        </thead>
        <tfoot>
            <tr>
                <th>Name</th>
                <th>Gender</th>
            </tr>
        </tfoot>        
        <tbody> 
        <?php
        while ($row = mysqli_fetch_array($res)) {
            $name = $row['name'];
            $gender = $row['gender'];
        ?>
            <tr>
                <td><?php echo $name;?></td>
                <td><?php echo $gender;?></td>                  
            </tr>
<?php
    }
?>
    </tbody>
</table>
<?php
mysqli_close($conn);
?>


<script>
$(document).ready(function() {
    $('#batchList').DataTable();
} );
</script>

【问题讨论】:

  • 什么数据库,什么表结构,样本数据
  • 您是否了解“男性”是“女性”的最后四个字母,因此这取决于您的搜索代码究竟是如何工作的?所以请出示该代码。
  • 是的,我明白这一点。我不能给你搜索发生的部分,因为它是来自数据表的 jquery,而且它不是那么容易理解(至少对我来说)希望你理解我的观点@CodeCaster
  • 如果你不能显示你的代码,没有人可以回答这个问题。对于此列,您只需要完全匹配,而不是“LIKE”查询。没有任何代码就不能说更多。

标签: php search mysqli datatables


【解决方案1】:

如果您使用的是 ssp 类,下面的示例可能会很有用。

ssp.class.php -> 过滤方法

// Individual column filtering
    if ( isset( $request['columns'] ) ) {
        for ( $i=0, $ien=count($request['columns']) ; $i<$ien ; $i++ ) {
            $requestColumn = $request['columns'][$i];
            $columnIdx = array_search( $requestColumn['data'], $dtColumns );
            $column = $columns[ $columnIdx ];

            $str = $requestColumn['search']['value'];

            if ( $requestColumn['searchable'] == 'true' &&
             $str != '' ) {
                if(!empty($column['db'])){
                    if ($requestColumn['search']['regex'] == 'true') {
                        $binding = self::bind( $bindings, $str, PDO::PARAM_STR );
                        $columnSearch[] = "`".$column['db']."` = ".$binding;
                    } else {
                        $binding = self::bind( $bindings, '%'.$str.'%', PDO::PARAM_STR );
                        $columnSearch[] = "`".$column['db']."` LIKE ".$binding;
                    }
                }
            }
        }
    }

javascript DataTable 的配置对象

initComplete: function () {

                this.api().columns().every( function () {
                    var that = this;
                    var column = this;

                    $( 'input', this.footer() ).on( 'keyup change clear', function () {
                        if ( that.search() !== this.value ) {
                            that
                                .search( this.value )
                                .draw();
                        }
                    } );

                    $( 'select', this.footer() ).on( 'change clear', function () {

                        if ( that.search() !== this.value ) {
                            if (this.value === '') {
                                that
                                    .search( this.value )
                                    .draw();
                            } else {
                                that
                                    .search( this.value, true, false)
                                    .draw();
                            }
                        }
                    } );

                } );
            },

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-06-11
    • 1970-01-01
    • 1970-01-01
    • 2017-10-08
    • 1970-01-01
    • 2021-01-13
    • 2013-09-01
    • 1970-01-01
    相关资源
    最近更新 更多