Javascriptで表の並べ替えを実現します。
9506 ワード
英文原文:[url]http://kryogenix.org/code/browser/sorttable/
[/url]
sorttable:Make all your table sortable
Now,how to use it.To make a table of your chice sortable,there are three steps:
Download the Javascript library
Include the Javascript library,by putting a link to it in the HEAD of your page,like so:
<script src=「sorttable.js」>
[/script]
Mark your table as a sortable one by giving it a class of「sortable」:
And that's all you need.Your table will now have column sorting avaible by clicking the headers.For niness,you might want to add the following style to your styreet,or make up some of your bathis:
--------------------------------------------------
Adving anced usage
You probably don't need the bits here.Sorttable is designed to require no configration,no fiddling around.Just tare it and use it.If you need it to do more than it by default,then come back here and read.partis
How do I make my“totals”row stay the bottom of my table?
How do I sort some data in a format that sorttable doesn't understand?
What do I do if sorttable isn't sorting my data the way I'd expect it to?
Can sorttable do a stable sort?
How do I make some columns not be sortable?
Can sort table sort a table as son the page loads、without requiring the user to click?
How do I make sorttable sort columns in descending order first rather than ascending order?
What's new in version 2 of sorttable?
Can I use sorttable in a commerical product?On a commerical website?What licencing terms is it available under?
I love sorttable so mucthat I want to donate some money to you even though I don't have to.Can I?
Totals rows
If you have a「totals」row at the bottom of your table that you want to stay the bottom of yourtable(and not get sorted)、then add it to asection in yourtable
Person Monthly pay
Steve Nicol l 8,500
Steve McMahon 9,200
ジョンバーンズ15,300
Jan Molby£12,000
TOTAL£45,000
Using custom sort keys
You may have some data which does go in and order but isn't identifed by sorttable.The way to fix is problem is to use custom sort keys.Take,for example,a column of spelled nbers.Ordinalit。it will treat the spelled-out numbers as strigs,and so would sort the numbers in alphabers order,i.e.five,four,one,three,two.To get around this,you can specify a cell your tablecustomkey atribute、and sorttable will use the contens of that ate instead of the text in the cell itself when sorting the table.So、for example、yourtable might look like is:
one Steve
two Jan
three Bruce
You can fix practically any problem you have with sorttable's atomatic column typing by specifying custom sort keys.
Manualy specifying a column's type
Sorttable work s works out the type of your columns in order to workout how to sort them(numberss sort differently than letters,for example).Ocasionally,it might get it wrong.If so,oucan explicitititititititittytytytytytytytytytytytytytytytytytytytytytytytytytytytytytytytytytytytytytytytytytytytytytytype.Iftttttttttttttttttttttttttttttttflong.If.Iソフタブルcolumntype to the header row of that column.Available column types are numeric,alpha,dd mm,and mmdd.The latter tworle for dates,but are not likelyto be useful because because therity atwartable the atwitter atwitter
So,for example,if you have a「part number」column which you want to be treated as if it were numeric,then you might do your table like this:
Stable sorting
Sorttable,by default,does an unstable sort.This means that t t t mantain the order of rowhee throws have the same key in the sorted column.Wikipedia has has more stable sorting.If prothisisisa prorerereaaaaaaaaaaablblble prorereretttttttttttttttttttttttttttttttwiwiwiwiwiwirererererererererererererererererererererererererererererererererererered d d d d d d and find the lineas:
//*If you want a stable sort,uncomment the follwing line*/
//sorttable.sharker_sort;
//*andコメントout this one*/
row_array.sort(this.sorttable function);and change them so the sharker_sort line is uncommentd instead:
sorttable.sharker_sort;
//row_array.sort(this.sorttable function);The sort will now be stable.However,it will be consider able slower(the stable sorting can take eigh times as long as the unstable sort)、which is why it isn't enabled by default.
Making some columns unsortable
If you'd like some column headers to not be clickable,meaning that your users won't be able to the table by those columns,then add class=「sorttable」to thecolumhers.Orver."
Name 6 Team Number of legs
Adiles、Ossie Tottenham Hotspur 2
Charie Nichollas Asenal 2
Hughes,Mark Manchester Utd 2
Molby,Jan Liverpool 2
ニコル、Steve Liverpool 2
Sorting the table when the page is loaded
Lots of people ask,“how do I I make sort table sot the table the first time the page is loaded?”The answes is:you don't.Sorttable isaaut chaging the HTML the is served from your server server ver server ver ver ver ver ver ver ver ver ver ver ver ver ver ver ver thethethethethethererereverververververververververververververververververthethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethetheaaaaaaaaaaaaaath th th th ffth th So,if you want the table sorted when a page is first displayed,serve the table in sorted order.Tables over compone out of a database;get the data from the database in a sorted order with an ORDER BY clause in your SQL.Any soution which involves you running sorttable as the page loads(i.e.without user input is)a.sonton
Sort in descending order first
Making sorttable sort your columns in descending order first rather than ascending order requires eding sorttable.js.Find the line:
row_array.sort(this.sorttable function);and after it、add a newライン:
row_array.reverse()Changes from sorttable v 1
Sorttable has changed quite a lot in this new version 2.Please read the release announcement for more details of what's changed and who is to be thanked.If for some reason the original version 1 of sortable dowblev 1.js along with the original instructions.
Licencing
Sorttable、like all my DOM scripts、is under the X 11 licence、which baically means You can do what You want with it、including ust or in a commerical setting or product.If You're not sure、tor.ore。
Can I give you money for sorttable?I really like it
You don't have to.I believe in Free Software;writing this and seeigng people use it is its own reward.Of course,if you're determined to give me money then I'm not going to stop you.You can send money to myPal account with the button below.
[/url]
sorttable:Make all your table sortable
Now,how to use it.To make a table of your chice sortable,there are three steps:
Download the Javascript library
Include the Javascript library,by putting a link to it in the HEAD of your page,like so:
<script src=「sorttable.js」>
[/script]
Mark your table as a sortable one by giving it a class of「sortable」:
<table class="sortable">
Note that the library's JavaScript file is cared sorttable(two Ts)、but the class the class to the table is sortable(one T)And that's all you need.Your table will now have column sorting avaible by clicking the headers.For niness,you might want to add the following style to your styreet,or make up some of your bathis:
/* Sortable tables */
table.sortable thead {
background-color:#eee;
color:#666666;
font-weight: bold;
cursor: default;
}
This is version 2 of sorttable,released Appril 2007.If you're using the old version,you may find it useful to udate;v 2 has numerous new feature s and shuld be compleetely backwards comppatible.--------------------------------------------------
Adving anced usage
You probably don't need the bits here.Sorttable is designed to require no configration,no fiddling around.Just tare it and use it.If you need it to do more than it by default,then come back here and read.partis
How do I make my“totals”row stay the bottom of my table?
How do I sort some data in a format that sorttable doesn't understand?
What do I do if sorttable isn't sorting my data the way I'd expect it to?
Can sorttable do a stable sort?
How do I make some columns not be sortable?
Can sort table sort a table as son the page loads、without requiring the user to click?
How do I make sorttable sort columns in descending order first rather than ascending order?
What's new in version 2 of sorttable?
Can I use sorttable in a commerical product?On a commerical website?What licencing terms is it available under?
I love sorttable so mucthat I want to donate some money to you even though I don't have to.Can I?
Totals rows
If you have a「totals」row at the bottom of your table that you want to stay the bottom of yourtable(and not get sorted)、then add it to asection in yourtable
<table class="sortable">
<thead>
<tr><th>Person</th><th>Monthly pay</th></tr>
</thead>
<tbody>
<tr><td>Jan Molby</td><td>£12,000</td></tr>
<tr><td>Steve Nicol</td><td>£8,500</td></tr>
<tr><td>Steve McMahon</td><td>£9,200</td></tr>
<tr><td>John Barnes</td><td>£15,300</td></tr>
</tbody>
<tfoot>
<tr><td>TOTAL</td><td>£45,000</td></tr>
</tfoot>
</table>
Note how sorting the table leaves the TOTAL row at the bottom.Person Monthly pay
Steve Nicol l 8,500
Steve McMahon 9,200
ジョンバーンズ15,300
Jan Molby£12,000
TOTAL£45,000
Using custom sort keys
You may have some data which does go in and order but isn't identifed by sorttable.The way to fix is problem is to use custom sort keys.Take,for example,a column of spelled nbers.Ordinalit。it will treat the spelled-out numbers as strigs,and so would sort the numbers in alphabers order,i.e.five,four,one,three,two.To get around this,you can specify a cell your tablecustomkey atribute、and sorttable will use the contens of that ate instead of the text in the cell itself when sorting the table.So、for example、yourtable might look like is:
<table class="sortable">
<tr><th>Number (spelled)</th><th>Person</th></tr>
<tr><td sorttable_customkey="2">two</td><td>Jan</td></tr>
<tr><td sorttable_customkey="3">three</td><td>Bruce</td></tr>
<tr><td sorttable_customkey="1">one</td><td>Steve</td></tr>
</table>
Note that clicking on the「Number」column in the table below sorts it in the corectone,two,three order.Number(spelled)Personone Steve
two Jan
three Bruce
You can fix practically any problem you have with sorttable's atomatic column typing by specifying custom sort keys.
Manualy specifying a column's type
Sorttable work s works out the type of your columns in order to workout how to sort them(numberss sort differently than letters,for example).Ocasionally,it might get it wrong.If so,oucan explicitititititititittytytytytytytytytytytytytytytytytytytytytytytytytytytytytytytytytytytytytytytytytytytytytytytype.Iftttttttttttttttttttttttttttttttflong.If.Iソフタブルcolumntype to the header row of that column.Available column types are numeric,alpha,dd mm,and mmdd.The latter tworle for dates,but are not likelyto be useful because because therity atwartable the atwitter atwitter
So,for example,if you have a「part number」column which you want to be treated as if it were numeric,then you might do your table like this:
<table class="sortable">
<tr>
<th class="sorttable_numeric">Part number</th><th>Part name</th>
</tr>
<tr>
<td>111-A5</td><td>Three-eighths Gripley</td>
</tr>
<tr>
<td>31337-H4X0R</td><td>Computer system intrusion toolkit</td>
</tr>
</table>
Remember:ou probably dot need to do this.It is unlikely that you'll need to"force"Sorttable to recognise a column type.You may also to investigate using custom sokeys,above,asa betteving of。Stable sorting
Sorttable,by default,does an unstable sort.This means that t t t mantain the order of rowhee throws have the same key in the sorted column.Wikipedia has has more stable sorting.If prothisisisa prorerereaaaaaaaaaaablblble prorereretttttttttttttttttttttttttttttttwiwiwiwiwiwirererererererererererererererererererererererererererererererererererered d d d d d d and find the lineas:
//*If you want a stable sort,uncomment the follwing line*/
//sorttable.sharker_sort;
//*andコメントout this one*/
row_array.sort(this.sorttable function);and change them so the sharker_sort line is uncommentd instead:
sorttable.sharker_sort;
//row_array.sort(this.sorttable function);The sort will now be stable.However,it will be consider able slower(the stable sorting can take eigh times as long as the unstable sort)、which is why it isn't enabled by default.
Making some columns unsortable
If you'd like some column headers to not be clickable,meaning that your users won't be able to the table by those columns,then add class=「sorttable」to thecolumhers.Orver."
Name 6 Team Number of legs
Adiles、Ossie Tottenham Hotspur 2
Charie Nichollas Asenal 2
Hughes,Mark Manchester Utd 2
Molby,Jan Liverpool 2
ニコル、Steve Liverpool 2
Sorting the table when the page is loaded
Lots of people ask,“how do I I make sort table sot the table the first time the page is loaded?”The answes is:you don't.Sorttable isaaut chaging the HTML the is served from your server server ver server ver ver ver ver ver ver ver ver ver ver ver ver ver ver ver thethethethethethererereverververververververververververververververververthethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethetheaaaaaaaaaaaaaath th th th ffth th So,if you want the table sorted when a page is first displayed,serve the table in sorted order.Tables over compone out of a database;get the data from the database in a sorted order with an ORDER BY clause in your SQL.Any soution which involves you running sorttable as the page loads(i.e.without user input is)a.sonton
Sort in descending order first
Making sorttable sort your columns in descending order first rather than ascending order requires eding sorttable.js.Find the line:
row_array.sort(this.sorttable function);and after it、add a newライン:
row_array.reverse()Changes from sorttable v 1
Sorttable has changed quite a lot in this new version 2.Please read the release announcement for more details of what's changed and who is to be thanked.If for some reason the original version 1 of sortable dowblev 1.js along with the original instructions.
Licencing
Sorttable、like all my DOM scripts、is under the X 11 licence、which baically means You can do what You want with it、including ust or in a commerical setting or product.If You're not sure、tor.ore。
Can I give you money for sorttable?I really like it
You don't have to.I believe in Free Software;writing this and seeigng people use it is its own reward.Of course,if you're determined to give me money then I'm not going to stop you.You can send money to myPal account with the button below.