Sorting is dependent on how I populate the data?

Sorting is dependent on how I populate the data?

DavidPKCDavidPKC Posts: 3Questions: 1Answers: 0

Hello

I am using data tables 1.10.16. We are using web methods in C#, to return the data. This builds up a list object, for each column in the table and then adds the individual lists to a master list. On the front end, we are using Ajax to call the web method and then JavaScript to populate and set the properties of the table.

For example:-

            for (var i = 0; i < dataLength; i++) {
                data.push([r.d[0][i]],r.d[1][i]],r.d[2][i]],);
    }

The above code works perfectly and this includes that when you click a table header, the table sorted and this sort is case insensitive, which is what we want.

This is an example of a 3 column table. In real life, we have more than 3 columns. Therefore, I have developed generic code, both in my C# web method and the JavaScript. An example of this JavaScript code is:-

            for (var row = 0; row < noOfRecords; row++) {
                record = [];
                for (var col =0; col <= noOfColumns; col++) {
                    record.push([r.d[col][row]]);
                }
                data.push(record);
    }

The thinking behind this is that I am trying to simplify the code as much as possible, so that colleagues can just copy and paste and just change the SQL as required.

This generic code works with 1 annoying side effect-it turns the sorting into case-sensitive! Any idea why?

(My generic code does additional stuff, such as, the 1st list in the arrays, returns a list of column names and on my page I have a table header defined without columns and then I use JavaScript to add the table columns. However, I have tested a simplified version of my code and I am confident that it is the difference in the 2 code blocks above that changes it from case insensitive to case-sensitive sorting).

Thanks

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 20,302Questions: 26Answers: 4,769

    This generic code works with 1 annoying side effect-it turns the sorting into case-sensitive! Any idea why?

    Are you saying that when clicking on the column to sort it is a case sensitive sort?

    Its hard to say what the problem might be without actually seeing it. We would need to see the data, how you have sorting setup, etc. Can you put together a simple example with a subset of your data set so we can see what is going on?
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

  • DavidPKCDavidPKC Posts: 3Questions: 1Answers: 0

    Yes-if I use the first snippet of code above, clicking on a column sorts it and it is case insensitive. However, if I replace that code with the second code snippet, then the sort is case sensitive.

    I have built a test page, within my own environment, and the only difference between the two data tables are the code snippets above.

    Unfortunately, the data is coming from an internal databases server and I cannot think of an easy way to replicate that on the public web page.

  • DavidPKCDavidPKC Posts: 3Questions: 1Answers: 0

    I have managed to set an example online. It can be found at:-
    http://live.datatables.net/zevibaha/1/edit

    The table uses a JavaScript array, but obviously my version retrieve data from a database. There are two comments, about sorting. If you comment and uncomment the line, immediately below the comments, you can observe the behaviour I have described by trying to sort the colour column.

    Thanks

  • kthorngrenkthorngren Posts: 20,302Questions: 26Answers: 4,769
    Answer ✓

    Thanks for the test case. It helps.

    The problem is each column data is an array:

    You need to change record.push([data2[col][row]]); to record.push(data2[col][row]);. For example:
    http://live.datatables.net/xoginexi/1/edit

    Kevin

This discussion has been closed.