Solved

JQuery DataTables : Can I Bind to an Associative Object

Posted on 2014-10-27
3
777 Views
Last Modified: 2014-10-29
Below is a standalone HTML page with two jQuery datatables. The first example works and binds the JavaScript object to the table. However, I have a dataset of unique values (like an associative array) and I want to bind this as shown in example 2. Does anybody have any ideas how I could achieve this?


<html>
    <head>
        <title>DataTables With Multi Dimensional Objects</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link href="//cdn.datatables.net/1.10.3/css/jquery.dataTables.css" rel="stylesheet" type="text/css"/>

        <script type="text/javascript" language="javascript" src="//code.jquery.com/jquery-1.11.1.min.js"></script>
        <script type="text/javascript" language="javascript" src="//cdn.datatables.net/1.10.3/js/jquery.dataTables.min.js"></script>

        <script>

            $(document).ready(function () {

                var dataSet1 = [
                    {"name": "jim", "age": 3, "next": 4},
                    {"name": "bill", "age": 5, "next": 6}
                ];

                $('#example1').dataTable({
                    "data": dataSet1,
                    "columns": [
                        {"data": "name"},
                        {"data": "age"},
                        {"data": "next"}
                    ]
                });

                var dataSet2 = {
                    "jim": {"age": 3, "next": 4},
                    "bill": {"age": 5, "next": 6}
                };

                //This doesn't work. I can't think how to bind the dataset name to the columns

                $('#example2').dataTable({
                    "data": dataSet2,
                    "columns": [
                        {"data": "name"},
                        {"data": "age"},
                        {"data": "next"}
                    ]
                });
            });

        </script>

    </head>
    <body>

        <!-- EXAMPLE 1 -->
        <table id="example1" class="display" cellspacing="0" width="100%">
            <thead>
                <tr>
                    <th>Name</th>
                    <th>Age</th>
                    <th>Next</th>
                </tr>
            </thead>
        </table>

        <!-- EXAMPLE 2 -->
        <table id="example2" class="display" cellspacing="0" width="100%">
            <thead>
                <tr>
                    <th>Name</th>
                    <th>Age</th>
                    <th>Next</th>
                </tr>
            </thead>
        </table>

    </body>
</html>

Open in new window

0
Comment
Question by:AdrianSmithUK
  • 2
3 Comments
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 500 total points
Comment Utility
Well it seems to me that the possibilities described in the documentation about nested objects don't include your situation, your data is just too different.

How do you feel about using a little conversion function?
        <script>

            $(document).ready(function () {

                var dataSet1 = [
                    {"name": "jim", "age": 3, "next": 4},
                    {"name": "bill", "age": 5, "next": 6}
                ];

                $('#example1').dataTable({
                    "data": dataSet1,
                    "columns": [
                        {"data": "name"},
                        {"data": "age"},
                        {"data": "next"}
                    ]
                });

                var dataSet2 = {
                    "jim": {"age": 3, "next": 4},
                    "bill": {"age": 5, "next": 6}
                };

                $('#example2').dataTable({
                    "data": convertData(dataSet2),
                    "columns": [
                        {"data": "name"},
                        {"data": "age"},
                        {"data": "next"}
                    ]
                });
            });

            function convertData(d) {
                var o = [];
                $.each(d, function(idx, obj) {
                    o.push($.extend(obj, {name: idx}));
                });
                return o;
            }

        </script>

Open in new window

0
 

Author Comment

by:AdrianSmithUK
Comment Utility
Hi Robert

I think this is going to be the only solution.

I've opened a forum question:

http://datatables.net/forums/discussion/24244/how-to-bind-to-an-associative-object#latest

But if nobody comes up with anything better I'll select your solution and close the question.

Many Thanks,
Adrian
0
 

Author Closing Comment

by:AdrianSmithUK
Comment Utility
Many thanks Robert. A very nice solution.

Kind Regards,
Adrian
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

What is a Lightbox? A Lightbox is the effect you see when you click, for example, an image and the screen fades out and up pops the same image but in its full size dimensions. There are lots of Lightbox effects for jQuery. Problem is they are a…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now