Solved

JQuery DataTables : Can I Bind to an Associative Object

Posted on 2014-10-27
3
1,005 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 500 total points
ID: 40406489
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
ID: 40406533
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
ID: 40410274
Many thanks Robert. A very nice solution.

Kind Regards,
Adrian
0

Featured Post

Linux Academy Android App Now Supports Chromecast

We have some fantastic news for our Android fans. We’re so excited to announce that the Linux Academy Android app is now available with Chromecast support. That’s right – simply download the latest update of the Linux Academy App and start casting your favorite course videos!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
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)
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…
Suggested Courses

623 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