In this post we will see how to show our JSON data in a client side grid. Here we are going to use jquery.columns grid, which is light weight and pretty easy to use. For the demo purpose we will create a JSON dynamically in client side and format it as a grid. I hope you will like this.
Using the code
First of all you need to download the needed files from here.
Then add reference to your page.
- <link href="css/classic.css" rel="stylesheet" />
- <script src="js/jquery.min.js"></script>
- <script src="js/jquery.columns-1.0.min.js"></script>
Creating a JSON dynamically
You can create a JSON array as follows.
- var myData = new Array();
- var firstNames = ["Andrew", "Nancy", "Shelley", "Regina", "Yoshi", "Antoni", "Mayumi", "Ian", "Peter", "Lars", "Petra", "Martin", "Sven", "Elio", "Beate", "Cheryl", "Michael", "Guylene"];
- var lastNames = ["Fuller", "Davolio", "Burke", "Murphy", "Nagase", "Saavedra", "Ohno", "Devling", "Wilson", "Peterson", "Winkler", "Bein", "Petersen", "Rossi", "Vileid", "Saylor", "Bjorn", "Nodier"];
- var productNames = ["Black Tea", "Green Tea", "Caffe Espresso", "Doubleshot Espresso", "Caffe Latte", "White Chocolate Mocha", "Cramel Latte", "Caffe Americano", "Cappuccino", "Espresso Truffle", "Espresso con Panna", "Peppermint Mocha Twist"];
- var priceValues = ["2.25", "1.5", "3.0", "3.3", "4.5", "3.6", "3.8", "2.5", "5.0", "1.75", "3.25", "4.0"];
- function createData(num)
- {
- myData = [];
- for (var i = 0; i < num; i++)
- {
- var row = {};
- var productindex = Math.floor(Math.random() * productNames.length);
- var price = parseFloat(priceValues[productindex]);
- var quantity = 1 + Math.round(Math.random() * 10);
- row["firstname"] = firstNames[Math.floor(Math.random() * firstNames.length)];
- row["lastname"] = lastNames[Math.floor(Math.random() * lastNames.length)];
- row["productname"] = productNames[productindex];
- row["price"] = price;
- row["quantity"] = quantity;
- row["total"] = price * quantity;
- myData[i] = row;
- }
- }
Here the function createData will create a JSON array.
Next we need to create a table and give this array as a data source to the grid.
- <table id="dataGrid" class="display" width="100%"></table>
- $(function ()
- {
- createData(25); $('#dataGrid').columns
- ({
- data: myData
- });
- });
Pretty simple, right?
Complete code
- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <title>Convert JSON Data Into Data Grid Columns</title>
- <link href="css/classic.css" rel="stylesheet" />
- <script src="js/jquery.min.js"></script>
- <script src="js/jquery.columns-1.0.min.js"></script>
- <script>
- var myData = new Array();
- var firstNames = ["Andrew", "Nancy", "Shelley", "Regina", "Yoshi", "Antoni", "Mayumi", "Ian", "Peter", "Lars", "Petra", "Martin", "Sven", "Elio", "Beate", "Cheryl", "Michael", "Guylene"];
- var lastNames = ["Fuller", "Davolio", "Burke", "Murphy", "Nagase", "Saavedra", "Ohno", "Devling", "Wilson", "Peterson", "Winkler", "Bein", "Petersen", "Rossi", "Vileid", "Saylor", "Bjorn", "Nodier"];
- var productNames = ["Black Tea", "Green Tea", "Caffe Espresso", "Doubleshot Espresso", "Caffe Latte", "White Chocolate Mocha", "Cramel Latte", "Caffe Americano", "Cappuccino", "Espresso Truffle", "Espresso con Panna", "Peppermint Mocha Twist"];
- var priceValues = ["2.25", "1.5", "3.0", "3.3", "4.5", "3.6", "3.8", "2.5", "5.0", "1.75", "3.25", "4.0"];
- $(function()
- {
- createData(25);
- $('#dataGrid').columns(
- {
- data: myData
- });
- });
- function createData(num)
- {
- myData = [];
- for (var i = 0; i < num; i++)
- {
- var row = {};
- var productindex = Math.floor(Math.random() * productNames.length);
- var price = parseFloat(priceValues[productindex]);
- var quantity = 1 + Math.round(Math.random() * 10);
- row["firstname"] = firstNames[Math.floor(Math.random() * firstNames.length)];
- row["lastname"] = lastNames[Math.floor(Math.random() * lastNames.length)];
- row["productname"] = productNames[productindex];
- row["price"] = price;
- row["quantity"] = quantity;
- row["total"] = price * quantity;
- myData[i] = row;
- }
- }
- </script>
- </head>
- <body>
- <table id="dataGrid" class="display" width="100%"></table>
- </body>
- </html>
Output
If everything goes fine, you can see the following as an output in your page.
Conclusion
Did I miss anything that you may think is needed? Did you find this post useful? I hope you liked this article. Please share with me your valuable suggestions and feedback.
Your turn. What do you think?
A blog isn’t a blog without comments, but do try to stay on topic. If you have a question unrelated to this post, you’re better off posting it on C# Corner, Code Project, Stack Overflow, Asp.Net Forum instead of commenting here. Tweet or email me a link to your question there and I’ll definitely try to help if I can.


SenoritaPosted May 23, 2020, 3:35 AM
Hi ,its really helpful to beginner like me.
Sibeesh VenuPosted Apr 27, 2016, 7:43 AM
Gowtham Rajamanickam Thanks much
Gowtham RajamanickamPosted Apr 27, 2016, 7:17 AM
simple and good
Sibeesh VenuPosted Feb 5, 2016, 4:48 AM
Ankur Mistry Thank you
Ankur MistryPosted Feb 5, 2016, 3:57 AM
Simple and nice
Sibeesh VenuPosted Feb 4, 2016, 12:24 AM
Santhakumar Munuswamy Thanks much :)
Sibeesh VenuPosted Feb 4, 2016, 12:24 AM
Gagan Sikri Thanks much :)
Sibeesh VenuPosted Feb 4, 2016, 12:22 AM
Upendra Pratap Shahi Thanks much :)
Sibeesh VenuPosted Feb 4, 2016, 12:19 AM
Ankit Saxena Thanks much :)
Sibeesh VenuPosted Feb 4, 2016, 12:19 AM
Sthitaprajnya Debasis Thanks much :)
Santhakumar MunuswamyPosted Feb 3, 2016, 2:49 PM
Thanks for nice share
Gagan SikriPosted Feb 3, 2016, 1:36 PM
Nice One :)
Upendra Pratap ShahiPosted Feb 3, 2016, 12:19 PM
nice share sir...
Ankit SaxenaPosted Feb 3, 2016, 11:07 AM
Very nice explanation..
Sthitaprajnya Debasis NayakPosted Feb 3, 2016, 10:36 AM
Nice One !!!
Ace MaryPosted Feb 3, 2016, 8:21 AM
mapping issues with Ext.data.Model and nested JSON Data for ExtJS 4 Grid Store Hi, I'm struggling from 2 days to populate simple nested json data into the grid. I have gone through all possible forum posts without any luck Here is my code ExtJS Ver: 4.0.2a Data Model: Code: Ext.define('Episode', { extend: 'Ext.data.Model', idProperty: 'Id', fields: [ {name: 'AssetId', type: 'int'}, {name: 'Id', type: 'int'}, {name: 'Notes', type: 'string'}, {name: 'Number', type: 'int'}, {name: 'ProductionNumber', type: 'int'}, {name: 'SeasonId', mapping: 'Season.Id', type: 'int' }, {name: 'SeasonName', mapping: 'Season.Name', type: 'string' }, {name: 'SeasonDesc', mapping: 'Season.Description', type: 'string' }, {name: 'Title', type: 'string'} ] }); http://www.besanttechnologies.com/training-courses/php-training/php-training-institute-in-chennai | http://www.besanttechnologies.com/training-courses/cloud-computing-training/vmware-training-institute-in-chennai
Sibeesh VenuPosted Feb 3, 2016, 7:53 AM
Shubham Kumar Thank you
Shubham KumarPosted Feb 3, 2016, 7:10 AM
nice share sir
Sibeesh VenuPosted Feb 3, 2016, 7:07 AM
Shashangka Shekhar Thanks much :)
Shashangka ShekharPosted Feb 3, 2016, 7:03 AM
I like the way you finish "Your turn. What do you think?". Thanks, it was nice :)
Sibeesh VenuPosted Feb 3, 2016, 6:52 AM
Aqib Shehzad Thank you
Muhammad Aqib ShehzadPosted Feb 3, 2016, 6:49 AM
nice dear