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

    « Nancy and jtable: paging and sortingSquishIt and Nancy »
    comments

    Introduction.

    From time to time you need to show data in a grid. And I picked jtable and Nancy to make that happen. Because reinventing the wheel is not something you want to do every week.

    Installation

    Everything you need can be found on Nuget. And this is what I needed.

    • jQuery
    • jQuery.UI.Combined
    • jTable
    • Nancy
    • Nancy.Hosting.Aspnet
    • Nancy.Viewengines.Razor

    I then copied all the scripts to the content folder, because that is the convention for Nancy. It is kind of sad that the jtable has no dependency on jquery or jqueryUI but I survived.

    Our first grid

    So now we need to get our first grid to fill up with some data.

    First we need a model.

    1. namespace NancyJTable.Models
    2. {
    3.     public class PlantModel
    4.     {
    5.         public int Id { get; set; }
    6.         public string Name { get; set; }
    7.         public string Genus { get; set; }
    8.         public string Species { get; set; }
    9.     }
    10. }

    The above is for our data.

    And we will need another model.

    1. using System.Collections.Generic;
    2.  
    3. namespace NancyJTable.Models
    4. {
    5.     public class PlantsModel
    6.     {
    7.         public string Result { get; set; }
    8.         public IList<PlantModel> Records { get; set; }
    9.     }
    10. }

    We need this model so we can send this data as json to our jtable grid. jTable uses ajax to get the data and the above will make it easy for it to know what to put where.

    Then I guess we need a module.

    1. using System.Collections.Generic;
    2. using Nancy;
    3. using Nancy.ModelBinding;
    4. using NancyJTable.Models;
    5.  
    6. namespace NancyJTable.Modules
    7. {
    8.     public class PlantsModule:NancyModule
    9.     {
    10.  
    11.         public PlantsModule()
    12.         {
    13.             Get["/"] = parameters => View["Plants"];
    14.             Post["/plants"] = parameters =>
    15.                 {
    16.                     var plantsModel = new PlantsModel
    17.                         {
    18.                             Result = "OK",
    19.                             Records = new List<PlantModel>(),
    20.                         };
    21.                     for (var i = 1; i < 25; i++)
    22.                     {
    23.                         plantsModel.Records.Add(new PlantModel()
    24.                             {
    25.                                 Id = i,
    26.                                 Name = "name" + i,
    27.                                 Genus = "genus" + i,
    28.                                 Species = "Species" + i
    29.                             });
    30.                     }
    31.                     return Response.AsJson(plantsModel);
    32.                 };
    33.         }
    34.     }
    35. }

    In the above we have a Get to show our page and a Post for jtable to get it's data in JSON format. A bit strange to use a POST for that but hey who am I to complain.

    And last but not least I need a View.

    1. @using Nancy
    2. <!DOCTYPE html>
    3.  
    4. <html>
    5.     <head>
    6.         <title>Plants</title>
    7.         <link href="@Url.Content("~/Content/Scripts/jtable/themes/metro/blue/jtable.min.css")" rel="stylesheet" type="text/css" />
    8.         <script src="@Url.Content("~/Content/Scripts/jquery-1.9.0.min.js")" type="text/javascript"></script>
    9.         <script src="@Url.Content("~/Content/Scripts/jquery-ui-1.10.0.min.js")" type="text/javascript"></script>
    10.         <script src="@Url.Content("~/Content/Scripts/jtable/jquery.jtable.min.js")" type="text/javascript"></script>
    11.         <script type="text/javascript">
    12.             $(document).ready(function () {
    13.                 $('#PlantsTableContainer').jtable({
    14.                     title: 'Table of plants',
    15.                     actions: {
    16.                         listAction: '/plants',
    17.                     },
    18.                     fields: {
    19.                         Id: {
    20.                             key: true,
    21.                             list: true,
    22.                             width: '10%'
    23.                         },
    24.                         Name: {
    25.                             title: 'Name',
    26.                             width: '30%'
    27.                         },
    28.                         Genus: {
    29.                             title: 'Genus',
    30.                             width: '30%'
    31.                         },
    32.                         Species: {
    33.                             title: 'Species',
    34.                             width: '30%'
    35.                         }
    36.                     }
    37.                 });
    38.                 $('#PlantsTableContainer').jtable('load');
    39.             });
    40. </script>
    41.     </head>
    42.     <body>
    43.         <div>
    44.             <div id="PlantsTableContainer"></div>
    45.         </div>
    46.     </body>
    47. </html>

    The listAction is the url where jtable can get it's list. Then you define the different columns and then you do a load so that the data gets filled.

    And you need a div somewhere with an id that you use in the javascript. In my case PlantsTableContainer.

    And here is the result of all that hard work.

    Conclusion

    The fact that you need a Post to Get the data is a bit of a weird choice but it becomes clear why they did that when we learn how to do paging and sorting in our next post.

    About the Author

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

    1 comment

    Comment from: Halil İbrahim Kalkan [Visitor] · http://www.halilibrahimkalkan.com
    Halil &#304;brahim Kalkan Hi,

    I'm the author of jTable. I'm very glad to see such a tutorial.
    I put links to this posts on jTable.org: http://jtable.org/Home/Documents
    Thanks a lot for this good tutorials ;)
    01/27/13 @ 14:30

    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.)