Item table now loads with AJAX
This commit is contained in:
parent
b37576534b
commit
769ea75e82
109
lib/getitemtable.php
Normal file
109
lib/getitemtable.php
Normal file
@ -0,0 +1,109 @@
|
||||
<?php
|
||||
|
||||
require_once __DIR__ . '/../required.php';
|
||||
|
||||
dieifnotloggedin();
|
||||
|
||||
require_once __DIR__ . '/userinfo.php';
|
||||
|
||||
header("Content-Type: application/json");
|
||||
|
||||
$out = [];
|
||||
|
||||
$out['draw'] = intval($VARS['draw']);
|
||||
|
||||
$out['recordsTotal'] = $database->count('items');
|
||||
$filter = false;
|
||||
|
||||
// sort
|
||||
$order = null;
|
||||
$sortby = "DESC";
|
||||
if ($VARS['order'][0]['dir'] == 'asc') {
|
||||
$sortby = "ASC";
|
||||
}
|
||||
switch ($VARS['order'][0]['column']) {
|
||||
case 2:
|
||||
$order = ["name" => $sortby];
|
||||
break;
|
||||
case 3:
|
||||
$order = ["catname" => $sortby];
|
||||
break;
|
||||
case 4:
|
||||
$order = ["locname" => $sortby];
|
||||
break;
|
||||
case 5:
|
||||
$order = ["code1" => $sortby];
|
||||
break;
|
||||
case 6:
|
||||
$order = ["code2" => $sortby];
|
||||
break;
|
||||
case 7:
|
||||
$order = ["qty" => $sortby];
|
||||
break;
|
||||
// Note: We're not going to sort by assigned user. It's too hard. Maybe later.
|
||||
}
|
||||
|
||||
// search
|
||||
if (!is_empty($VARS['search']['value'])) {
|
||||
$filter = true;
|
||||
$wherenolimit = [
|
||||
"OR" => [
|
||||
"name[~]" => $VARS['search']['value'],
|
||||
"catname[~]" => $VARS['search']['value'],
|
||||
"locname[~]" => $VARS['search']['value'],
|
||||
"code1[~]" => $VARS['search']['value'],
|
||||
"code2[~]" => $VARS['search']['value']
|
||||
]
|
||||
];
|
||||
$where = $wherenolimit;
|
||||
$where["LIMIT"] = [$VARS['start'], $VARS['length']];
|
||||
} else {
|
||||
$where = ["LIMIT" => [$VARS['start'], $VARS['length']]];
|
||||
}
|
||||
if (!is_null($order)) {
|
||||
$where["ORDER"] = $order;
|
||||
}
|
||||
|
||||
|
||||
$items = $database->select('items', [
|
||||
'[>]categories' => ['catid' => 'catid'],
|
||||
'[>]locations' => ['locid' => 'locid']
|
||||
], [
|
||||
'itemid',
|
||||
'name',
|
||||
'catname',
|
||||
'locname',
|
||||
'loccode',
|
||||
'code1',
|
||||
'code2',
|
||||
'qty',
|
||||
'userid'
|
||||
], $where);
|
||||
|
||||
|
||||
$out['status'] = "OK";
|
||||
if ($filter) {
|
||||
$recordsFiltered = $database->count('items', [
|
||||
'[>]categories' => ['catid' => 'catid'],
|
||||
'[>]locations' => ['locid' => 'locid']
|
||||
], 'itemid', $wherenolimit);
|
||||
} else {
|
||||
$recordsFiltered = $out['recordsTotal'];
|
||||
}
|
||||
$out['recordsFiltered'] = $recordsFiltered;
|
||||
|
||||
$usercache = [];
|
||||
for ($i = 0; $i < count($items); $i++) {
|
||||
$items[$i]["editbtn"] = '<a class="btn btn-blue btn-xs" href="app.php?page=edititem&id=' . $items[$i]['itemid'] . '"><i class="fa fa-pencil-square-o"></i> ' . lang("edit", false) . '</a>';
|
||||
if (is_null($items[$i]['userid'])) {
|
||||
$items[$i]["username"] = "";
|
||||
} else {
|
||||
if (!isset($usercache[$items[$i]['userid']])) {
|
||||
$usercache[$items[$i]['userid']] = getUserByID($items[$i]['userid']);
|
||||
}
|
||||
$items[$i]["username"] = $usercache[$items[$i]['userid']]['name'];
|
||||
}
|
||||
}
|
||||
$out['items'] = $items;
|
||||
|
||||
echo json_encode($out);
|
@ -35,12 +35,9 @@ redirectifnotloggedin();
|
||||
'loccode',
|
||||
'code1',
|
||||
'code2',
|
||||
'text1',
|
||||
'text2',
|
||||
'text3',
|
||||
'qty',
|
||||
'userid'
|
||||
]);
|
||||
], ["LIMIT" => 100]);
|
||||
$usercache = [];
|
||||
foreach ($items as $item) {
|
||||
if (is_null($item['userid'])) {
|
||||
|
@ -131,6 +131,7 @@ function lang2($key, $replace, $echo = true) {
|
||||
function dieifnotloggedin() {
|
||||
if ($_SESSION['loggedin'] != true) {
|
||||
sendError("Session expired. Please log out and log in again.");
|
||||
die();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -22,9 +22,35 @@ $('#itemtable').DataTable({
|
||||
{
|
||||
targets: 1,
|
||||
orderable: false
|
||||
},
|
||||
{
|
||||
targets: 8,
|
||||
orderable: false
|
||||
}
|
||||
],
|
||||
order: [
|
||||
[2, 'asc']
|
||||
],
|
||||
serverSide: true,
|
||||
ajax: {
|
||||
url: "lib/getitemtable.php",
|
||||
dataFilter: function (data) {
|
||||
var json = jQuery.parseJSON(data);
|
||||
json.data = [];
|
||||
json.items.forEach(function (row) {
|
||||
json.data.push([
|
||||
"",
|
||||
row.editbtn,
|
||||
row.name,
|
||||
row.catname,
|
||||
row.locname + " (" + row.loccode + ")",
|
||||
row.code1,
|
||||
row.code2,
|
||||
row.qty,
|
||||
row.username
|
||||
]);
|
||||
});
|
||||
return JSON.stringify(json);
|
||||
}
|
||||
}
|
||||
});
|
Loading…
x
Reference in New Issue
Block a user