{"action":"created","issue":{"url":"https://api.github.com/repos/DataTables/DataTablesSrc/issues/78","repository_url":"https://api.github.com/repos/DataTables/DataTablesSrc","labels_url":"https://api.github.com/repos/DataTables/DataTablesSrc/issues/78/labels{/name}","comments_url":"https://api.github.com/repos/DataTables/DataTablesSrc/issues/78/comments","events_url":"https://api.github.com/repos/DataTables/DataTablesSrc/issues/78/events","html_url":"https://github.com/DataTables/DataTablesSrc/pull/78","id":162343575,"number":78,"title":"Support column ids in order","user":{"login":"shmax","id":773172,"avatar_url":"https://avatars.githubusercontent.com/u/773172?v=3","gravatar_id":"","url":"https://api.github.com/users/shmax","html_url":"https://github.com/shmax","followers_url":"https://api.github.com/users/shmax/followers","following_url":"https://api.github.com/users/shmax/following{/other_user}","gists_url":"https://api.github.com/users/shmax/gists{/gist_id}","starred_url":"https://api.github.com/users/shmax/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/shmax/subscriptions","organizations_url":"https://api.github.com/users/shmax/orgs","repos_url":"https://api.github.com/users/shmax/repos","events_url":"https://api.github.com/users/shmax/events{/privacy}","received_events_url":"https://api.github.com/users/shmax/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":8,"created_at":"2016-06-26T19:42:06Z","updated_at":"2016-06-29T14:47:36Z","closed_at":null,"pull_request":{"url":"https://api.github.com/repos/DataTables/DataTablesSrc/pulls/78","html_url":"https://github.com/DataTables/DataTablesSrc/pull/78","diff_url":"https://github.com/DataTables/DataTablesSrc/pull/78.diff","patch_url":"https://github.com/DataTables/DataTablesSrc/pull/78.patch"},"body":"Adds support for sorting on column identifiers, as discussed in https://github.com/DataTables/DataTables/issues/837\r\n\r\nExample usage:\r\n```\r\n $(document).ready( function() {\r\n      $('#example').dataTable( {\r\n        orderByName:true,\r\n        order:[[\"userId\", \"desc\"]],\r\n        columns:[\r\n        \t{\r\n        \t\tname:\"userId\",\r\n\t      \t}\r\n        ]\r\n      });\r\n    });\r\n```\r\n\r\nAlternatively, you can key off of  ```column.data``` (if it's a string), or a ```<th>``` element's ```id``` attribute.\r\n\r\n```\r\n<table id=\"example\">\r\n    <thead>\r\n        <tr>\r\n            <th id=\"userId\">User Id</th>\r\n            <th id=\"date\">Date</th>\r\n        </tr>\r\n    </thead>\r\n</table>\r\n<script>\r\n $(document).ready( function() {\r\n      $('#example').dataTable( {\r\n        serverSide:true,\r\n        ajax:\"/path/to/endpoint\",\r\n        orderByName:true,\r\n        order:[[\"userId\", \"desc\"]],\r\n      });\r\n    });\r\n</script>\r\n```\r\n\r\nChanges:\r\n\r\n* added new ```orderByName``` configuration option, ```false``` by default. If ```false```, everything works the way it used to. If ```true```, conversions between column identifier and column index will happen as necessary.\r\n* added two new functions: ```_fnColumnIndexToColumnId``` and ```_fnColumnIdToColumnIndex```\r\n* changed the default for ```aaSorting``` to empty array (because I can't convert the \"0\" that used to be there until after we have ```oSettings``` initialized), but I initialize it to the old defaults later in the process\r\n* fixed some PHP warnings (I'm on 7.0, and it doesn't like class-name-as-constructor-name. The changes I made are compatible with 5.4)\r\n* removed some unused variables\r\n\r\n\r\nI tested tables from markup, from AJAX, with\\without ```columns```, with\\without ```columns.orderData```, with\\without ```order``` and so on. Did I miss anything?\r\n\r\nAlso, you mentioned something about this possibly conflicting with extensions. If you point me at the ones you had in mind I can investigate."},"comment":{"url":"https://api.github.com/repos/DataTables/DataTablesSrc/issues/comments/229379561","html_url":"https://github.com/DataTables/DataTablesSrc/pull/78#issuecomment-229379561","issue_url":"https://api.github.com/repos/DataTables/DataTablesSrc/issues/78","id":229379561,"user":{"login":"shmax","id":773172,"avatar_url":"https://avatars.githubusercontent.com/u/773172?v=3","gravatar_id":"","url":"https://api.github.com/users/shmax","html_url":"https://github.com/shmax","followers_url":"https://api.github.com/users/shmax/followers","following_url":"https://api.github.com/users/shmax/following{/other_user}","gists_url":"https://api.github.com/users/shmax/gists{/gist_id}","starred_url":"https://api.github.com/users/shmax/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/shmax/subscriptions","organizations_url":"https://api.github.com/users/shmax/orgs","repos_url":"https://api.github.com/users/shmax/repos","events_url":"https://api.github.com/users/shmax/events{/privacy}","received_events_url":"https://api.github.com/users/shmax/received_events","type":"User","site_admin":false},"created_at":"2016-06-29T14:47:36Z","updated_at":"2016-06-29T14:47:36Z","body":"> In the case of the name for the sort you are still assuming that the API is going to know to look in order[ i ][ name ] to get the column name - which might be true of the API you are using but certainly isn't going to be universal.\r\n\r\nNot at all. Again, I'm not in a position to let DataTables talk directly to my back end. By including ```type``` in ```data.order``` I am simply removing the need to do the work of converting physical column indices to logical ids in my ```ajax``` method. Here's what my ```ajax``` method currently looks like:\r\n\r\n```\r\najax:function(data, callback, settings) {\r\n\t\t\t\t\t\tutil.ajax(\"/api/Actions/find\", {\r\n\t\t\t\t\t\t\ttype:\"GET\",\r\n\t\t\t\t\t\t\tdata:{\r\n\t\t\t\t\t\t\t\tpagination:{\r\n\t\t\t\t\t\t\t\t\toffset:data.start,\r\n\t\t\t\t\t\t\t\t\tlimit:data.length\r\n\t\t\t\t\t\t\t\t},\r\n                                                                // I only have to do the map operation here because if \r\n                                                                // I sent the order array as-is I would get a validation\r\n                                                                // error for the unexpected 'column' property\r\n\t\t\t\t\t\t\t\tsort:data.order.map(function(sort, idx, arr){\r\n\t\t\t\t\t\t\t\t\treturn {\r\n\t\t\t\t\t\t\t\t\t\ttype: sort.type,\r\n\t\t\t\t\t\t\t\t\t\tdir: sort.dir\r\n\t\t\t\t\t\t\t\t\t};\r\n\t\t\t\t\t\t\t\t})\r\n\t\t\t\t\t\t\t},\r\n\t\t\t\t\t\t\tdataType: \"json\",\r\n\t\t\t\t\t\t\tsuccess: function(response){\r\n\t\t\t\t\t\t\t\tcallback({\r\n\t\t\t\t\t\t\t\t\tdata:response.items,\r\n\t\t\t\t\t\t\t\t\trecordsTotal:response.pagination.totalItems,\r\n\t\t\t\t\t\t\t\t\trecordsFiltered:response.pagination.totalItems\r\n\t\t\t\t\t\t\t\t});\r\n\t\t\t\t\t\t\t}\r\n\t\t\t\t\t\t});\r\n\t\t\t\t\t},\r\n```\r\n\r\nSo people who are using an ```ajax``` method like me will have it a little easier, and people who are writing a bespoke back end are already prepared to consume what DataTables is sending (and this will relieve them of the need to convert, as well).\r\n\r\n> the \"as a courtesy\" doesn't feel strong enough for me to ship this baked into DataTables\r\n\r\nPreaching to the choir. I did advise against it! I did it this way at my last company because we were writing in-house tools and everybody was on FF or Chrome, and it made all the conversions I had to do more straightforward. Just thought I'd show you in case you have some idea percolating to release a new major version some day that officially drops support for older browsers (like jQuery did not too long ago). I'm not hung up on it.\r\n\r\n> I'm reluctant to add redundant information since that just increases the payload size\r\nDo you actually know how many people let DataTables talk directly to the back end? It's just not something I can see happening on a serious, professional scale, and I suspect it's something done by small, mom-and-pop grade projects. if you're serious about APIs, you're going to want to validate the input, and I can't see anybody wanting to take on the work of validating all the information that DataTables wants to send along. You would have to write a really long, complex schema, and its documentation would essentially just mirror DataTables's documentation. If I tried to pitch that to the back end team at my current company i would get a lecture about how important it is for the back end to not be bound to any specific front end, etc, etc.\r\n\r\nIn other words, to me--and I expect a lot of others--the ```data``` is *already* much too long and complex to be sending to the back end. Adding a friendly ```type``` property just makes ```ajax``` easier to write for us serious-API folks, and doesn't affect the people who are more hobbyist level..\r\n\r\nI can tell that my arguing isn't getting me anywhere. How about a different approach: what about exposing an API method for doing the conversion? Or could an extension do all this?\r\n\r\nMeanwhile, I'm eager to start using my own changes, but I'm still not clear on how one consumes one's own fork without doing a build step. Any advice?"}}
{"actor":{"display_login":"shmax"}}