How Can I correct sort the column

How Can I correct sort the column

VolreconVolrecon Posts: 8Questions: 1Answers: 0

Description of problem:
Hello,

I have the problem with second column. I have to sort the column but sort doesnt work correcly.
How can i do this?

Columns are type "string".

Answers

  • kthorngrenkthorngren Posts: 21,327Questions: 26Answers: 4,949
    edited April 2021

    You can create a sorting plugin to custom sort that column. There may be a sorting plugin that might be close and help you get started. Take a look at natural, num-html and numString.

    Guessing you only want to sort on the first number you could use Orthogonal data and return the leading number (or other data point) for the sorting operation.

    Kevin

  • VolreconVolrecon Posts: 8Questions: 1Answers: 0
    edited April 2021

    I created data-order="4534523" attribute in the td. Is that work?
    Attribute was created in createdRow: function (row, data, dataIndex).

  • kthorngrenkthorngren Posts: 21,327Questions: 26Answers: 4,949

    If your table is sourced from the DOM then it should work according to this:
    https://datatables.net/manual/data/orthogonal-data#HTML-5

    Kevin

  • VolreconVolrecon Posts: 8Questions: 1Answers: 0

    thead is defined in HTLM but tbody is created from ajax API

    data-order doesnt work

  • kthorngrenkthorngren Posts: 21,327Questions: 26Answers: 4,949

    Thats correct. You can use columns.render. The example has if ( type === 'display' || type === 'filter' ). Instead use if ( type === 'order' ) .

    Kevin

  • VolreconVolrecon Posts: 8Questions: 1Answers: 0
                columnDefs: [
                    {
                        targets: 4,
                        render: function (data, type, row, meta) {
                            if (type == 'sort') {
                                const split = data.split(' ')
    
                                if (split[2] < 10) {
                                    split[2] = '0' + split[2]
                                }
    
                                if (split[4] < 10) {
                                    split[4] = '0' + split[4]
                                }
    
                                if (split[6] < 10) {
                                    split[6] = '0' + split[6]
                                }
    
                                const nr = split[0] + split[2] + split[4] + split[6]
    
                                console.log(parseInt(nr))
                                //row.children[4].setAttribute('title', nr)
                                //row.children[4].setAttribute('data-order', parseInt(nr))
    
                                return nr
                            } else {
                                return data
                            }
                        }
                    }
                ],
    

    The code doesnt work

  • kthorngrenkthorngren Posts: 21,327Questions: 26Answers: 4,949

    Can you provide a running test case so we can help you debug the code?
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

  • VolreconVolrecon Posts: 8Questions: 1Answers: 0

    I Will try send link tomorrow. Thx for help :)

  • VolreconVolrecon Posts: 8Questions: 1Answers: 0
  • VolreconVolrecon Posts: 8Questions: 1Answers: 0

    No. Doesnt work. I have to create function to sort? How can i do that?

  • kthorngrenkthorngren Posts: 21,327Questions: 26Answers: 4,949

    I didn't realize that your string is a Polish datetime format :smile:

    I would use the Date time sorting solution documented in this blog. I updated your example to show this:
    http://live.datatables.net/veqecoti/1/edit

    The console log is outputting the format that Datatables automatically finds for the datetime column in both tables. Note that the first table works and sets the moment type but the second table its a string. The problem is with your first array element:

      const tableOptions = {
                data: [
                    [
                        "1",
                        '02393945545',
                        '',
                        '2021-03-24 15:26:57 [id: 7]',
                      "28 dni 19 godzin 33 minut 3 sekund"
                    ],
    

    3 sekund should be 03 sekund to match the format found in the other rows. Change it in the example then the sorting will work.

    Kevin

  • VolreconVolrecon Posts: 8Questions: 1Answers: 0

    I thing it`s work :) Thx

This discussion has been closed.