Google have created an awesome visualization API. It enables you to add live charts, graphs and tables to your web pages. It is quite flexible and has some great features that are quite easy to use once you get started. I started playing around with this API and decided to build a datatable using ASP.net MVC and the visualization API.
Click here to view a simple demo.
Okay, let's break this code down to get an idea of what's going on.
1. Initially we are loading the Google Visualization API and instructing it to use the tables package.
2. This is the location of our MVC Controller that we want to post to.
3. Add the Column names and data types.
4. Loop through all the results that we got back and set the data to the appropriate column.
5. We are adding a Number Formatter to the column. If the number is negative, we change the column to red. We are also prepending a currency symbol in front of the value. This is an optional feature that you can use to jazz up your table.
6. This is another Column Formatter, but this time we are adding a colour to the column if it falls between certain range. Again, this is optional.
7. Finally, the paging is being enabled and the paging size is being set to 2. The reason I am setting it so low is because I am only returning a small amount of data for this demo. You may want to set this higher depending on your preferences.
We have one last thing to do, and this is just to add a div to our view that will contain the table and its data.
And that's it!
The great advantages of using this API to help construct your tables is that it has features like Column formatting, paging and sorting, as well as the ability to totally manipulate the look and feel of your table. For more information on column formatting and paging please refer to the documentation. The API offers some additional features such as the ability to add Bar Formatters and Arrow Formatters.
I've created a small solution based on this tutorial.
For further reading on this API, please take a look at the following links.
Add your comment