Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi
I am trying to design my JQUERY DATATABLE for variable devices . So i want to hide some columns in my table . So for better Responsiveness and its advantages i choose BOOTSTRAP 3.0


Well i done everything but i am unable to FIND logic how to hide columns which are DYNAMICALLY generated under HEAD tags in my HTML table .

Here is my code for better understanding :

JavaScript
<script  type="text/javascript">



    $(document).ready(function () {

         $('#myDataTable').dataTable({

            "bProcessing": true,
            "bServerSide": true,
            "sAjaxSource": 'DataProvider',
            "bJQueryUI": true,
            "aoColumns": [
                         {
                             "sName": "ID"
                         },
                         { "sName": "COMPANY_NAME" },
                         { "sName": "ADDRESS" },
                         { "sName": "TOWN" }
            ]
            
        });
      

    });

</script>

}

<div class="table-responsive">

<table id="myDataTable" class="table">
                    <thead>

                         <tr>
                            <th>ID</th>
                            <th class="hidden-xs">Company name</th>
                            <th>Address</th>
                           <th>Town</th>
                        </tr>
                    
                    </thead>
                        
                    <tbody> 
                   </tbody>

                </table>


    </div>



 <style>
.table-responsive tr td:nth-child(2)  {
visibility:hidden;
background: red;
position:absolute;

}
.table-responsive tr th:nth-child(2) {
visibility:hidden;
position:absolute;
background: white;
}
table#myDataTable tbody td:nth-child(2) // I AM MAKING 2ND COLUMNS RED IF I INCORPORATE hidden-xs here while making column red means i guess will succeed but how to do it 
    {
        background: red;
    }

</style>  // added style tag with some css . i am able to display background color of respective column but i need to hide with bootstrap class="hidden-xs" and i donno how to 


CHROME INSPECT ELEMENT :

<tr class="odd" id="17"> //Class odd is used when id is odd likely even class for even id
<td class=" sorting_1">17</td>
<td class=""><a href="Home/Details/17">Emkay Entertainments</a></td> // yes i want to hide this 
<td class="">Nobel House, Regent Centre</td>
<td class="" title="Click to select town">Lothian</td>
</tr>


In the above code see you cant find the TD tags but when my code executes the plugin will get the data i.e under TH tags with respective TD's ..

What i tried : I know it wont work for TD'S STILL I tried so used css class hidden-xs on TH Tag when i reduce my browser size the TH tag is hiding but not the columns under it .

I NEED SUGGESTIONS TO HIDE 2ND OR WATEVER COLUMNS WHICH ARE DYNAMICALLY GENERATED . .

regards
Posted
Updated 18-Apr-14 10:02am
v4

1 solution

I am not sure of Bootstrap but you can simply apply the same class to second TD using the Javascripts after the table is created.

JavaScript
$(function() {
    $(".table-class tr").each(function() {
        $(this).find('td:eq(2)').addClass("hidden-xs");
    });
});
 
Share this answer
 
Comments
sunil gutta 18-Apr-14 14:39pm    
Hi ty for looking into .Actually TD'S are created dynamically right and i dont know how to use your above code especially where ? in my code context ? Thank you
EDIT : ADDED some more code which can actually make more clear to you . i captured the TD for one row in chrome . please look into it
Guruprasad.K.Basavaraju 18-Apr-14 15:55pm    
just add the below function after creating the column filter in your script..

$(function() {
$(".table tr").each(function() {
$(this).find('td:eq(2)').addClass("hidden-xs");
});
});
sunil gutta 18-Apr-14 16:00pm    
column filter ? i dont have that in my above . can you be more specific just becoz i suck big time
EDIT :once check my post i edited table#myDataTable in style tag which now working . once check details of it which i written some comments
Guruprasad.K.Basavaraju 18-Apr-14 16:10pm    
Sorry.. see below..

<script type="text/javascript">



$(document).ready(function () {

$('#myDataTable').dataTable({

"bProcessing": true,
"bServerSide": true,
"sAjaxSource": 'DataProvider',
"bJQueryUI": true,
"aoColumns": [
{
"sName": "ID"
},
{ "sName": "COMPANY_NAME" },
{ "sName": "ADDRESS" },
{ "sName": "TOWN" }
]

});


});

$(function() {
$(".table tr").each(function() {
$(this).find('td:eq(2)').addClass("hidden-xs");
});
});
</script>
sunil gutta 18-Apr-14 16:27pm    
tried but not working :( one thing i can say the above piece of code is perfect .

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900