Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

columnLabelable date, wrong order #17

Open
m4urer opened this issue Jul 12, 2013 · 5 comments
Open

columnLabelable date, wrong order #17

m4urer opened this issue Jul 12, 2013 · 5 comments

Comments

@m4urer
Copy link

m4urer commented Jul 12, 2013

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
Copy link
Owner

rwjblue commented Jul 12, 2013

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
Copy link
Author

m4urer commented Jul 12, 2013

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
Copy link
Author

m4urer commented Jul 12, 2013

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
Copy link
Owner

rwjblue commented Jul 12, 2013

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 [email protected] 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
Copy link
Collaborator

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants