How can I disable the "Auto-detected columns type"?
How can I disable the "Auto-detected columns type"?
Mat786786
Posts: 6Questions: 1Answers: 0
Link to test case:
Debugger code (debug.datatables.net):
Error messages shown:
Description of problem:
Answers
You can use
DataTable.type()
and set the function fordetect
to returnfalse
- e.g.There is more discussion about type detection and handling available in the manual.
Allan
Is that what you actually want to do, or do you just want to change a class name or something? You didn't exactly provide a lot of information about what you are trying to do
Allan
Thanks for replying Allan, i'm using jQuery in my project, and i just want to disable by default the auto detect columns type
Fair enough. My post above describes how to do that. You'd need to do it for each of the built in types.
Allan
So if i understand correctly i need to replace this function.
DataTable.type('num', {
className: 'dt-type-numeric',
detect: function ( d, settings )
{
var decimal = settings.oLanguage.sDecimal;
return _isNumber( d, decimal ) ? 'num' : null;
},
order: {
pre: function (d, s) {
var dp = s.oLanguage.sDecimal;
return __numericReplace( d, dp );
}
}
});
to this in datatables.js file?
DataTable.type('num', 'detect', () => false);
No, run the function I gave before you initialise DataTables. Don't modify the DataTables core file.
You'll need to run it for the other built in types as well, like I said.
Allan
It's not working for me, here is my code.
I'm not sure why it is outside the
script
?Sorry my mistake while copying my code in forum.
I have like this in my file:
<script type="text/javascript" language="javascript"›
$(document). ready(function){
DataTable.type('num', 'detect', () => false);
$(' table.datatable'). DataTable({
language:
"url": 'vendor/datatables/localisation/es_es.json'},
responsivé: true, fixedHeader: true, autoWidth: false, pagingType: 'simple', info: true, lengthMenu: [
[15, 50, 100, 300, 500, -1],
[15, 50, 100, 300, 500, "Todos"]
],
fixedHeader:
},
columnDefs: [
headerOffset: $('.navbar').outerHeight(),
targets: 'no-search', searchable: false,
targets: 'no-sort', orderable: false,
targets:
'sort_asc'
orderSequence: ['asc'],
targets:
'sort_desc'
orderSequence: ['desc'],
3);
3);
</script>
I built a test case and it doesn't seem to make the change:
https://live.datatables.net/sofuweve/1/edit
Kevin
That only removes the
num
type. The other types would need to be removed as well:https://live.datatables.net/sofuweve/2/edit
I still have no idea why one want to do that though.
Allan
I the test case
DataTable.type('num', 'detect', () => false);
doesn't seem to remove thenum
type as thedt-type-numeric
is still applied:Possibly I'm missing something
Kevin
Nevermind, looking at it in more detail I see that the Age column can match any of these:
https://live.datatables.net/sofuweve/4/edit
Once these are removed its no longer type detected.
Kevin
Yup, I reused the class name for a few of them
Hey allan,
This worked for me:
But now the problem is that the sorting is not working correctly with numeric columns
No, of course it isn't. Type detection has been disabled and DataTables won't detect numeric columns. It thinks everything is a string.
This is why I asked, back at the start, what it is that you are actually trying to do!
Could you clarify that please? Do you just want the numbers left aligned instead of default right aligned?
For that you could do:
https://live.datatables.net/yofivifi/1/edit
Allan
Hi Allan!
I think I can elaborate a bit on the why, as I'm facing the exact same issue currently and trying to figure out how to work around it. In my case, we're using Server Side Processing and the SQL is doing the sorting. When returning the data, previously, Datatables would just respect the order that the SSP gave. Since the 2.0 update, it's now seeming to take the data returned from SSP and then apply this secondary sorting.
So in our case, we have the data stored for an appointment in the database in YYYY-MM-DD HH:II:SS format, and the SQL query handles the ordering based on the order parameter that Datatables sends via AJAX. The data is returned though with a formatter that not only formats it US style (m/d/Y h:i a) but also adds some additional HTML with additional information from other columns (IE red text saying CANCELED if applicable).
I'm only just now starting to debug the issue and maybe the code snippet you supplied above to remove the different sorting classes will work and solve the issue, but just to clarify a use case, with SSP, where the front end sorting in fact unsorts the data.
Perhaps there can instead be an option added to turn this off with a true/false parameter?
@jfade
None of the client side processes are enabled when using server side processing. Take a look at this example. Use the browser's network inspector tool. You will see that the table is displayed in the order of the JSON response. You should see this too in your site. If not then please provide a link to a test case showing the issue.
https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case
If you are unable to provide a test case then post the JSON response and a screenshot of the table showing the difference in sorting.
Is this done client or server side?
Kevin
Hi Kevin,
Everything is server side. And because the data is returned differently in every case (every filter, every client's data being different) this bug is being exceedingly difficult to find consistently. I'll try to locate a consistent case and provide you with an example.
But I know for sure in one instance: I have a simple table in a modal popup with name, address, and a view button that's added in client side with the columnDefs. The Address field most definitely has a dt-type-numeric class added to it. This right aligns the header which isn't desired. I ASSUME it's doing some other sorting as well, but maybe it isn't, and it's just the styling applied. But this is new behavior that it never did before.
Thus, I may be wrong in my conclusion in blaming the client side for this, but I updated 7 sites to use Datatables 2.0 and after the update these sorting issues appeared intermittently in them, none of the server side logic was modified at all, I only updated the JS libs.
One last comment: I suspect this is applied based on the first pull of data, and then is not applied with subsequent draws. The address example, on some pages it's only text or entirely blank, and it's still applying the numeric sort class. But in another copy of the exact same tool, this numeric class is never applied at all. The only difference is the data returned.
OK, still not having the incorrect sort issue on this other project, but if you go here: https://demo.donorwrangler.com/donations.php
Click around on the headers to sort them, and at some stage you'll see the numeric class suddenly applied to the Date and Amount fields. The header will align right. In this example it doesn't mess with the sorting, the data is cleaner and not with a bunch of extra HTML, but if this class is being added from server side data wouldn't it be logical that it starts applying sorting rules too?
I get access denied clicking your test case link. But its probably not needed unless you can show the incorrect sorting issue.
The class is applied to the column when its sorted by Datatables type detection. See the types docs for details. Here is a simple example. Open the browser's console. Sort the Salary column and you will see the type detection taking place. That's when the
dt-type-numeric
class is applied to theth
and the formatting changes.https://live.datatables.net/yaroyala/15/edit
This is only the type detection process which is separate from sorting. Client side sorting will not happen when server side processing is enabled.
There is some inconsistency in that the class is only applied to the header and not the cells in the column. With client side processing the class is also applied to the table cells. See this example.
@allan can comment but I suspect the class shouldn't be applied at all with server side processing.
As a work around you can try either of Allan's suggestions above. I used the code to turn off detection:
https://live.datatables.net/xamafiwo/1/edit
Kevin
@kthorngren The demo is public, just click the login link and you'll be given the username/password to use.
I guess I'll just use the snippet to turn it off and see if it affects the other system (which is on an intranet so I can't link to it) and fixes the sorting issue. If so I'll try to get back to you with more info.