到目前为止,这是我的语义UI API-设置:
$.fn.api.settings.api = {
'search' : '/api/search.api.php?query={value}'
};
$(document).ready(function() {
$('.searchterms input')
.api({
action : 'search',
stateContext : '.ui.input',
minCharacters : 3
});
});
我的search.api.php
触发一个mysql查询,结果应该加载<div id="#results">
。作为模板引擎,我使用smarty,因此search.api.php
包括以下行:
foreach($results AS $aresult) {
$smarty->display('single_result.tpl');
}
现在我的问题是:如何使用jQuery的load()来处理它?它应该是类似的东西
$("#results").load(...???);
好吧,我找到了一个解决方案,它运行良好。看看这些片段
Javascript:
$.fn.api.settings.api = {
'search' : '/api/search.api.php?query={value}'
};
$.fn.api.settings.successTest = function(response) {
if(response && response.success) {
return response.success;
}
return false;
};
$('.searchterms input')
.api({
action : 'search',
stateContext: '.ui.input',
minCharacters : 3,
successTest: function(response) {
return response.success || false;
},
onComplete: function(response) {
$("#items").html(response.items);
$(".results").html(response.data);
}
})
;
正如您所看到的,该解决方案可以通过回调onComplete
来实现。而且,重要的是,我没有使用load()
,而是使用html()来简单地替换<div class="results">
区域中的html代码:
onComplete: function(response) {
$("#items").html(response.items);
$(".results").html(response.data);
}
PHP(search.api.PHP):
$output='';
foreach($results AS $result) {
$smarty->assign("result", $result);
$output .= $smarty->fetch('single_result.tpl');
}
$response=json_encode(array(
'success' => true,
'message' => 'Okay',
'items' => $entries_found,
'data' => $output
));
echo $response;
在search.In.php中,重要的是不要使用$smarty->display()
,而是使用$smarty->fetch()
,并将结果捕获到变量$output
。可以返回此变量。