Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

JQuery DataTables : Can I Bind to an Associative Object

Posted on 2014-10-27
3
Medium Priority
?
1,080 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 2000 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
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 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

718 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