Solved

JQuery DataTables : Can I Bind to an Associative Object

Posted on 2014-10-27
3
949 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Building a glossary into your website 9 60
javascript delete path 7 45
JQuery & Classic ASP 10 41
how to properly reinitialize tinyMCE rich text html editor 8 17
Requirements JQuery 1.6+ HTML CSS Introduction This article was inspired by an EE question (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28372511.html) on how to make a page show some balloons animate up a page…
There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
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

752 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