YUILibrary - Open source JavaScript and CSS for building richly interactive software.
Fork YUI on GitHub
  [ 6 posts ]

Vasya Ivanov

  • Username: vasyaivanov
  • Joined: Tue Nov 24, 2009 4:03 pm
  • Posts: 3
  • Offline
  • Profile
Tags:

DataTable custom sort sortedBy not working

Post Posted: Tue Nov 24, 2009 4:09 pm
+0-
Hi I wrote a custom sort for a Datatable (I am using Progressive table), and
specified sortedBy this column. However initial sort is wrong or not applied,
but when I click on column to sort - it sorts it properly according to my custom
sort.
Does anyone know why?

Here is a code bellow:





// Custom sorting data fucntion
var sortDate = function(a, b, desc, field) {
// Deal with empty values
if(!YAHOO.lang.isValue(a)) {
return (!YAHOO.lang.isValue(b)) ? 0 : 1;
}
else if(!YAHOO.lang.isValue(b)) {
return -1;
}

var comp = YAHOO.util.Sort.compare;
var compState=0;

var aSplit=a.getData(field).split(new RegExp( "[-: ]{1}", "g" ));
var bSplit=b.getData(field).split(new RegExp( "[-: ]{1}", "g" ));
for(i=0;i<aSplit.length;i++)
{
compState = comp(aSplit[i], bSplit[i], desc);
if(compState!=0) return compState;
}
};






YAHOO.util.Event.addListener(window, "load", function() {
YAHOO.example.EnhanceFromMarkup2 = new function() {
var myColumnDefs = [

{key:"start",label:"Start",sortable:true,sortOptions:{sortFunction:sortDate}},
{key:"end",label:"End", sortable:false,
sortable:true,sortOptions:{sortFunction:sortLinkDate}},
{key:"error",label:"Error",sortable:true},
{key:"status",label:"Status",sortable:true},
{key:"bug",label:"Bug",sortable:false}
];


this.myDataSource = new
YAHOO.util.DataSource(YAHOO.util.Dom.get("statAll"));
this.myDataSource.responseType = YAHOO.util.DataSource.TYPE_HTMLTABLE;
this.myDataSource.responseSchema = {
fields: [{key:"start"},
{key:"end"},
{key:"error"},
{key:"status"},
{key:"bug"}
]
};

this.myDataTable = new YAHOO.widget.DataTable("statMarkup",
myColumnDefs, this.myDataSource,
{caption:"All Process Stats",
sortedBy:{key:"start",
dir:YAHOO.widget.DataTable.CLASS_DESC,sortFunction:sortDate}}
);
};
});

Satyam

YUI Contributor

  • Username: Satyam
  • Joined: Tue Dec 09, 2008 12:34 am
  • Posts: 1393
  • Location: Sitges, Spain
  • GitHub: Satyam
  • Gists: Satyam
  • YUI Developer
  • Offline
  • Profile

Re: DataTable custom sort sortedBy not working

Post Posted: Wed Nov 25, 2009 12:49 am
+0-
SortedBy is meant to tell the DataTable how the data came sorted from the source, so it can apply the proper visual styles. It won't sort the table but if the data came sorted, say, from an SQL server, where sorting is usually cheap, you can tell the DataTable about that.

Vasya Ivanov

  • Username: vasyaivanov
  • Joined: Tue Nov 24, 2009 4:03 pm
  • Posts: 3
  • Offline
  • Profile

Re: DataTable custom sort sortedBy not working

Post Posted: Wed Nov 25, 2009 10:28 am
+0-
Thank you for your response.
How do I make it sort when Datatable renders the first time?

Satyam

YUI Contributor

  • Username: Satyam
  • Joined: Tue Dec 09, 2008 12:34 am
  • Posts: 1393
  • Location: Sitges, Spain
  • GitHub: Satyam
  • Gists: Satyam
  • YUI Developer
  • Offline
  • Profile

Re: DataTable custom sort sortedBy not working

Post Posted: Wed Nov 25, 2009 12:34 pm
+0-
The best is to send it sorted from the server since SQL servers are highly optimized for that.

The next best solution is to sort it in response to dataReturnEvent:

http://developer.yahoo.com/yui/docs/YAH ... eturnEvent

or doBeforeCallback:

http://developer.yahoo.com/yui/datasource/#events

Dirren Lang

  • Username: lexx
  • Joined: Tue Feb 02, 2010 8:10 am
  • Posts: 2
  • Offline
  • Profile
Tags:

Re: DataTable custom sort sortedBy not working

Post Posted: Sun May 09, 2010 2:09 pm
+0-
Hi Satyam,

I have the same problem as Vasya, my datatable should get sorted client-side, after loading.

Here's my approach:

myDataSource.doBeforeParseData = function (oRequest, oRawResponse, oCallback) {
/* SORTING*/
var elColumn = myDataTable.getColumn("ColToSort");
if(elColumn != undefined) {
myDataTable.sortColumn(elColumn, "ASC");
}
return oRawResponse;
};

Unfortunately - nothing happens.

This code fragment is placed directly after my

myDataTable = new YAHOO.widget.DataTable(...)

statement.

How do I get that work? Is there really no other solution to let YUI sort onload?

Thanks for your help,

lexx

Satyam

YUI Contributor

  • Username: Satyam
  • Joined: Tue Dec 09, 2008 12:34 am
  • Posts: 1393
  • Location: Sitges, Spain
  • GitHub: Satyam
  • Gists: Satyam
  • YUI Developer
  • Offline
  • Profile

Re: DataTable custom sort sortedBy not working

Post Posted: Sun May 09, 2010 9:33 pm
+0-
You can't use sortColumn since the data has not reached the DataTable yet. It has barely reached the DataSource for that matter, it has not even been parsed. You have to use JavaScript's native Array.sort function on the raw response. It will be easier if you try to sort it later, when it is already parsed, either in doBeforeCallback or in DataTable's doBeforeLoadData
  [ 6 posts ]
Display posts from previous:  Sort by  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum