数据表过滤器下拉列表未正确显示

时间:2020-08-07 11:21:02

标签: javascript php jquery sql datatables

我正在尝试为我的每一列显示过滤器下拉列表。一些列正确显示了unqiue值。但是,我的过滤条件下拉菜单中有3个显示不正确。在这3列中,我在<td>中包括了隐藏的输入字段,因为我使用JS来检查来自隐藏的输入的数据值是否在一定范围内,所以这是必需的。 <td>将根据条件改变颜色。但是,这导致我的3过滤器下拉列表显示隐藏输入的值,这不是我希望的。可以帮忙吗?下面是我的代码。

PHP代码:

    <table id="pic_table" class="table" class="display">
                        <thead>
                            <tr>
                                <th class="filterhead"></th>
                                <th class="filterhead"></th>
                                <th class="filterhead"></th>
                                <th class="filterhead"></th>
                                <th class="filterhead"></th>
                                <th class="filterhead"></th>
                                <th class="filterhead"></th>
                            </tr>
                            <tr>
                                <th>Serial</th>
                                <th>Name</th>
                                <th>Project Reference</th>
                                <th>Basic Profile</th>
                                <th>Employment Permits</th>                 
                                <th>Last Updated</th>
                                <th>Status</th>
                            </tr>
                        </thead>
                        <tbody>
                <?php
                  .....................

                     while($row = sqlsrv_fetch_array( $sql_stmt, SQLSRV_FETCH_NUMERIC)){
        
                            echo"<tr>";
                            echo "<td>".$row[0]."</td>";
                            echo "<td class='name_col'>".$row[1]."</td>";
                            echo "<td class='prbk'><input type='hidden' class='pr' name='pr' value='".$row[3]."'>".$row[3]."%</td>";
                            echo "<td class='bpbk'><input type='hidden' class='bp' name='bp' value='".$row[4]."'>".$row[4]."%</td>";
                            echo "<td class='epbk'><input type='hidden' class='ep' name='ep' value='".$row[5]."'>".$row[5]."%</td>";                    
                            echo "<td>".$row[2]->format("d M Y")."</td>";
                            echo "<td id='status'></td>";
                            echo "</tr>";    
        
                  }
             ?>
</tbody>
</table>

JS

 $(document).ready( function () {              
          $('#pic_table').DataTable({
        
                initComplete: function () {
        
                        var i = 0;
                                this.api().columns().every( function () {
                                    var column = this;
                                    var select = $('<select><option value="">All</option></select>')
                                        .appendTo( $('.filterhead').eq(i).empty() )
                                        .on( 'change', function () {
                                            var val = $.fn.dataTable.util.escapeRegex(
                                                $(this).val()
                                            );
                         
                                            column
                                                .search( val ? '^'+val+'$' : '', true, false )
                                                .draw();
                                        } );
                         
                                    column.data().unique().sort().each( function ( d, j ) {
                                        select.append( '<option value="'+d+'">'+d+'</option>' )
                                    } );
                                    i++;
                                } );        
                }
                
            });
    });

错误屏幕截图:

enter image description here

1 个答案:

答案 0 :(得分:0)

感谢jameson2012。根据您的建议,我通过用for中的data属性替换隐藏的输入字段来使其工作。下面是我的简单解决方案。

<td>