2013-04-10 248 views
1

我有一個php頁面,它顯示來自多個MySQL查詢的表格,並從它們使用JavaScript函數對列結果進行排序,所有工作正常,我的問題是我需要刷新結果這些查詢每隔10秒左右工作正常(使用元刷新),問題是在列排序後進行刷新。當頁面刷新排序也被重置。這裏是一個排序功能的片段;

<script> 
    function tablesort(which){ <-----I tried using the $_GET method you suggested 
           <-----But i get a "missing formal parameter" error 
           <-----When also using this suggestion and use the 
           <-----"onclick" i get a "tablesort" is not defined 
           <-----error 
$(document).ready(function(){ 
if(which == '1.0'){<!--This sorts the pause row, descending --> 
$("#Mtable").tablesorter({sortList: [[1,0]]}); 
} 
if(which == '2.1'){<!--This sorts the total dialer row, descending --> 
$("#Mtable").tablesorter({sortList: [[2,1]]}); 
} 
if(which == '3.0'){<!--This sorts Wrap-up time row, descending --> 
$("#Mtable").tablesorter({sortList: [[3,0]]}); 
} 
if(which == '4.1'){<!--This sorts donation amount row, descending --> 
$("#Mtable").tablesorter({sortList: [[4,1]]}); 
} 
if(which == '5.1'){<!--This sorts Up-sale row, descending --> 
$("#Mtable").tablesorter({sortList: [[5,1]]}); 
} 
if(which == '6.1'){<!--This sorts the Monthl donation row, descending --> 
$("#Mtable").tablesorter({sortList: [[6,1]]}); 
} 
if(which == '7.1'){<!--This sorts the verified sales row, descending --> 
$("#Mtable").tablesorter({sortList: [[7,1]]}); 
} 
if(which == '8.1'){<!--This sorts the calles per hour row, descending --> 
$("#Mtable").tablesorter({sortList: [[8,1]]}); 
} 
if(which == '9.1'){<!--This sorts the payments per hour row, descending --> 
$("#Mtable").tablesorter({sortList: [[9,1]]}); 
} 
if(which == '10.1'){<!--This sorts the average sale row, descending --> 
$("#Mtable").tablesorter({sortList: [[10,1]]}); 
} 
if(which == '11.1'){<!--This sorts the sales total row, descending --> 
$("#Mtable").tablesorter({sortList: [[11,1]]}); 
} 
    }); 
    } 
    </script> 

這裏有那種因爲刷新IM想從onlcick傳遞變量數據類似於_GET $因此它的URL的表」

 Sort by: 
<a onclick="tablesort('1.0')"> Lowest Pause<a/>&nbsp &nbsp 
<a onclick="tablesort('2.1')"> Highest Dialer<a/>&nbsp &nbsp 
<a onclick="tablesort('3.0')"> Best Wrap-up<a/>&nbsp &nbsp 

的鏈接會像然後讀入排序功能;

本地主機/ dbtabke.php?它= 2.1 < ----使用完全相同的網址例如

如何做到這一點任何幫助將不勝感激,謝謝提前。

@prabeen義理我所提供的完整功能,再次感謝

+0

'$獲得(的 'localhost/dbtabke.php?Mtable =' + sort_by_var,...)'? – 2013-04-10 15:38:27

+0

您希望傳遞查詢變量作爲通過PHP頁面重新載入數據的AJAX請求的一部分。 – 2013-04-10 15:36:42

+0

@Mathew Darnell,我想通過URL將「」tablesort('1.0')「數據傳遞給javascript函數,因爲它的刷新部分 – Iz3k34l 2013-04-10 15:39:07

回答

0

最後我做以下使用$ _GET ,這裏是代碼;

<body> 
    <!--This gets the element from the URL to set the Sorting, so the page 
     can be refreshed without losing the sorting--> 
    <body onload="sorttable.innerSortFunction.apply(document.getElementById('<?php echo $_GET["id"]; ?>'), [])"> 

    <!--table headers that are used to determine columns to sort by--> 
    echo "<th id=\"dialertime\">".ucfirst("Dial Time")."</th>\n"; 
    echo "<th id=\"pausetime\">".ucfirst("Pause Time")."</th>\n"; 

    ...*other table info* 
    <!--links that sort the specific row--> 
    <a href="?id=wrap-by-time"> Wrap-up time<a/>&nbsp &nbsp 
    <a href="?id=wrap-by-percent"> Wrap-up %<a/>&nbsp &nbsp 
    </body> 

感謝每個人的投入,希望它有助於

0

你不一定必須使用GET方法保留排序順序。

您也可以使用cookie來存儲排序順序。這樣您的代碼看起來會更清晰。

function setCookie(c_name,value,exdays) 
{ 
var exdate=new Date(); 
exdate.setDate(exdate.getDate() + exdays); 
var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString()); 
document.cookie=c_name + "=" + c_value; 
} 

function getCookie(c_name) 
{ 
var i,x,y,ARRcookies=document.cookie.split(";"); 
for (i=0;i<ARRcookies.length;i++) 
{ 
    x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("=")); 
    y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1); 
    x=x.replace(/^\s+|\s+$/g,""); 
    if (x==c_name) 
    { 
    return unescape(y); 
    } 
    } 
} 

電話時,排序順序,並通過排序順序爲參數的setCookie()功能。

當頁面首次加載或刷新時,調用相同的排序函數並調用getCookie()以獲取cookie值並在排序前將其設置爲排序順序。

如果你想使用GET,當頁面刷新時,這也會做 工作,我希望你在文檔準備就緒時調用這個函數tablesort()

tablesort('<?php print $_GET['MTable']?>') ; 

注:請檢查參數是否有效的tablsort()功能,因爲在頁面加載時的第一次,GET變量將是空的,我相信