| Page 1 of 2 | [ 11 posts ] | Go to page 1, 2 Next |
|
I have some numbers in a column, although I guess they are actually put in the DT as text (I'm not using formatters). So, sorting isn't working, i.e. it's sorting my numbers like this:
0 0 156 111 2 25 67 99 Is there a "number" formatter? Where is the list of formatters? What's the best way to make it sort correctly? I'm confused because the client-side sorting example uses text as numbers and it sorts correctly... |
|
Carefully read this:
http://developer.yahoo.com/yui/datatable/#basicsort Pay close attention to the second paragraph: Quote: Keep in mind that sorting a Column first sorts the data in the underlying RecordSet and then updates the DOM UI to reflect this new sort order. Therefore, the type of the data (e.g., String, Number, Date, etc.) held in the RecordSet determines the sort algorithm, not the type as defined in your Column definition formatter property. And to the 4th paragraph--e.g., Quote: Note that setting the sortedBy property does not perform a sort on the Column, it merely applies the appropriate CSS when the DataTable loads. "Where is the list of formatters?" -- it's on the same page, here: http://developer.yahoo.com/yui/datatable/#format Just keep in mind that "sorting" and "formatting" are entirely different things. |
|
You need to use a parser in the DataSource so that numbers coming as strings in whichever message format you are using, are converted to numbers. Then, the sort function will sort them as numbers so, for example, you would do:
myDataSource.responseSchema.fields = [ ..... {key:'someNumber',parser:'number'} ]; |
|
Cool thanks fellas =)
|
|
I find simple column sorting curiously difficult to pull off. One of my problems was having sortedBy in the datatable constructor, which was locking the sort order. I took that out, and then I could sort one of my text-formatted columns correctly.
However, have some number-formatter columns that I can't get sorted like numbers. I've tried all combinations of using number formatters and number parsers, with no luck. The confusing thing is that only my last column is sorting correctly, with formatter: "number" and parser: "number". Other columns with the same combo and same data aren't working. What's going on here? Finally, I am trying to sort the table when the page loads by using: Code: dataTable.sortColumn(dataTable.getColumn("suName"), YAHOO.widget.DataTable.CLASS_ASC); This sorts the column "suName," which has a text formatter and a string parser, but the sorting isn't correct or alphabetical. Please help! Been having these problems for months. |
|
Okay, so I have fixed some of my number sorting problems by changing the datatype of what I use to add rows (my table is populated by calls to addRow()). Instead of passing strings I converted to ints manually (why wasn't the parser working???).
I still need to sort the "suName" column when the page loads. How can I do it? |
|
Maybe you can provide a pastie link to your code and some of the data.
We can't help you fix what we can't see! |
|
stlsmiths wrote: We can't help you fix what we can't see! Yea I know... it's difficult for me to post code samples because this isn't actually a web application... Google SketchUp allows plugin developers to create DHTML dialogs in a desktop application, and the data is loaded into JS in a very unorthodox way... So basically any example I make will be a bastardized version of my actual code. But I will try. |
|
Okay.
If you read "KOVACS" response earlier in this thread it is right on target. Satyam was also exactly correct. I'm not sure what framework you are using, but adding data to a DataTable via .addRows is not a very stable and correct approach. I would highly recommend you consider converting it to a via DataSource. Because within DataSource it does all of the conversion and parsing for you. Again, maybe the "framework" you are using won't permit that. As for using the "sortedBy" configuration parameter, as the above posting says, this parameter only "looks" like the table is sorted, id doesn't actually "perform the sort". It is intended for applications where the DataSource is populated from, for example a SQL query that has an ORDER BY clause and the data arrives already sorted. Assuming your DataTable is initially unsorted, you can use "sortedBy" to change the initial appearance properly, but you still need to do a .sortColumn after the DT renders to actually conduct the sort. In your case, if you are using .addRow to populate the table, I would recommend you do your .sortColumn call after all of the addRow's are complete. I worked up a quick live working example that is available at http://jsfiddle.net/UF8Ne/. See if you can follow that and understand where I am coming from. Todd |
|
Ah!
It turned out to be an issue with the order I was doing things in. My code didn't guarantee that all of the addRow calls were completed when the sortColumn method was invoked. And yes, I was initially confused about sortedBy but I had taken that out. dt.sortColumn applies the correct css as well. Unfortunately datasources aren't a great option for me, but good suggestion. Thanks very much stlsmiths and others, you were a great help! |
| Page 1 of 2 | [ 11 posts ] | Go to page 1, 2 Next |
| 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 |
© 2006-2013 Yahoo! Inc. All rights reserved.
All code on this site is licensed under the BSD License unless stated otherwise.
About This Site · Security Contact Info
Powered by phpBB® Forum Software © phpBB Group