Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate thead for data objects #13

Open
belg4mit opened this issue Jun 12, 2015 · 1 comment
Open

Generate thead for data objects #13

belg4mit opened this issue Jun 12, 2015 · 1 comment

Comments

@belg4mit
Copy link

If data: is an array of objects, use the object keys to create the table header for use in labeling, as well as sorting.

@belg4mit
Copy link
Author

Below is a patch to implement this behavior, although it may not be the most elegant approach. However, sorting does not seem to work. The column style changes when the header is clicked, but the table contents are not sorted to match. Filters do not function correctly either.

--- /tmp/datatable.js   2015-06-06 09:31:15.000000000 -0400
+++ datatable.js        2015-06-12 09:44:30.347206300 -0400
@@ -29,9 +29,17 @@

         var dataTable = this;

+        if ($.isArray(this.options.data)) {
+            this.data = this.options.data;
+        }
+ 
+
         if (this.table.find('thead').length === 0) {
             var head = $('<thead></thead>');
-            head.append(this.table.find('th').parent('tr'));
+            var row = head.append($("<tr/>"));
+           $.each(Object.keys(this.data[0]), function(colIndex, c) {
+              row.append($("<th/>").text(c));
+           });
             this.table.prepend(head);
         }

@@ -40,7 +48,7 @@
         }

         if ($.isArray(this.options.data)) {
-            this.data = this.options.data;
+            //this.data = this.options.data;
         }
         else if ($.isPlainObject(this.options.data)) {
             if (this.table.data('size')) {
@@ -973,7 +981,6 @@
                 var countTH = 0;

                 this.table.find('thead th').each(function () {
-
                     if ($(this).data('sort')) {
                         dataTable.options.sort = true;
                     }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants