How do you change the style of cell in a JQuery.Da

2019-03-14 07:56发布

I have a question about setting the style attributes for a data cell in the jQuery.DataTable. I was able to set the width for each column when initializing the dataTable using the following code:

oTable = $('#example').dataTable( {
    "aoColumns" : [ 
        { sWidth: '40%' }, 
        { sWidth: '60%' }
    ]
} );

Now I want to change the alignment for the second column like so: style="text-align: right;".

I am adding rows dynamically using this code:

/* Global var for counter */
var giCount = 2;

function fnClickAddRow() {
    oTable.fnAddData( [
        'col_1', 
        'col_2' ] );

    giCount++;  
}

Can you tell me how can I select the second cell of the new row after it's been inserted OR how to set the style of the row before/during insertion?

Any help would be greatly appreciated!

5条回答
爷的心禁止访问
2楼-- · 2019-03-14 08:10
$('#tblAssignment tr td:nth-child(1)').addClass('rightaligned');
查看更多
太酷不给撩
3楼-- · 2019-03-14 08:13

This is the code that worked for me:

<style>
    #tableExample .classDataTable { font-size: 20px; }
</style>

oTable = $('#tableExample').dataTable( {  
    "aoColumns" : [   
        { sWidth: '40%' },   
        { sClass: "classDataTable" }  
    ]   } );
查看更多
地球回转人心会变
4楼-- · 2019-03-14 08:26

you can also use something like that to another kind of customizations : inside fnRender you can insert label, span, and set class or style of the element inside this "td"

"aoColumns": [
                    { "sTitle": "Ativo","sClass": "center","bSearchable": true,
                        "fnRender": function(obj) {
                            var sReturn = obj.aData[ obj.iDataColumn ];
                            return "<a href=\"/"+sReturn.toLowerCase()+"\" class=\"tag\">/"+sReturn.toLowerCase()+"</a>";
                        }                   
                    },
查看更多
倾城 Initia
5楼-- · 2019-03-14 08:30

Cool, I am happy to report that I was able to answer my own question! I just defined a CSS style (alignRight), and added the style to the column like so:

<style media="all" type="text/css">
    .alignRight { text-align: right; }
</style>

oTable = $('#example').dataTable( {  
    "aoColumns" : [   
        { sWidth: '40%' },   
        { sWidth: '60%', sClass: "alignRight" }  
    ]   } );
查看更多
手持菜刀,她持情操
6楼-- · 2019-03-14 08:36

Quick and easy way would be to add an nth-child class for the table. So in your case:

#example td:nth-child(2) {
    text-align: right;
}
查看更多
登录 后发表回答