Search the web
Sign In
New User? Sign Up
ydn-javascript · Yahoo! User Interface Library Group
? Already a member? Sign in to Yahoo!

Yahoo! Groups Tips

Did you know...
Real people. Real stories. See how Yahoo! Groups impacts members worldwide.

Best of Y! Groups

   Check them out and nominate your group.
Having problems with message search? Fill out this form to ensure your group is one of the first to be migrated to the new message search system.

Messages

  Messages Help
Advanced
DataTable numbers/float values not sorted properly   Message List  
Reply | Forward Message #50236 of 52114 |
Re: DataTable numbers/float values not sorted properly


This is a guess since I can't see your data, but I suspect the json coming back
has the value(s) as string. For example:

{ count:2, rows:[
{ field1:"100", field2:100 },
{ field1:"50", field2:50 }
]
}

Field1 as a column in a datatable will sort incorrectly, field2 will sort
correctly. Make sure that the data coming back in the json is correctly
(syntactically) represented.

I, for example, am normally converting blobs of stuff in a array (from a
database lookup) into json, and I use a helper function to do that which scans
each field and determines if it should be quoted or not. Something like this
(PHP):

function makeJSON ($a) {

// get the keys

$keys = array_keys($a);
$cols = sizeof($keys);

// make the data string

$json = "";

for ($c=0; $c<$cols; $c++) {

$k = $keys[$c];
$val = $a[$k];
$o = $n = $d = 0;
$sz = strlen($val);
for ($i=0; $i<$sz; $i++) {
$x = substr($val,$i,1);
if ((($x == '-') || ($x == '+') ||
($x == '.')) && ($i == 0)) ++$n;
elseif (($x >= '0') && ($x <= '9')) ++$n;
elseif (($x == '.') && ($d == 0)) ++$d;
else ++$o;
}
if (($o == 0) && ($n > 0) && ($d <= 1)) $v = $val;
else $v = "'" . jsSafe($val) . "'";

$json .= ($c==0?" ":", ") . "$k:$v";

}

// return the snippet

return ($json);

}

Note: jsSafe is just a function that makes sure any necessary characters are
"escaped" in the string to make it a legal javascript string.

Hope that helps...

~~bret


--- In ydn-javascript@yahoogroups.com, "Crazy Serb" <gusic@...> wrote:
>
> Ok, I've tried a few suggestions from people around here, and nothing so
> far has worked - the numbers still get sorted as if they were strings:
>
> Total Net Value:
> 0.441
> 101.404
> 107.558
> 107.558
> 108.48
> 108.945
> 11.195
> 12.143
> 12.801
> 131.73
>
> Here is what I've got as far as the code goes:
>
> <code>
> YAHOO.example.DynamicData = function() {
> // Column definitions
> var myColumnDefs = [
> {key:"NAME", label:"Name", width:200, resizeable:true,
> sortable:true},
> {key:"DATE", label:"Start Date", formatter:"date"},
> {key:"NET", label:"Total Net Value", sortable:true}
> ];
>
> // DataSource instance
> var myDataSource = new YAHOO.util.DataSource("/?json&");
> myDataSource.responseType = YAHOO.util.DataSource.TYPE_JSON;
> myDataSource.responseSchema = {
> resultsList: "records",
> fields: [
> {key:"NAME"},
> {key:"DATE"},
> {key:"NET", parser:"number"}
> ],
> metaFields: {
> totalRecords: "totalRecords",
> paginationRecordOffset : "startIndex",
> paginationRowsPerPage : "pageSize",
> sortKey: "sort",
> sortDir: "dir"
> }
> };
>
> // DataTable configuration
> var myConfigs = {
> initialRequest: "sort=NAME&dir=asc&startIndex=0&results=15", //
> Initial request for first page of data
> dynamicData: true, // Enables dynamic server-driven data
> sortedBy : {key:"NAME", dir:YAHOO.widget.DataTable.CLASS_ASC},
> // Sets UI initial sort arrow
> paginator: new YAHOO.widget.Paginator({
> rowsPerPage: 25,
> template: YAHOO.widget.Paginator.TEMPLATE_ROWS_PER_PAGE,
> rowsPerPageOptions: [10,25,50,100],
> pageLinks: 3
> })
> };
>
> // DataTable instance
> var myDataTable = new YAHOO.widget.DataTable("tab1", myColumnDefs,
> myDataSource, myConfigs);
> // Update totalRecords on the fly with value from server
> myDataTable.handleDataReturnPayload = function(oRequest, oResponse,
> oPayload) {
> oPayload.totalRecords = oResponse.meta.totalRecords;
> return oPayload;
> }
>
> return {
> ds: myDataSource,
> dt: myDataTable
> };
>
> }();
> </code>
>
>
> What am I missing here? This is driving me bananas...
>





Sat Jul 4, 2009 4:03 pm

bretlevy
Offline Offline
Send Email Send Email

Forward
Message #50236 of 52114 |
Expand Messages Author Sort by Date

Ok, I've tried a few suggestions from people around here, and nothing so far has worked - the numbers still get sorted as if they were strings: Total Net...
Crazy Serb
realcrazyserb
Offline Send Email
Jul 3, 2009
6:39 pm

This is a guess since I can't see your data, but I suspect the json coming back has the value(s) as string. For example: { count:2, rows:[ { field1:"100",...
bretlevy
Offline Send Email
Jul 4, 2009
4:04 pm

That's probably it, as I can only get a hold of json data with all fields/values in quotes... and I would have to go through that list AFTER the fact and...
Crazy Serb
realcrazyserb
Offline Send Email
Jul 6, 2009
3:42 pm

It should, indeed, parseNumber will convert a string of digits into a number, either integer or float (basically, multiplies whatever the value by 1). I...
Satyam
satyamutsa
Offline Send Email
Jul 6, 2009
4:09 pm

Add an event listener for initEvent and then put a debugger statement in the listener. myDataTable.on('initEvent', function() { debugger; ...
Satyam
satyamutsa
Offline Send Email
Jul 6, 2009
6:23 pm
Advanced

Copyright © 2009 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Guidelines - Help