2012-07-19 2 views
0

반복되는 열을 처리 할 수있는 자바 스크립트 테이블 분류기가 필요합니다. 테이블이 너무 좁기 때문에 테이블이 다중 열에 배치됩니다.반복 열을 사용하는 자바 스크립트의 열 정렬

<table> 
<thead> 
    <tr> <th>name</th> <th>score</th> <th>name</th> <th>score</th> </tr> 
</thead> 
<tbody> 
<tr> <td> n1 </td> <td> 4 </td> <td> n3 </td> <td> 2 </td> </tr> 
<tr> <td> n2 </td> <td> 3 </td> <td> n4 </td> <td> 1 </td> </tr> 
</tbody> 
</table> 

점수 헤더 중 하나를 클릭하면 두 점수 열 모두에서 점수를 정렬 할 수 있도록 테이블을 원합니다. 물론 이름에 대해서도 마찬가지입니다. 내 실제 응용 프로그램에서는 100 행을 가질 수 있으며, 문자가 2 열 밖에 안되며, 재구성 된 표는 20 행 (=> 10 열)이므로보다 쉽게 ​​볼 수 있습니다.

실제로 실제로 길고 좁은 테이블을 넓고 좁은 테이블로 나눠서 자동으로 넓힐 수있는 html 테이블이 필요 하겠지만, 이것은 너무 많이 요구됩니다. 나는 존재하지 않는다고 생각합니다.)

이런 능력을 가진 js (더 나은, jquery) tablesorter가 존재합니까?

/의거

+0

귀하의 코드가 이해가되지 않습니다. 일반 HTML을 게시하십시오. –

+0

길고 좁은 표를 넓고 짧은 표로 나누어 자동으로 넓힐 수있는 HTML 표가 무엇을 의미하는지 설명해 주시겠습니까? – Mottie

답변

0

이봐, 난 당신이 열을 정렬하는 tablesorter에 플러그인을 찾고 있습니다 및 데모 여기서 일하는 무엇을보고 생각한다 : 당신에게http://jsfiddle.net/f9VvL/

주어진 예에서 당신이 점수를 클릭하고 일어나고있는 정렬을 볼 수 있습니다.

경우 예리한 : http://tablesorter.com/docs/ 당신이 그렇게 CSS를 awesomness를 사용할 수있는 스타일링을위한

;

희망이 helsp 원인 :)

스크립트

<script type='text/javascript' src="http://mottie.github.com/tablesorter/js/jquery.tablesorter.js"></script> 


    <script type='text/javascript' src="http://mottie.github.com/tablesorter/js/jquery.tablesorter.widgets.js"></script> 


    <script type='text/javascript' src="http://mottie.github.com/tablesorter/addons/pager/jquery.tablesorter.pager.js"></script> 


    <link rel="stylesheet" type="text/css" href="http://mottie.github.com/tablesorter/addons/pager/jquery.tablesorter.pager.css"> 

코드

$('table').tablesorter({ 
    // *** Appearance *** 
    // fix the column widths 
    widthFixed: false, 
    // include zebra and any other widgets 
    widgets: ['zebra'], 
    // other options: "ddmmyyyy" & "yyyymmdd" 
    dateFormat: "mmddyyyy", 

    // *** Functionality *** 
    // starting sort direction "asc" or "desc" 
    sortInitialOrder: "asc", 
    // These are detected by default, 
    // but you can change or disable them 
    headers: { 
     // set "sorter : false" (no quotes) to disable the column 
     0: { sorter: "text" }, 
     1: { sorter: "digit" }, 
     2: { sorter: "text" }, 
     3: { sorter: "url" } 
    }, 
    // extract text from the table - this is how is 
    // it done by default 
    textExtraction: { 
     0: function(node) { 
      return $(node).text(); 
     }, 
     1: function(node) { 
      return $(node).text(); 
     } 
    }, 
    // forces the user to have this/these column(s) sorted first 
    sortForce: null, 
    // initial sort order of the columns 
    // [[columnIndex, sortDirection], ... ] 
    sortList: [], 
    // default sort that is added to the end of the users sort 
    // selection. 
    sortAppend: null, 
    // Use built-in javascript sort - may be faster, but does not 
    // sort alphanumerically 
    sortLocaleCompare: false, 
    // Setting this option to true will allow you to click on the 
    // table header a third time to reset the sort direction. 
    sortReset: false, 
    // Setting this option to true will start the sort with the 
    // sortInitialOrder when clicking on a previously unsorted column. 
    sortRestart: false, 
    // The key used to select more than one column for multi-column 
    // sorting. 
    sortMultiSortKey: "shiftKey", 

    // *** Customize header *** 
    onRenderHeader: function() { 
     // the span wrapper is added by default 
     $(this).find('span').addClass('roundedCorners'); 
    }, 
    // jQuery selectors used to find the header cells. 
    selectorHeaders: 'thead th', 

    // *** css classes to use *** 
    cssAsc: "headerSortUp", 
    cssChildRow: "expand-child", 
    cssDesc: "headerSortDown", 
    cssHeader: "header", 
    tableClass: 'tablesorter', 

    // *** widget css class settings *** 
    // column classes applied, and defined in the skin 
    widgetColumns: { 
     css: ["primary", "secondary", "tertiary"] 
    }, 
    // find these jQuery UI class names by hovering over the 
    // Framework icons on this page: 
    // http://jqueryui.com/themeroller/ 
    widgetUitheme: { 
     css: [ 
      "ui-icon-arrowthick-2-n-s", // Unsorted icon 
      "ui-icon-arrowthick-1-s", // Sort up (down arrow) 
      "ui-icon-arrowthick-1-n" // Sort down (up arrow) 
     ] 
    }, 
    // 
    widgetZebra: { 
     css: ["even", "odd"] 
    }, 

    // *** prevent text selection in header *** 
    cancelSelection: true, 

    // *** send messages to console *** 
    debug: false 

}).tablesorterPager({ 

    // target the pager markup - see the HTML block below 
    container: $(".pager"), 

    // use this format: "http:/mydatabase.com?page={page}&size={size}" 
    // where {page} is replaced by the page number and {size} is replaced 
    // by the number of records to show 
    ajaxUrl: null, 

    // process ajax so that the data object is returned along with 
    // the total number of rows 
    ajaxProcessing: function(ajax) { 
     if (ajax && ajax.hasOwnProperty('data')) { 
      // example ajax: 
      // { 
      // "data" : [{ "ID": 1, "Name": "Foo", "Last": "Bar" }], 
      // "total_rows" : 100 
      // } 
      // return [ "data", "total_rows" ]; 
      return [ajax.data, ajax.total_rows]; 
     } 
    }, 

    // output string - default is '{page}/{totalPages}'; 
    // possible variables: 
    // {page}, {totalPages}, {startRow}, {endRow} and {totalRows} 
    output: '{startRow} to {endRow} ({totalRows})', 

    // apply disabled classname to the pager arrows when the rows at 
    // either extreme is visible - default is true 
    updateArrows: true, 

    // starting page of the pager (zero based index) 
    page: 0, 

    // Number of visible rows - default is 10 
    size: 10, 

    // if true, the table will remain the same height no matter 
    // how many records are displayed. The space is made up by an empty 
    // table row set to a height to compensate; default is false 
    fixedHeight: false, 

    // remove rows from the table to speed up the sort of large tables. 
    // setting this to false, only hides the non-visible rows; needed 
    // if you plan to add/remove rows with the pager enabled. 
    removeRows: false, 

    // css class names of pager arrows 
    cssNext: '.next', 
    // next page arrow 
    cssPrev: '.prev', 
    // previous page arrow 
    cssFirst: '.first', 
    // go to first page arrow 
    cssLast: '.last', 
    // go to last page arrow 
    cssPageDisplay: '.pagedisplay', 
    // location of the "output" 
    cssPageSize: '.pagesize', 
    // dropdown that sets the "size" option 
    // class added to arrows when at the extremes 
    // (i.e. prev/first arrows are "disabled" when on the first page) 
    // Note there is no period "." in front of this class name 
    cssDisabled: 'disabled' 

});​