各位大大好:
目前頁面想要用ajax做資料換頁及頁籤切換的function,但目前寫出來要到下頁時資料無法顯示,謝謝!
JS程式碼如下:
if(method == "littlegreenman/list"){
var supplyCatesId = getDataValue("supplyCatesId");
var keyword = decodeUrlStr(getDataValue("keyword"));
var dataIndex = 0;
var dataCnt = 20;
$("#tran-back-btn").attr("href",referrer);
if(getDataValue("supplyCatesId") != null){
supplyCatesId = getDataValue("supplyCatesId");
}
if(getDataValue("keyword") != null){
var eventLabel = keyword;
keyword = decodeUrlStr(getDataValue("keyword"));
}
var total = $("#page-zone").data("total");
var totalPage = Math.ceil(total/dataCnt);
var nowPage = 1;
var pageArray = pageSet(nowPage,totalPage);
var beginPage = pageArray[0];
var endPage = pageArray[1];
pageZoneSet(nowPage,beginPage,endPage,totalPage,total,dataCnt,"");
$("#page-zone").find(".page-btn").unbind("click");
$("#page-zone").find(".page-btn").bind("click",function(){
var selectPageIndex = parseInt($(this).attr("alt"));
selectGreenmanByCates(supplyCatesId,keyword,selectPageIndex,dataCnt);
});
$("#page-next").unbind("click");
$("#page-next").bind("click",function(){
var nextPageIndex = (nowPage)*dataCnt;
selectGreenmanByCates(supplyCatesId,keyword,nextPageIndex,dataCnt);
});
}
function transLittleGreenmanListPage(){
var supplyCatesId = decodeUrlStr(getDataValue("supplyCatesId"));
var keyword = $("#search-text").val();
saveGreenmanUrl(supplyCatesId,keyword);
selectGreenmanByCates(supplyCatesId,keyword);
}
function saveGreenmanUrl(supplyCatesId,keyword){
var data_json = {};
var appendData = "";
var saveLink = relativeNewPath+"littlegreenman/list";
supplyCatesId = decodeUrlStr(getDataValue("supplyCatesId"));
keyword = decodeUrlStr(keyword);
if(supplyCatesId != null && supplyCatesId != ""){
if(appendData == ""){
appendData = appendData+"?";
}else{
appendData = appendData+"&";
}
appendData = appendData+"supplyCatesId="+supplyCatesId;
data_json['supplyCatesId'] = supplyCatesId;
}
if(keyword != null && keyword != ""){
if(appendData == ""){
appendData = appendData+"?";
}else{
appendData = appendData+"&";
}
appendData = appendData+"keyword="+keyword;
data_json['keyword'] = keyword;
}
saveLink = saveLink+appendData;
save_url(data_json, document.title, saveLink);
}
function selectGreenmanByCates(supplyCatesId,keyword,dataIndex,dataCnt){
$("#search-list").html("");
$("#page-zone").html("");
$.ajax({
url : relativeNewPath+"ajax/selectGreenmanByCates",
type : "GET",
dataType : "json",
data:{
supplyCatesId : supplyCatesId,
keyword : keyword,
dataIndex : dataIndex,
dataCnt : dataCnt
},
error : function(data) {
$("#loading").hide();
console.log("selectGreenmanByCates","error");
},
success : function(data) {
console.log("selectGreenmanByCates",data.status);
$("#loading").hide();
if(data.status!="selectGreenmanByCatesSuccess"){
$(".data-total").html(0);
$("#page-zone").attr("data-total",0);
$("#page-zone").data("total",0);
$("#search-list").html("<tr><td>查無資訊</td></tr>");
}else{
var total = data.total;
$(".data-total").html(total);
$("#page-zone").attr("data-total",total);
$("#page-zone").data("total",total);
var totalPage = Math.ceil(total/dataCnt);
var nowPage = (dataIndex/dataCnt)+1;
var pageArray = pageSet(nowPage,totalPage);
var beginPage = pageArray[0];
var endPage = pageArray[1];
pageZoneSet(nowPage,beginPage,endPage,totalPage,total,dataCnt,"");
$("#page-prev").unbind("click");
$("#page-prev").bind("click",function(){
var prevPageIndex = (nowPage-2)*dataCnt;
selectGreenmanByCates(supplyCatesId,keyword,prevPageIndex,dataCnt);
});
$("#page-zone").find(".page-btn").unbind("click");
$("#page-zone").find(".page-btn").bind("click",function(){
var selectPageIndex = parseInt($(this).attr("alt"));
selectGreenmanByCates(supplyCatesId,keyword,selectPageIndex,dataCnt);
});
$("#page-next").unbind("click");
$("#page-next").bind("click",function(){
var nextPageIndex = (nowPage)*dataCnt;
selectGreenmanByCates(supplyCatesId,keyword,nextPageIndex,dataCnt);
});
for(var g = 0 ;g < data.greenman.length ; g++){
var nutritionid = data.greenman[g].nutritionid;
var genericName = decodeUrlStr(data.greenman[g].nutrition);
var nutrition = data.greenman[g].nutrition;
var greenmanLink = relativeNewPath+"littlegreenman/information?nutritionid="+nutritionid;
$("#search-list").append(
"<tr>" + "<td class=\"ellipsis\" style=\"width:25%; \"><a target=\"_blank\" href=\""+greenmanLink+"\">"+genericName+"</a></td>" + "</tr>"
);
}
}
}
});
}
PHP程式碼如下:
<?php
/*******$_GET supplyCatesId and keyword********/
if(isset($_GET["keyword"])){
$searchType="keyword";
$searchval=$_GET["keyword"];
$selectType_n=$searchval;
}
elseif($_GET["supplyCatesId"]!=""){
$searchType="benefitIds";
$searchval=$_GET["supplyCatesId"];
$greenmanListResult = selectGreenmanBenefitList($requestLanguage,$databaseIP,$database,$databasePort);
while(list($k,$v)=each($greenmanListResult["SupplyCates"])){
$shortName[$v["supplyCatesId"]]=$v["supplyCatesShort"];
$CateName[$v["supplyCatesId"]]=$v["supplyCatesShort"];
}
$searchcates=explode(",",$searchval);
updateGreenmanBenefitClicks($searchcates,$requestLanguage,$databaseIP,$database,$databasePort); //----增加click數
$selectType_n=array();
foreach($searchcates as $scv){
if(!in_array($shortName[$scv],$selectType_n)) $selectType_n[]=$shortName[$scv];
}
$selectType_n=implode(",",$selectType_n);
}
$dataIndex=0;
$dataCnt=20;
$selectResult=selectGreenmanByCates($searchType,$searchval,$dataIndex,$dataCnt,$requestLanguage,$databaseIP,$database,$databasePort);
$dataarr=array();
if($selectResult!="selectGreenmanByCatesFalse"){
$totalRows=$selectResult["total"];
foreach($selectResult["greenman"] as $val){
$dataarr[]= "
<td class=\"ellipsis\" style=\"width:25%; \">
<a target=\"_blank\" href=\"".$relativeNewPath."littlegreenman/information?nutritionid=".$val["nutritionid"]."\">".$val["nutrition"]."</a>
</td> ";
}
}
?>
請善用f12大法。請善用f12大法。請善用f12大法。(很重要,講三次)
你就會發現你的問題在哪。
其實你的問題看來你並未正確的傳回json格式的資料。
理論上你的ajax因該是出錯的才對。
先確定好你的回傳值。