2015-07-13 52 views

回答

0

感謝我最終作出一個小部件,以當前的排序添加到哈希值。它沒有經過徹底測試,並且因this demo不能工作,因爲無法在內部幀上設置散列。它在整頁演示中工作。

/*! Widget: sort2Hash */ 
;(function($) { 
'use strict'; 
var ts = $.tablesorter || {}, 
s2h = { 
    init : function(c, wo) { 
     var arry, indx, len, column, direction, 
      sort = s2h.getSort(c, wo); 
     if (sort) { 
      arry = sort.split(wo.sort2Hash_separator); 
      len = arry.length; 
      sort = []; 
      for (indx = 0; indx < len; indx++) { 
       column = arry[ indx++ ]; 
       direction = arry[ indx ]; 
       if (typeof direction !== 'undefined') { 
        sort.push([ column, direction ]); 
       } 
      } 
      if (sort.length) { 
       c.sortList = sort; 
      } 
     } 
     c.$table.on('sortEnd.sort2hash', function() { 
      s2h.setHash(c, wo); 
     }); 
    }, 
    getTableId : function(c, wo) { 
     return wo.sort2Hash_tableId || 
      c.table.id || 
      'table' + $('table').index(c.$table); 
    }, 
    getSort : function(c, wo, clean) { 
     // modified original code from http://www.netlobo.com/url_query_string_javascript.html 
     var name = s2h.getTableId(c, wo).replace(/[\[]/, '\\[').replace(/[\]]/, '\\]'), 
      sort = (new RegExp('[\\#&]' + name + '=([^&]*)')).exec(window.location.hash); 
     if (sort === null) { 
      return ''; 
     } else { 
      if (clean) { 
       window.location.hash = window.location.hash.replace('&' + name + '=' + sort[ 1 ], ''); 
      } 
      return sort[ 1 ]; 
     } 
    }, 
    setHash : function(c, wo) { 
     var hash, indx, 
      arry = [], 
      tableId = s2h.getTableId(c, wo) + '=', 
      sort = c.sortList || [], 
      len = sort.length; 
     if (len) { 
      s2h.getSort(c, wo, true); // remove hash 
      window.location.hash += (window.location.hash.length ? '' : wo.sort2Hash_hash) + 
       '&' + tableId + 
       // flatten array, then join with separator 
       [].concat.apply([], sort).join(wo.sort2Hash_separator); 
     } 
    } 
}; 

ts.addWidget({ 
    id: 'sort2Hash', 
    options: { 
     sort2Hash_hash  : '#', // hash prefix 
     sort2Hash_separator : '-', // don't '#' or '=' here 
     sort2Hash_tableId : null // this option > table ID > table index on page 
    }, 
    init: function(table, thisWidget, c, wo) { 
     s2h.init(c, wo); 
    }, 
    remove: function(table, c) { 
     c.$table.off('sortEnd.sort2hash'); 
    } 
}); 

})(jQuery); 

$(function() { 

    $('table').tablesorter({ 
     theme: 'blue', 
     widgets: [ 'zebra', 'sort2Hash' ], 
     widgetOptions : { 
      // hash prefix 
      sort2Hash_hash  : '#', 
      // don't '#' or '=' here 
      sort2Hash_separator : ',', 
      // this option > table ID > table index on page 
      sort2Hash_tableId : null 
     } 
    }); 

}); 

我會在我的fork of tablesorter的下一次更新中包含此小部件。

+0

謝謝你爲我工作的這個真棒部件。 –

+0

我在這裏看到的一些問題是,當我移動單擊並選擇列排序我得到這樣的鏈接 http://localhost/test/run.php?pgm = 20150714#&resultTable = 0,0,1,0, 2,0,3,0,4,0 現在,當我從url中刪除此字符串(#&resultTable = 0,0,1,0,2,0,3,0,4,0)並重新加載頁面時字符串將追加到URL,而不是加載一個新的頁面,舊的頁面再次加載,我有我的排序結果。是否有可能加載頁面加載給定的網址,以防新的頁面加載? –

+0

此外,我看到,當瀏覽器緩存清除新頁面加載與給定的URL與附加結果表字符串。也爲我的代碼表頭'href = javascript:void(0)%s「,$ colnames [$列] 當我點擊表格標題時,Mozilla打開一個新的空白窗口 我在谷歌瀏覽器中看不到這種行爲 –

相關問題