2016-03-24 8 views
0

Ich habe eine Dashboard-Seite, um die letzten fünf Tage Anwesenheitsdaten aus der Datenbank anzuzeigen. Dafür Skript aus Aufruf einer Funktion, die ich in separaten .js-Datei definiert. Mit der PHP-Funktion senden Sie die Daten zurück an Javascript als Callbackdata. Ich bin in Javascript, ich weiß nicht, wie man diese Array-Daten verarbeitet. Eigentlich habe ich diese Idee von einer Beispielanwendung, die gut mit Strings funktioniert, aber ich konnte nicht für die Arrays codieren. Index.php SeiteSenden von Array als Callbackdata in JS von PHP

 <div class="inner">        
      <table class="table table-borderless table-condensed "> 
      <thead><tr><th>Date</th><th>Total</th></tr></thead> 
      <tbody id ="attendancelist">      
      </tbody> 
      </table> 
     </div> 
    <script> 
    modJs.getlastfiveattendance(); 
    </script> 

JS Datei:

Display.method('getlastfiveattendance', function() { 
    var that = this; 
    var object = {"emp_id":empId}; 
    var reqJson = JSON.stringify(object); 
    var callBackData = []; 
    callBackData['callBackData'] = []; 
    callBackData['callBackSuccess'] = 'getlastPunchSuccessCallBack'; 
    callBackData['callBackFail'] = 'getlastPunchFailCallBack'; 
    this.customAction('getLastfive','modules=attendance',reqJson,callBackData); 
}); 

Display.method('getlastPunchSuccessCallBack', function(callBackData) { 
    var punches = callBackData; 
    $('#attendancelist').html(''); 
    var row = '<tr><td>_date_</td><td>_total_</td></tr>'; 

    $.each(punches, function(key, value) { //here i have to process this array to get each data 
    var trow = row;  
     trow = trow.replace(/_date_/g,Date.parse(key).toString('MMM d, yyyy (dddd)')); 
     trow = trow.replace(/_total_/g,value); 
     html += trow; 
    }); 
    $('#attendancelist').html(html);   

});  
Display.method('getlastPunchFailCallBack', function(callBackData) {  
    this.showMessage("error","Failed to Retrieve data");   
}); 

In PHP-Datei im Datum bekommen und es als Array übergeben, ich brauche nur Datum und Gesamtstunden an diesem Datum.

public function getLastfive($req){   
     empid = $req->emp_id;   
     $attendance = new Attendance(); 
     $attendanceList = $attendance->Find("employee = ? ORDER BY id DESC LIMIT 5",array(empid));   
     $dayMap = array();   
     foreach($attendanceList as $attendance){ 
      $dayMap[$attendance->in_time] = $attendance->note; //emp in_time as key and the total time as value 
     } 
     return new simRes(simRes::SUCCESS,$dayMap);   
    } 

Aber hier kommt die Kontrolle nicht in $ .each Schleife. WANN überprüfe ich mit dem folgenden Code, ob die Punches Array sind oder nicht, indem ich den folgenden Code verwende. IT zeigt als nicht Array.

if(Object.prototype.toString.call(punches) === '[object Array]') { 
    this.showMessage("yes","It's array"); 
    } 
    else 
     this.showMessage("No","Its no array"); 

Innen simRes Klasse:

class SimRes{ 
    const SUCCESS = "SUCCESS"; 
    const ERROR = "ERROR"; 
    var $status; 
    var $data; 
    public function __construct($status,$data = null){ 
     $this->status = $status;  
     $this->data = $data;  
    } 
    public function getStatus(){ 
     return $this->status; 
    } 
    public function getData(){ 
     return $this->data; 
    } 
    public function getJsonArray(){ 
     return array("status"=>$this->status,"data"=>$this->data); 
    } 
} 
+0

neues simRes (simRes :: SUCCESS, $ dayMap); - Es antwortet mit JSON? Kannst du eine Antwort geben? – num8er

+0

tun dies: Display.method ('getlastPunchSuccessCallBack', Funktion (CallBackData) {console.log (callBackData); öffnen Sie Inspektor-Panel und geben Sie uns console.log – num8er

+0

Es zeigt als 'null' in Inspektor-Panel – Anu

Antwort

0

versuchen Sie dies:

in Ihrem PHP Teil:

public function getLastfive($req){   
    $attendance = new Attendance(); 
    $attendanceList = $attendance->Find("employee = ? ORDERBY id DESC LIMIT 5)",array((int)$req->emp_id));  
    $dayMap = array(); 
    // generating array of attendances with key value 
    foreach($attendanceList as $attendance){ 
     $dayMap[] = [ 
      'date' => $attendance->in_time, 
      'total' => $attendance->note 
     ]; 
    } 

    return new simRes(simRes::SUCCESS, json_encode($dayMap)); // I don't know if it's already json-ifies result, but if Yes, so remove json_encode 
} 

in Ihrem HTML (enthalten lodash lib, es erleichtert die Arbeit mit JS):

<script src="https://raw.githubusercontent.com/lodash/lodash/4.6.1/dist/lodash.min.js"></script> 

und in Ihrem JS-Code:

Display.method('getlastPunchSuccessCallBack', function(callBackData) { 
    $('#attendancelist').html(''); // making empty before processing data 

    // if response is string, it happens when You don't send application/json content type on response headers 
    if(_.isString(callBackData)) { 
     callBackData = JSON.parse(callBackData); // parse string make it json 
    } 
    if(!_.isArray(callBackData)) { // if it's not array of objects, so stop here 
     return; 
    } 

    var punches = callBackData; 

    var html = ''; // create empty var to generate html 
    _.each(punches, function(punch) { 
     // iterate array of punches and pass punch to iterator function 
     // punch is object like {date: "some date", total: "some value"} 

     html+='<tr>'; 
     html+='<td>'+Date.parse(punch.date).toString('MMM d, yyyy (dddd)')+'</td>'; 
     html+='<td>'+punch.total+'</td>'; 
     html+='</tr>'; 
    }); 

    $('#attendancelist').html(html); 
}); 
+0

kann es Probleme mit dem Code sein, denn nicht getestet haben, aber Sie können Logik verstehen – num8er

+0

ich versuchte diesen Code durch Ändern der PHP-Datenbankabfrage, tatsächlich gibt es einen Fehler . Jetzt, wenn ich im Inspektor-Panel sehe, druckt console.log alle Daten im Format: [{"date": "2016-01-29 08:45:17", "total": "09:31:37"}, {"Datum": "2016-01-28 08:58:25", "gesamt": "09:49:23"}, {"Datum": "2016-01-27 08:55:48", "" total ":" 09:27:35 "}, {" date ":" 2016-01-25 09:07:25 "," gesamt ":" 08:59:10 "}, {" date ":" 2016 -01-23 09:14:33 "," total ":" 07:11:44 "}] – Anu

+1

Es funktioniert.Th Ank you.Ich habe heute eine neue Sache gelernt. – Anu

Verwandte Themen