I have to order a table, but the numbers are coming in string and not int?

I have to order a table, but the numbers are coming in string and not int?

ragseragse Posts: 4Questions: 1Answers: 0

when i try to order the list looks this

1
10
11
12
2
20
21
22
3
30

and should loooks like this

1
2
3
10
11

and so long...

This question has accepted answers - jump to:

Answers

  • ragseragse Posts: 4Questions: 1Answers: 0

    I hope you guys can help, bc im really stuck with this is what I try but is not working

        this.dtOptions = {
            destroy: true,
            displayLength: 50,
            paging: false,
            order: [],
            language: {
                url: './assets/json/datatable.spanish.json'
            },
            columnDefs: [{'orderable': true, 'targets': 'sort'}]
        };
    
  • colincolin Posts: 15,237Questions: 1Answers: 2,599
    Answer ✓

    DataTables tries to work out the type of the data, and apply the ordering appropriately. Your numbers are being displayed as strings - so that would suggest there's a non-numeric value in that column somewhere. You can force it to be numeric with columns.type set to num,

    Colin

  • ragseragse Posts: 4Questions: 1Answers: 0

    they are coming from the database like strings, like this: '1', '2', '3'

  • tangerinetangerine Posts: 3,365Questions: 39Answers: 395
    Answer ✓

    You can force it to be numeric with columns.type set to num,

    Did you do that?

  • ragseragse Posts: 4Questions: 1Answers: 0

    I try to do that but it doesnt work

  • kthorngrenkthorngren Posts: 21,171Questions: 26Answers: 4,922

    Typically Datatables will take care of this for you through its type detection operation. Generally when the sorting behavior is not as expected there is otehr types of data in the column. Do you have null or some other type in the data? Can you provide a link to your page or a test case replicating the issue so we can take a look? If not maybe you can capture a debugger image for the developers to look at.
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

This discussion has been closed.