Reputation: 2015
I am using Datatable for my candidate listing and search of candidates using district and party name. I am using ajax for this. The problem is when there is no any result for search I am not able to show 'No Result Found'. How am I to do this? Code: when search button is clicked:
$(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
});
}
In search_candidates controller code is:
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);
}
Data obtained thorough this is:
{"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"]]}
How am i to solve this? Any help/suggestins is welcome.
Upvotes: 2
Views: 3115
Reputation: 219
you are return a json value to the page,check to the page to
decode_json($res);
and
if(empty($res)){
echo "No data found";
}
check the array null or not
array['']
Upvotes: 0
Reputation: 2561
Datatables takes care of this. All you have to do is pass a empty array []
. From the code I see that you have $res = array("aaData"=>'');
. This might be the why you are not getting the "No results found" message. I dont know php so I am unable suggest the exact changes, but to get the message the json output should be like this
{"aaData":[]}
Cheers!!
Upvotes: 2