Last active
May 23, 2016 11:13
-
-
Save Maxghp/26b2afa1cc9b6c7e6ebb64bfda1f7220 to your computer and use it in GitHub Desktop.
Example of ajax query
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//This ajax query is in getpunchtotal.phtml and call /application/index/getpunchadmin view and send user_id and data for | |
//getpunchadminAction, and that in turn returns. | |
$('.show-hide').click(function () { | |
event.preventDefault(); | |
var link = $(this); | |
var icon = link.find('span'); | |
var table = link.next('table'); | |
var user_id = link.attr('data-user_id'); | |
if (icon.hasClass('glyphicon-plus')) { | |
icon.addClass('glyphicon-minus').removeClass('glyphicon-plus'); | |
table.removeClass('hidden'); | |
// Check is empty table | |
$.ajax({ | |
'method': 'POST', | |
'url': '/application/index/getpunchadmin', | |
'data': { | |
user_id: user_id, | |
date_begin: $("#datepicker1").val(), | |
date_end: $("#datepicker2").val() | |
} | |
}).done(function (response) { | |
table.html(response); | |
}); | |
} else { | |
icon.addClass('glyphicon-plus').removeClass('glyphicon-minus'); | |
table.addClass('hidden'); | |
} | |
}); | |
//getpunchAdmin action get data from model and return view model in getpunchadmin.phtml | |
public function getpunchadminAction() | |
{ | |
$response = array(); | |
$model = $this->loadModel(); | |
if ($this->getRequest()->isPost()) { | |
$response['list'] = $model->getDataBySearchAdmin($this->params()->fromPost()); | |
} | |
$viewModel = new ViewModel($response); | |
$viewModel->setTerminal(true); | |
return $viewModel; | |
} | |
// this function grab what we need and return data to getpunchadminAction | |
public function getDataBySearchAdmin($date) | |
{ | |
$query = " | |
SELECT DATE_FORMAT(punchin_local, '%Y/%m/%d') as date, | |
DATE_FORMAT(punchin_local, '%h:%i %p') as punch_in, | |
DATE_FORMAT(punchout_local, '%h:%i %p') as punch_out, | |
total_mins as total, | |
user.user_id as id, | |
punch.punch_id as pid, | |
user_FN, user_LN | |
FROM punch | |
INNER JOIN user | |
ON user.user_id = punch.user_id | |
INNER JOIN location | |
ON location.location_id = user.location_id | |
WHERE DATE_FORMAT(punchin_local, '%Y-%m-%d') BETWEEN '{$date['date_begin']}' | |
AND '{$date['date_end']}' AND user.user_id='{$date['user_id']}' | |
"; | |
$punchall = $this->dbAdapter->query($query)->execute(); | |
return $punchall; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment