Link to home
Start Free TrialLog in
Avatar of AdrianSmithUK
AdrianSmithUK

asked on

JQuery DataTables : Can I Bind to an Associative Object

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

ASKER CERTIFIED SOLUTION
Avatar of Robert Schutt
Robert Schutt
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of AdrianSmithUK
AdrianSmithUK

ASKER

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
Many thanks Robert. A very nice solution.

Kind Regards,
Adrian