rwjblue / pivot.js

Build Pivot Tables from CSV/JSON Data
http://rwjblue.github.com/pivot.js/
Other
783 stars 134 forks source link

columnLabelable date, wrong order #17

Open m4urer opened 11 years ago

m4urer commented 11 years ago

I have a columnLabel with the format 'dd/mm/yyyy' (my country's date format) and the problem is the column order is wrong, since 29th of june (29/06) is "larger" than 1st of July (01/07) number wise.

Is there a way to tell pivot.js that the column value and the sorting value are not the same?? I tried with a pseudoFunction and a displayFunction with no luck. Can this be done?

Thanks!!

rwjblue commented 11 years ago

Unless I am mistaken (I could be mis-remembering), the sorting in the demo application is actually handled by datatables. They have a bunch of configurable options, and whole section in their documentation on sorting.

See here for details. Please report back and let us know the solution if you fix it.

m4urer commented 11 years ago

The columnLabels is not part of the data of the datatables. I found the sort function that the columnLabel is using. In pivot.js you have:

  function populateColumnLabelColumnsResults(columnLabels){
    var keys  = objectKeys(columnLabels).sort(),
        i     = -1,
        m     = keys.length,
        w     = objectKeys(displayFields.summaries).length;
    while (++i < m){
      resultsColumns.push({fieldName: keys[i], width: w, type: 'column'})
    };
    return resultsColumns;
  }

The objectKeys(columnLabels).sort() is using the "natural order" which is the problem for me. I'll try to patch the code and use a custom sort function when it's needed. If you have a better idea or some suggestions, be my guest!

Thanks in advanced!

m4urer commented 11 years ago

The solution:

added a sortFunction to my field definition:

{name: 'dateField', type: 'string', filterable: true, pseudo: true,columnLabelable: true, rowLabelable: false,
            pseudoFunction: function(row){
                //my pseudo function
            },
            sortFunction: function(a,b){
                   //my custom sort function
            }
}

And then made some changes to the populateColumnLabelColumnsResults of pivot.js: added a key parameter that has to be added in the method invocation. And then added the first 6 rows and changed the 7th to use sortedKeys. And that's it!

  function populateColumnLabelColumnsResults(key, columnLabels){
    var sortedKeys; 
    if (fields[key].sortFunction !== undefined){
        sortedKeys = objectKeys(columnLabels).sort(fields[key].sortFunction);
    }else{
                sortedKeys = objectKeys(columnLabels).sort();
        }
    var keys  = sortedKeys,
        i     = -1,
        m     = keys.length,
        w     = objectKeys(displayFields.summaries).length;

    while (++i < m){
      resultsColumns.push({fieldName: keys[i], width: w, type: 'column'})
    };

Now the custom sortFunction gets called when it's defined! If you want I can make a pull request...

rwjblue commented 11 years ago

Great job! Yes, a pull request would be most welcome.

Robert Jackson

-- twitter: rwjblue -- github: rjackson

On Jul 12, 2013, at 3:41 PM, m4urer notifications@github.com wrote:

The solution:

added a sortFunction to my field definition:

{name: 'dateField', type: 'string', filterable: true, pseudo: true,columnLabelable: true, rowLabelable: false, pseudoFunction: function(row){ //my pseudo function }, sortFunction: function(a,b){ //my custom sort function } } And then made some changes to the populateColumnLabelColumnsResults of pivot.js: added a key parameter that has to be added in the method invocation. And then added the first 6 rows and changed the 7th to use sortedKeys. And that's it!

function populateColumnLabelColumnsResults(key, columnLabels){ var sortedKeys; if (fields[key].sortFunction !== undefined){ sortedKeys = objectKeys(columnLabels).sort(fields[key].sortFunction); }else{ sortedKeys = objectKeys(columnLabels).sort(); } var keys = sortedKeys, i = -1, m = keys.length, w = objectKeys(displayFields.summaries).length;

while (++i < m){
  resultsColumns.push({fieldName: keys[i], width: w, type: 'column'})
};

Now the custom sortFunction gets called when it's defined! If you want I can make a pull request...

— Reply to this email directly or view it on GitHub.

rondale-sc commented 11 years ago

This looks good. Once @rjackson gets a chance to look at it we'll merge it in.