Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Web Developer

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

    Search

    XML Feeds

    Google Ads

    « Trying out AngularjsNancy and jquery datatables »
    comments

    Introduction

    Yesterday I started using jquery datatables and today I will play some more. I did this blogpost using jtable before. And now I'm gonna try the same thing and maybe a little more using datatables.

    This should be the result if everything goes well, which it never does from the first time, because Murphy is pairing with us.

    Isn't it purdy?

    Ok it isn't but it works.

    Adding a link

    Making a column linkable is as easy as it was in jtable.

    In our aoColumns definition we just use mRender which takes a function.

    1. {
    2.                         "mData": "Name", "sTitle": "Name", "sWidth": "30%", "bSortable": false,
    3.                         "mRender": function (data, type, row) {
    4.                             return '<a href="/plants/' + row.Id + '">' + data + '</a>';
    5.                         }
    6.                     }

    So data is the data for that cell, row is the object (in our case, could be an array too if your json datasource returns an array of arrays) and type is... not yet found a use for that.

    Formatting our dates with time

    As far as I can tell datatables has no way to format dates built in like jtable did. So we jump straight to moment.js.

    1. {
    2.                         "mData": "DateAdded", "sTitle": "Date added", "sWidth": "20%", "bSortable": false,
    3.                         "mRender": function (data, type, row) {
    4.                             return moment(data).format('DD/MM/YYYY HH:mm:ss');
    5.                         }
    6.                     }

    Don't forget to add the moment.js nuget and import the moment.js file.

    Color a row based on something

    In Excel they call this conditional formatting. In the example above I made all the rows lightcoral where the id was divisable by 3.

    My first attempt was this.

    1. "fnRowCallback": function( nRow, aData, iDisplayIndex, iDisplayIndexFull ) {
    2.                     if ( aData.Id % 3 == 0 )
    3.                     {
    4.                         $(nRow).css('background-color', 'lightcoral');
    5.                         $(nRow).css('color', 'white');
    6.                     }
    7.                 }

    And that is not the desired result because someone is overriding my nice formatting with their own on a td level, bad, bad people.

    But I can be mean too. And this will work.

    1. "fnRowCallback": function( nRow, aData, iDisplayIndex, iDisplayIndexFull ) {
    2.                     if ( aData.Id % 3 == 0 )
    3.                     {
    4.                         $('td', nRow).css('background-color', 'lightcoral');
    5.                         $('td', nRow).css('color', 'white');
    6.                     }
    7.                 }

    Conclusion

    It's fairly easy to bend the data to your will and format things as you desire.

    About the Author

    User bio imageChris is awesome.
    Social SitingsTwitterHomePageLTD RSS Feed
    c#, datatables, nancy
    InstapaperVote on HN

    No feedback yet

    Leave a comment


    Your email address will not be revealed on this site.

    To mislead the spambots.

    Your URL will be displayed.
    (Line breaks become <br />)
    (Name, email & website)
    (Allow users to contact you through a message form (your email will not be revealed.)