datatable with livedata

This commit is contained in:
2023-10-16 17:19:15 +07:00
parent a6ce730088
commit 9bea66a705
2 changed files with 181 additions and 126 deletions
+122 -125
View File
@@ -6,7 +6,7 @@
Desc. : v4 - HTML Table (jQuery & Bootstrap)
Created By : Siau Pheng ([email protected]).
Created Date : January 16th, 2008.
Last Modified : May 18th, 2023.
Last Modified : October 16th, 2023.
(c) 2008 - 2023, 3FONIA Software; WWW.3FONIA.COM.
@@ -17,30 +17,32 @@ declare(strict_types=1);
namespace siaupheng\fonia3\Web;
final class Table {
private $__table = "";
private $__tquery = "";
private $__tquery_footer = "";
private $__aheader = array();
private $__afield = array();
private $__astyle = array();
private $__aformat = array();
private $__afooter = array();
private $__aotype = array();
private $__arowcol = array();
private $__arow_func = array();
private $__aheader = [];
private $__afield = [];
private $__astyle = [];
private $__aformat = [];
private $__afooter = [];
private $__aotype = [];
private $__arowcol = [];
private $__arow_func = [];
private $__tfunc_select = "";
private $__tfunc_navi = "";
private $__btxtbutton = false;
private $__bnobutton = false;
private $__bscrollbar = false;
private $__acontrol = array('field'=>false, 'value'=>"");
private $__acbox = array();
private $__aoption = array();
private $__acontrol = ['field'=>false, 'value'=>""];
private $__acbox = [];
private $__aoption = [];
private $__npage_rec = 10;
private $__nreclimit = 0;
private $__nscrollheight = 0;
private $__ord_field = "";
private $__ord_dir = "A";
private $__tcss = "";
private $__livedata = false;
protected $__cdb = null;
public function __construct($db = null){
@@ -48,6 +50,10 @@ final class Table {
else { $this->__cdb = new \siaupheng\fonia3\DB\MySQL(); }
}
public function set_table($ttable = "") {
$this->__table = $ttable;
}
public function set_query($tquery = "") {
$this->__tquery = $tquery;
}
@@ -80,7 +86,7 @@ final class Table {
public function set_row_color($tfield = "", $tval = "", $tcss = "") {
if ($tfield=="") return;
$this->__arowcol[] = array('F'=>$tfield, 'V'=>$tval, 'C'=>$tcss);
$this->__arowcol[] = ['F'=>$tfield, 'V'=>$tval, 'C'=>$tcss];
}
public function set_func_navi($tfunc = "") {
@@ -92,7 +98,7 @@ final class Table {
}
public function set_func_btn($tfunc = "", $tlabel = "") {
$this->__arow_func[] = array('label' => $tlabel, 'func' => $tfunc);
$this->__arow_func[] = ['label' => $tlabel, 'func' => $tfunc];
}
public function set_row_control($tfield = "", $tvalue = "Y") {
@@ -101,26 +107,26 @@ final class Table {
}
public function add_row_form($tlabel = "", $tfunc = "", $tfield = "", $tvalue = "") {
$this->__aoption[] = array('type' => "button", 'func' => $tfunc, 'icon' => "fon-tabel-dt", 'label' => $tlabel, 'field' => $tfield, 'value' => $tvalue);
$this->__aoption[] = ['type' => "button", 'func' => $tfunc, 'icon' => "fon-tabel-dt", 'label' => $tlabel, 'field' => $tfield, 'value' => $tvalue];
}
public function add_row_btn($ticon = "", $tlabel = "", $tfunc = "", $tfield = "", $tvalue = "", $tfield_css = "", $avalue_css = array()) {
$this->__aoption[] = array('type' => "button", 'func' => $tfunc, 'icon' => $ticon, 'label' => $tlabel, 'field' => $tfield, 'value' => $tvalue, 'field_css' => $tfield_css, 'avalue_css' => $avalue_css);
public function add_row_btn($ticon = "", $tlabel = "", $tfunc = "", $tfield = "", $tvalue = "", $tfield_css = "", $avalue_css = []) {
$this->__aoption[] = ['type' => "button", 'func' => $tfunc, 'icon' => $ticon, 'label' => $tlabel, 'field' => $tfield, 'value' => $tvalue, 'field_css' => $tfield_css, 'avalue_css' => $avalue_css];
}
public function add_row_del($tlabel = "", $tfunc = "", $tfield = "Unix", $tvalue = "") {
$this->__aoption[] = array('type' => "button", 'func' => $tfunc, 'icon' => "x", 'label' => $tlabel, 'field' => $tfield, 'value' => $tvalue);
$this->__aoption[] = ['type' => "button", 'func' => $tfunc, 'icon' => "x", 'label' => $tlabel, 'field' => $tfield, 'value' => $tvalue];
}
public function add_row_edit($tlabel = "", $tfunc = "") {
$this->add_row_btn("note", $tlabel, $tfunc);
}
public function add_row_checkbox($tfield = "ID", $tcontrol = "Edit", $avalue = array("Y","N")) {
public function add_row_checkbox($tfield = "ID", $tcontrol = "Edit", $avalue = ["Y","N"]) {
$this->__acbox['field'] = $tfield;
$this->__acbox['control'] = $tcontrol;
$this->__acbox['avalue'] = $avalue;
$this->__aoption[] = array('type' => "checkbox", 'field' => $tfield, 'avalue' => $avalue);
$this->__aoption[] = ['type' => "checkbox", 'field' => $tfield, 'avalue' => $avalue];
}
public function no_button() {
@@ -137,11 +143,11 @@ final class Table {
}
public function json_table($nama_tabel = "tabel_", $baris_tabel = 10) {
$adata = array();
$adata = []; $njml_kol = 0;
// adding header
for ($i=0; $i<count($this->__afield); $i++) {
$adata['header'][] = array('f'=>$this->__astyle[$i], 'o'=>$this->__afield[$i], 'c'=>$this->__aheader[$i], 's'=>$this->__aotype[$i]);
$adata['header'][] = ['f'=>$this->__astyle[$i], 'o'=>$this->__afield[$i], 'c'=>$this->__aheader[$i], 's'=>$this->__aotype[$i]];
$njml_kol += ($this->__astyle[$i]=="hidden") ? 0 : 1;
}
@@ -158,144 +164,135 @@ final class Table {
} else {
$tval2 = ($this->__afooter[$i]<>"") ? $this->__afooter[$i] : $tval;
}
$adata['footer'][] = array('c'=>$tval2);
$adata['footer'][] = ['c'=>$tval2];
}
}
$acontent = array();
$this->__cdb->Query($this->__tquery);
while ($this->__cdb->Next()) {
$abaris = array();
$aoption = array('button'=>array());
if (count($this->__aoption)>0) {
if ((($this->__acontrol['field']===false) || ($this->__cdb->Row($this->__acontrol['field'])==$this->__acontrol['value'])) && $this->__bnobutton===false) {
// adding all option
for ($i=0; $i<count($this->__aoption); $i++) {
if ($this->__aoption[$i]['type']=="button") {
if (($this->__aoption[$i]['field']!="") && ($this->__cdb->Row($this->__aoption[$i]['field'])<>(trim("".$this->__aoption[$i]['value'])!="" ? $this->__aoption[$i]['value'] : $_SESSION['__WEB_USER']['UNIX']))) {} else {
$aoption['button'][] = array($this->__aoption[$i]['icon'], (empty($this->__aoption[$i]['field_css'])?'':$this->__aoption[$i]['avalue_css'][$this->__cdb->Row($this->__aoption[$i]['field_css'])]??''));
$acontent = [];
if (!$this->__livedata) {
$this->__cdb->Query($this->__tquery);
while ($this->__cdb->Next()) {
$abaris = [];
$aoption = ['button'=>[]];
if (count($this->__aoption)>0) {
if ((($this->__acontrol['field']===false) || ($this->__cdb->Row($this->__acontrol['field'])==$this->__acontrol['value'])) && $this->__bnobutton===false) {
// adding all option
for ($i=0; $i<count($this->__aoption); $i++) {
if ($this->__aoption[$i]['type']=="button") {
if (($this->__aoption[$i]['field']!="") && ($this->__cdb->Row($this->__aoption[$i]['field'])<>(trim("".$this->__aoption[$i]['value'])!="" ? $this->__aoption[$i]['value'] : $_SESSION['__WEB_USER']['UNIX']))) {} else {
$aoption['button'][] = [$this->__aoption[$i]['icon'], (empty($this->__aoption[$i]['field_css'])?'':$this->__aoption[$i]['avalue_css'][$this->__cdb->Row($this->__aoption[$i]['field_css'])]??'')];
}
}
}
}
// adding checkbox option
if (array_key_exists("field", $this->__acbox)) {
$aoption['checkbox']['field'] = $this->__cdb->Row($this->__acbox['field']);
$aoption['checkbox']['value'] = $this->__cdb->Row($this->__acbox['control']);
// adding checkbox option
if (array_key_exists("field", $this->__acbox)) {
$aoption['checkbox']['field'] = $this->__cdb->Row($this->__acbox['field']);
$aoption['checkbox']['value'] = $this->__cdb->Row($this->__acbox['control']);
}
}
}
}
if (count($this->__arowcol)>0) {//up: 2014-06-13
for ($i=0; $i<count($this->__arowcol); $i++) {
if ($this->__cdb->Row($this->__arowcol[$i]['F']) == $this->__arowcol[$i]['V']) $aoption['rowcss'] = $this->__arowcol[$i]['C'];
if (count($this->__arowcol)>0) {//up: 2014-06-13
for ($i=0; $i<count($this->__arowcol); $i++) {
if ($this->__cdb->Row($this->__arowcol[$i]['F']) == $this->__arowcol[$i]['V']) $aoption['rowcss'] = $this->__arowcol[$i]['C'];
}
}
}
$abaris[] = $aoption;
for ($i=0; $i<count($this->__afield); $i++) {
$tval = stripslashes($this->__cdb->Row($this->__afield[$i]));//up: 2015-01-05
if (is_array($this->__aformat[$i])) {
$tval = $this->__aformat[$i][$tval];
} else if (!is_null($this->__aformat[$i]) && function_exists($this->__aformat[$i])) {
$tval = call_user_func($this->__aformat[$i], $tval);
$abaris[] = $aoption;
for ($i=0; $i<count($this->__afield); $i++) {
$tval = stripslashes($this->__cdb->Row($this->__afield[$i]));//up: 2015-01-05
if (is_array($this->__aformat[$i])) {
$tval = $this->__aformat[$i][$tval];
} else if (!is_null($this->__aformat[$i]) && function_exists($this->__aformat[$i])) {
$tval = call_user_func($this->__aformat[$i], $tval);
}
$abaris[] = $tval;
}
$abaris[] = $tval;
$acontent[] = $abaris;
}
$acontent[] = $abaris;
}
// navigator
$anavi = array('func_navi' => $this->__tfunc_navi, 'button' => $this->__arow_func );
$anavi = [ 'func_navi' => $this->__tfunc_navi, 'button' => $this->__arow_func ];
$adata['type'] = "table";
$adata['id'] = $nama_tabel;
$adata['config'] = array('css' => $this->__tcss, 'row_count' => $baris_tabel, 'func_select' => $this->__tfunc_select, 'nobutton' => $this->__bnobutton, 'txtbutton' => $this->__btxtbutton, 'scrollbar' => $this->__bscrollbar, 'scroll_height' => $this->__nscrollheight, 'sort_col' => $this->__ord_field, 'sort_dir' => $this->__ord_dir);
$adata['option'] = array();
$adata['config'] = ['css' => $this->__tcss, 'row_count' => $baris_tabel, 'func_select' => $this->__tfunc_select, 'nobutton' => $this->__bnobutton, 'txtbutton' => $this->__btxtbutton, 'scrollbar' => $this->__bscrollbar, 'scroll_height' => $this->__nscrollheight, 'sort_col' => $this->__ord_field, 'sort_dir' => $this->__ord_dir, 'live' => $this->__livedata];
$adata['option'] = [];
foreach ($this->__aoption as $aval) {
if ($aval['type']!=="checkbox") unset($aval['field']); unset($aval['value']); unset($aval['field_css']); unset($aval['value_css']); $adata['option'][] = $aval;
}
$adata['navi'] = $anavi;
$adata['content'] = $acontent;
return $adata;
}
}
/*public function json_table_db($nama_tabel = "tabel_", $baris_tabel = 10) {//new
$adata = array();
$dbpage = \siaupheng\fonia3\Web\Param::Get($nama_tabel.'_pg', 0);
// adding header
for ($i=0; $i<count($this->__afield); $i++) {
$adata['header'][] = array('f'=>$this->__astyle[$i], 'o'=>$this->__afield[$i], 'c'=>$this->__aheader[$i]);//add width later
$njml_kol += ($this->__astyle[$i]=="hidden") ? 0 : 1;
}
public function db_table($nama_tabel = "tabel_", $baris_tabel = 10) {
$this->__livedata = true;
$id_draw = intval($_REQUEST['draw'] ?? 0);//
$i_start = intval($_REQUEST['start'] ?? 0);//
$i_length = intval($_REQUEST['length'] ?? 0);//
$i_order = intval($_REQUEST['order'] ?? 0);//
// adding footer
if ($this->__tquery_footer) {
$this->__cdb->Query($this->__tquery_footer);
$this->__cdb->Next();
for ($i=0; $i<count($this->__afield); $i++) {
$tval = $this->__cdb->Row($this->__afield[$i], 0);
if (is_array($this->__afooter[$i])) {
$tval2 = $this->__afooter[$i][$tval];
} else if (function_exists($this->__afooter[$i])) {
$tval2 = call_user_func($this->__afooter[$i], $tval);
} else {
$tval2 = ($this->__afooter[$i]<>"") ? $this->__afooter[$i] : $tval;
}
$adata['footer'][] = array('c'=>$tval2);
if ($id_draw > 0) {
$t_order = "ASC";
if ($i_order <> 0) {
$t_order = ($i_order[0]['dir']=="asc") ? "ASC" : "DESC";
$i_order = $i_order[0]['column'];
}
}
$sql_limit = ($i_start>=0 && $i_length != -1) ? " LIMIT ".$i_start.", ".$i_length : "";
$sql_order = ($this->__afield[$i_order]) ? " ORDER BY ".$this->__afield[$i_order]." ".$t_order : "";
$acontent = array();
$this->__cdb->Query($this->__tquery);
while ($this->__cdb->Next()) {
$abaris = array();
$aoption = array('button'=>array());
if (count($this->__aoption)>0) {
if ((($this->__acontrol['field']===false) || ($this->__cdb->Row($this->__acontrol['field'])==$this->__acontrol['value'])) && $this->__bnobutton===false) {
// adding all option
for ($i=0; $i<count($this->__aoption); $i++) {
if ($this->__aoption[$i]['type']=="button") {
if (($this->__aoption[$i]['field']!="") && ($this->__cdb->Row($this->__aoption[$i]['field'])<>(trim($this->__aoption[$i]['value'])!="" ? $this->__aoption[$i]['value'] : $_SESSION['__WEB_USER']['UNIX']))) {} else {
$aoption['button'][] = $this->__aoption[$i]['icon'];
$record = 0;
$this->__cdb->Query("SELECT COUNT(*) Item FROM ".$this->__table);
if ($this->__cdb->Next()) $record = intval($this->__cdb->Row("Item", 0));
$adata['draw'] = $id_draw;
$adata['recordsTotal'] = $record;
$adata['recordsFiltered'] = $record;
$adata['data'] = [];
$this->__cdb->Query("SELECT `".implode('`,`', $this->__afield)."` FROM ".$this->__table.$sql_order.$sql_limit);
while ($this->__cdb->Next()) {
$abaris = [];
$aoption = ['button'=>[]];
if (count($this->__aoption)>0) {
if ((($this->__acontrol['field']===false) || ($this->__cdb->Row($this->__acontrol['field'])==$this->__acontrol['value'])) && $this->__bnobutton===false) {
// adding all option
for ($i=0; $i<count($this->__aoption); $i++) {
if ($this->__aoption[$i]['type']=="button") {
if (($this->__aoption[$i]['field']!="") && ($this->__cdb->Row($this->__aoption[$i]['field'])<>(trim("".$this->__aoption[$i]['value'])!="" ? $this->__aoption[$i]['value'] : $_SESSION['__WEB_USER']['UNIX']))) {} else {
$aoption['button'][] = [$this->__aoption[$i]['icon'], (empty($this->__aoption[$i]['field_css'])?'':$this->__aoption[$i]['avalue_css'][$this->__cdb->Row($this->__aoption[$i]['field_css'])]??'')];
}
}
}
}
// adding checkbox option
if (array_key_exists("field", $this->__acbox)) {
$aoption['checkbox']['field'] = $this->__cdb->Row($this->__acbox['field']);
$aoption['checkbox']['value'] = $this->__cdb->Row($this->__acbox['control']);
// adding checkbox option
if (array_key_exists("field", $this->__acbox)) {
$aoption['checkbox']['field'] = $this->__cdb->Row($this->__acbox['field']);
$aoption['checkbox']['value'] = $this->__cdb->Row($this->__acbox['control']);
}
}
}
}
if (count($this->__arowcol)>0) {//up: 2014-06-13
for ($i=0; $i<count($this->__arowcol); $i++) {
if ($this->__cdb->Row($this->__arowcol[$i]['F']) == $this->__arowcol[$i]['V']) $aoption['rowcss'] = $this->__arowcol[$i]['C'];
if (count($this->__arowcol)>0) {//up: 2014-06-13
for ($i=0; $i<count($this->__arowcol); $i++) {
if ($this->__cdb->Row($this->__arowcol[$i]['F']) == $this->__arowcol[$i]['V']) $aoption['rowcss'] = $this->__arowcol[$i]['C'];
}
}
}
$abaris[] = $aoption;
for ($i=0; $i<count($this->__afield); $i++) {
$tval = stripslashes($this->__cdb->Row($this->__afield[$i]));//up: 2015-01-05
if (is_array($this->__aformat[$i])) {
$tval = $this->__aformat[$i][$tval];
} else if (function_exists($this->__aformat[$i])) {
$tval = call_user_func($this->__aformat[$i], $tval);
$abaris[] = $aoption;
for ($i=0; $i<count($this->__afield); $i++) {
$tval = stripslashes($this->__cdb->Row($this->__afield[$i]));//up: 2015-01-05
if (is_array($this->__aformat[$i])) {
$tval = $this->__aformat[$i][$tval];
} else if (!is_null($this->__aformat[$i]) && function_exists($this->__aformat[$i])) {
$tval = call_user_func($this->__aformat[$i], $tval);
}
$abaris[] = $tval;
}
$abaris[] = $tval;
$adata['data'][] = $abaris;
}
$acontent[] = $abaris;
} else {
$adata = $this->json_table($nama_tabel, $baris_tabel);
}
// navigator
$anavi = array('func_navi' => $this->__tfunc_navi, 'button' => $this->__arow_func );
$adata['type'] = "table";
$adata['id'] = $nama_tabel;
$adata['config'] = array('page'=>$dbpage, 'row_count' => $baris_tabel, 'func_select' => $this->__tfunc_select, 'nobutton' => $this->__bnobutton, 'txtbutton' => $this->__btxtbutton, 'scrollbar' => $this->__bscrollbar, 'scroll_height' => $this->__nscrollheight, 'sort_col' => $this->__ord_field, 'sort_dir' => $this->__ord_dir);
$adata['option'] = $this->__aoption;
$adata['navi'] = $anavi;
$adata['content'] = $acontent;
return $adata;
}*/
}
}
?>