[ 8 posts ]

dtportnoy69

  • Joined: Tue May 12, 2009 6:03 am
  • Posts: 4
  • Offline
  • Profile

Default sorting not by Resultset

Post Posted: Tue May 12, 2009 6:14 am
+0-
Hi guys,

I'm going crazy over this problem. I have my Datasource all set up, but it's in no particular order, in fact due to the Database structure and its return in PHP, having the Datasource sorted the way I want initially will get ugly.

Basically I want YUI's Datatable to sort by a field right away instead of having the user click the column first. Is there a way I can instruct YUI to do this after the datatable is loaded? Remember I"m not talking about using the sortedBy option , which won't help my cause since the Datasource is not sorted initially.

Any help is much appreciated.
Thanks! :)

Lawrence

Satyam

YUI Contributor

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

Re: Default sorting not by Resultset

Post Posted: Tue May 12, 2009 6:36 am
+0-
You can call the sortColumn method in response to initEvent. It is likely to produce a flicker since the table will be drawn unsorted and then redrawn sorted. Depending on the size of your data it might be noticeable or not. Actually, noticeable it will be, it might be annoying or not.

Another alternative is to override method doBeforeCallBack from DataSource. The default method does nothing but return its parsedResponse argument unchanged. This method is meant to be overridden, it is a hook placed there to do this kind of manipulation and whatever it is you return from that method is what the DataTable will see.

The parsedResponse argument is an object with several properties, one of which is going to be an array containing all your data. You can sort that array with JavaScript native Array.sort function and return the parsedResponse object with the sorted array within. This will be faster and will not produce any flicker as the DataTable would not have been drawn yet.

dtportnoy69

  • Joined: Tue May 12, 2009 6:03 am
  • Posts: 4
  • Offline
  • Profile

Re: Default sorting not by Resultset

Post Posted: Tue May 12, 2009 6:56 am
+0-
Perfect! Thanks for the quick response and all your activity in the JS world! All your posts are extremely helpful.

dtportnoy69

  • Joined: Tue May 12, 2009 6:03 am
  • Posts: 4
  • Offline
  • Profile

Re: Default sorting not by Resultset

Post Posted: Tue May 12, 2009 1:28 pm
+0-
Ah sorry to be a pain. My Datasource is a multi-dimensional json array. It doesn't seem I can use the basic array.sort methods to sort by a given field in this JSON array.

So I am trying your first suggestion but am not getting anywhere, mind you its my first time working with initEvent and the sortColumn method. I thought I would just be able to subscribe to the initEvent and call the method, but that seems to be returning an error. Here's a snippet of some of the code I have so far:

Code:
var myDataTable = new YAHOO.widget.DataTable("sortHome", myColumnDefs, myDataSource);
                 
myDataTable.subscribe('initEvent', myDataTable.sortColumn('pts'));


And here's the error I get in Firebug:
"Invalid callback for subscriber to 'initEvent'"

I've been using YUI for a while now, but mostly working off of examples so in theory I'm still a newbie.
Thanks for the helping hand. :)

Satyam

YUI Contributor

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

Re: Default sorting not by Resultset

Post Posted: Wed May 13, 2009 12:27 am
+0-
By the time doBeforeCallback gets called, your data should have been flattened to a plain array so even if you are using dot notation in the fields array to access hierarchical data, the fields would have been extracted by then. There is another overriddable method, doBeforeParseData where your data is still raw, you would be in trouble with that one, but doBeforeCallback happens after that and just before the DataTable gets its data, which can only be flat.

If you are unraveling the hierarchical structure in the formatter function of each column def then, as much as possible, don't. The correct place to flatten the structure is in the DataSource, either with the built-in parsers, the dot notation for hierarchical JSON data or by providing your own parsers.

The problem with your listener is that you are not providing a reference to the function to be called but the result of calling the function. The parenthesis with the arguments after the callback function name will make it execute immediately and return a value. To pass arguments to an event listener, you have to use the third argument which will get merged with the arguments the event will naturally provide.

See:

http://developer.yahoo.com/yui/docs/YAH ... #method_on

dtportnoy69

  • Joined: Tue May 12, 2009 6:03 am
  • Posts: 4
  • Offline
  • Profile

Re: Default sorting not by Resultset

Post Posted: Wed May 13, 2009 5:33 am
+0-
Thanks Satyam,

I took more time to research the implementation of all this as it is a first. So I'll just break down some sources and code in case it will help anyone else reading this post.

At first I was unfamiliar with doBeforeCallback, so I researched it more and ran into another one of your posts about datatables which had an example using the doBeforeCallback. That post was here:

http://yuiblog.com/blog/2008/10/15/data ... -part-one/

From there I just console.log'ged what I needed. For example, I wasn't sure of the structure of 'oParsedResponse' so I checked it out and found out that all my results are in objects in 'oParsedResponse.results'.

I then needed a little 101 on JS array sorting. The 'brain block' in my head was how can I sort this out if it's in a multi-dimensional array or in this case multiple objects. All the sort examples I've seen on the net are all straight forward non associative arrays.

If you're new to array sorting a good example and explanation for array sorts can be found here http://www.javascriptkit.com/javatutors/arraysort.shtml. Turns out the Array.sort method accepts a parameter which can take in a custom method which from what I can tell compares values and acts depending whether the result is negative, 0, or positive. From there, with some more console.logging I ended up with the following code which is now working well:

Code:
<script type="text/javascript">   
    YAHOO.util.Event.onDOMReady(function() {
            var myColumnDefs = [
                {key:"player",label:"Jouer",sortable:true},
                {key:"g",label:"B",sortable:true},
                {key:"a",label:"A",sortable:true},
                {key:"pts",label:"PTS",sortable:true},
                {key:"pim",label:"PUN",sortable:true}
            ];
   
            var myDataSource = new YAHOO.util.DataSource(YAHOO.example.Data.stats);
            myDataSource.responseType = YAHOO.util.DataSource.TYPE_JSARRAY;
            myDataSource.responseSchema = {
                fields: ["player","g","a","pts","pim","ppg"]
            };
        myDataSource.doBeforeCallback = function (oRequest, oFullResponse, oParsedResponse, oCallback) {
                oParsedResponse.results.sort(sortPts);
                return oParsedResponse;
            };
           
            var myDataTable = new YAHOO.widget.DataTable("sortHome", myColumnDefs,
                    myDataSource, {sortedBy:{key:"pts", dir:"asc"}});
             
            return {
                oDS: myDataSource,
                oDT: myDataTable
            };
        }() );
       
        function sortPts(a, b){
            return b.pts - a.pts;
        }
    </script>

The main thing to look at that I needed in order to sort my dataset is the 'myDataSource.doBeforeCallback' and the sortPts method.
If you look at my function sortPts you'll see that you can sort by comparing object values. All in all a productive day!
Thanks Satyam :geek:

Eugene Rudenko

  • Username: mrgrechkinn
  • Joined: Mon Jul 16, 2012 7:02 am
  • Posts: 1
  • Twitter: mrgrechkinn
  • Offline
  • Profile

Re: Default sorting not by Resultset

Post Posted: Mon Jul 16, 2012 7:30 am
+0-
Hello.

I am using YUI version 2.8.1. Has something changed in the matter of the initial sorting?
I don't want "flicker" effect and sorting by hand

I found a way to sort records after calling setRecords method, it's sends "recordsSetEvent" event, but there is a bug with creating this event. Of course, I can fix it, but it's not clearly way.

What can you advise me?

Regards, Eugene.

Satyam

YUI Contributor

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

Re: Default sorting not by Resultset

Post Posted: Mon Jul 16, 2012 8:26 am
+0-
I don't recall any significant changes since the posts above. The code in the codebox of the posting before your question should still work and produce no flicker at all.

The DataTable never sorted records when the sortedBy configuration attribute was given. The idea for that attribute is for the DataTable to reflect the sorting done server-side, it was never a command to get data sorted. That is still the best alternative, adding an ORDER BY clause to your SQL statement is still the best, but, if you need to do it client side, the code above should work nicely.
  [ 8 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