我使用Datatable作为我的候选人列表,并使用区和党名搜索候选人。我正在使用ajax。问题是当搜索没有任何结果时我无法显示“找不到结果”。我该怎么做? 码: 单击搜索按钮时:
$(document).on("click", "#submit_filter", function(){
$(this).attr('disabled', 'disabled');
$('input#loading').css('display', 'block');
var districtId = $("select#filter_district option:selected").val();
var partyId = $("select#filter_party option:selected").val();
var langId = $("input#hidden_lang").val();
var table = $.fn.dataTable.fnTables(true);
if ( table.length > 0 ) {
var oTable = $('#candidates_table').dataTable();
oTable.fnDestroy();
}
$("#candidates_table").DataTable({
"bSort": false,
"bProcessing": true,
"sAjaxSource": "<?php echo base_url('search_candidates') ?>"+"/"+districtId+"/"+partyId+"/"+langId,
"fnServerData": function ( sSource, aoData, fnCallback ) {
$.ajax({
"dataType": 'json',
"type": "POST",
"url": sSource,
"data": aoData,
"success": fnCallback
});
}
在search_candidates中,控制器代码为:
function index($districtId,$partyId,$langId){
$result = $this->search->SearchCandidates($langId, $districtId, $partyId);
$json_array = array();
$res = array("aaData"=>'');
if(isset($result) && !empty($result) && is_array($result)){
foreach($result as $ind=>$val){
$nos = $ind+1;
$data['S.Nos.'] = $nos;
$data['District'] = $val->district;
$data['Election Area Nos'] = $val->election_nos;
$data['Candidate Name'] = $val->candidate_name;
$data['Political Party'] = $val->party_name;
$data['Age'] = $val->age;
$data['Gender'] = $val->gender;
$array = array($nos, $district, $election_nos, $val->candidate_name, $val->party_name, $val->age, $val->gender);
array_push($json_array, $array);
}
$res['aaData'] = $json_array;
}
echo json_encode($res);
}
通过此获得的数据是:
{"aaData":[[1,"Kathmandu","1","Prakash Man Singh","Nepali Congress","58","M"],[2,"Kathmandu","2","Madhav Kumar Nepal","Nepal Communist Party(Markswadi-Leninwadi)","60","M"],[3,"Kathmandu","3","Rameshwor Fuyal","Nepal Communist Party (Ekikrit Markswadi-Leninwadi)","51","M"],[4,"Kathmandu","4","Gagan Kumar Thapa","Nepali Congress","37","M"],[5,"Kathmandu","5","Narhari Acharya","Nepali Congress","60","M"],[6,"Kathmandu","6","Bhimsen Das Pradhan","Nepali Congress","59","M"],[7,"Kathmandu","7","Ram Bir Manandhar","Nepal Communist Party (Ekikrit Markswadi-Leninwadi)","50","M"],[8,"Kathmandu","8","Nabindra Raj Joshi","Nepali Congress","52","M"],[9,"Kathmandu","9","Dhyan Govinda Ranjit","Nepali Congress","66","M"],[10,"Kathmandu","10","Rajendra Kumar K.C.","Nepali Congress","55","M"]]}
我如何解决这个问题?欢迎任何帮助/建议。
答案 0 :(得分:2)
Datatables负责这一点。您所要做的就是传递一个空数组[]
。从代码中我看到你有$res = array("aaData"=>'');
。这可能就是为什么你没有得到&#34;没有找到结果&#34;信息。我不知道php所以我无法建议确切的更改,但要获取消息json输出应该是这样的
{"aaData":[]}
干杯!!
答案 1 :(得分:0)
您将返回一个json值到页面,检查页面
decode_json($res);
和
if(empty($res)){
echo "No data found";
}
检查数组是否为空
array['']