ThaiThai
ThaiThai

Reputation: 127

Datatable filter dropdown not showing correctly

I'm trying to display filter dropdown for each of my column. Some columns are displaying the unqiue value correctly. However, there are 3 of my filter dropdown not displaying correctly. In those 3 columns, I have included hidden input field within the <td> which is necessary as I'm using JS to check if the data value from the hidden input is within certain range. The <td> will change color based on a condition. However, this has caused my 3 filter drop down to show value from the hidden input which is not what i have hoped for. Can anyhow help? Below are my codes.

PHP code:

    <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++;
                                } );        
                }
                
            });
    });

Error Screenshot:

enter image description here

Upvotes: 0

Views: 415

Answers (1)

ThaiThai
ThaiThai

Reputation: 127

Thanks jameson2012. Based on your suggestion, i have made it work by replacing my hidden input field with data attribute in <td>. Below is my simple solution.

 echo "<td class='prbk' data-id='".$row[3]."'>".$row[3]."%</td>";
 echo "<td class='bpbk' data-id='".$row[4]."'>".$row[4]."%</td>";
 echo "<td class='epbk' data-id='".$row[5]."'>".$row[5]."%</td>";  

   

Upvotes: 0

Related Questions