YUILibrary - Open source JavaScript and CSS for building richly interactive software.
Fork YUI on GitHub
  [ 4 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: 692
  • 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: 692
  • 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
  [ 4 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