Holt59 / datatable

Javascript Plugin for data tables creation
http://holt59.github.io/datatable/
MIT License
107 stars 42 forks source link

Handling complex tables with complex data #22

Closed julesbl closed 7 years ago

julesbl commented 7 years ago

Hi Been using this quite a bit and it works well in most situations.

I've been thinking of an enhancement that would handle complex tables

config.sort = [false, true, 4, {column: 7, func: number_sort,}];

So the integer is similar to data-sort="4" in that header.

{column: 7, func: number_sort,} applies the function number_sort to column 7 in a similar way to the current functionality except it is now possible to specify the data column.

Thought these two enhancements would allow the plugin to handle much more complex tables.

Jules

Holt59 commented 7 years ago

I don't really understand what you want to do... You would like to be able to sort specific columns without having to specify true/false for all the others?

julesbl commented 7 years ago

Hi Mikaël I'm obviously not making the idea clear. At the moment config.sort tends to be like [true, false, true, true etc..]

This does not help with tables like this

        <tr>
            <th>stallion</th>
            <th>runners</th>
            <th colspan="2">winners</th>
            <th>w/r</th>
            <th>starts</th>
            <th>wins</th>
            <th colspan="2">stakes </th>
            <th colspan="2">progeny  earnings</th>
        </tr>

with complex strings like £1,500,000 in the sort columns for progeny earnings which need to be sorted out by a function.

What I was suggesting is the ability to put an integer in the column position to indicate a sort column, not just true, and also the capability of and integer and a function for processing the data to make it sortable. config.sort = [false, true, true, 4, false, false, false, {column: 9, func: price_sort,}]; would make 'w/r' sort on column 4 and 'progeny earnings' would sort on column 9 using the function price_sort to implement the sorting function for data like £1,500,000

Hope that is clearer.

Jules

Holt59 commented 7 years ago

@julesbl I have to admit that I am not even sure of all the features available in this plugin anymore (the documentation is way outdated... ), but I think that what you want is already implemented in the following way:

config.sort = {
    0: false,
    1: true,
    2: true,
    4: true,
    5: false,
    6: false,
    9: number_sort
};
julesbl commented 7 years ago

Works very well when there are the same number of header columns as data columns, where it comes unstuck at the moment is when they differ, as in this case 8 header cols and 11 data columns, there is no way to tell the sort function to work on anything but column 7

Holt59 commented 7 years ago

@julesbl The index/keys of the array/object you use for the sort options are match with the <th> elements (the nth element of config.sort will be used for the nth <th>). If either:

...you can use an Object instead of an Array, such as the one in my previous answer, to specify on which column each <th> should sort.

Small example:

If your <th> are like this:

<tr>
  <th>TH 1</th>
  <th colspan="2">TH 2</th>
  <th>TH 3</th>
  <th colspan="2">TH 4</th>
</tr>

Then:

[true, true, true, true];

...will make TH 1 sort the first column, TH 2 the second, TH 3 the third, and so on...

But:

{
    0: true,
    1: true,
    3: true,
    4: true
} 

...will make TH 1 sort the first, TH 2 sort the second, TH 3 sort the fourth and TH 4 the fifth.

julesbl commented 7 years ago

Thanks, had not twigged about the object