TableFlexigrid.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. class TableFlexigrid {
  3. private $_tbl = '';
  4. private $_cnf = '';
  5. private $_htmlID = '';
  6. private $_dataSource = null;
  7. function __construct($tblName, $tblConf) {
  8. $this->_tbl = $tblName;
  9. $this->_cnf = $tblConf;
  10. $this->_htmlID = $tblName;//uniqid($tblName);
  11. Lib::loadClass('Data_Source');
  12. $this->_dataSource = new Data_Source();
  13. $this->_dataSource->set_table($this->_tbl);
  14. }
  15. function getHtmlID() {
  16. return $this->_htmlID;
  17. }
  18. function render() {
  19. $visible_cols = $this->_cnf->getVisibleFieldList();
  20. $colModel = array();
  21. foreach ($visible_cols as $name) {
  22. $colModel[] = (object)array('display'=>$name, 'name'=>$name, 'width'=>strlen($name) * 5, 'sortable'=>true, 'align'=>'left');
  23. }
  24. $out = '';
  25. $out .= '<link rel="stylesheet" href="stuff/flexigrid/css/flexigrid.css" type="text/css" />' . "\n";
  26. $out .= '<script src="stuff/flexigrid/js/flexigrid.js"></script>' . "\n";
  27. $out .= '<form id="' . $this->_htmlID . 'Frm" style="display:none">' . "\n";
  28. $out .= 'Value 1 : <input type="text" name="val1" value="" autocomplete="off" /><br />' . "\n";
  29. $out .= 'Value 2 : Is a hidden input with value 3<input type="hidden" name="val2" value="3" /><br />' . "\n";
  30. $out .= 'Value 3 : ' . "\n";
  31. $out .= '<select name="val3">' . "\n";
  32. $out .= '<option value="1">One</option>' . "\n";
  33. $out .= '<option value="2">Two</option>' . "\n";
  34. $out .= '<option value="3">Three</option>' . "\n";
  35. $out .= '<option value="4">Four</option>' . "\n";
  36. $out .= '<option value="5">Five</option>' . "\n";
  37. $out .= '</select><br />' . "\n";
  38. $out .= 'Value 4 : <input type="checkbox" name="val4" id="val4" value="4" /><label for="val4">This will pass a value 4 if checked</label>' . "\n";
  39. $out .= '</p>' . "\n";
  40. $out .= '<p><input type="submit" value="Submit" /></p>' . "\n";
  41. $out .= '</form>';
  42. $out .= '<table id="' . $this->_htmlID . '" style="display:none"></table>' . "\n";
  43. $out .= '<script>' . "
  44. (function ($) {
  45. $.fn.flexGetOptions = function (p) {
  46. return this.each(function () {
  47. console.log('flexGetOptions..');
  48. if (this.grid) {
  49. console.log('this.grid');
  50. console.log(this.grid);
  51. console.log('this.p');
  52. console.log(this.p);
  53. }
  54. });
  55. };
  56. })(jQuery);
  57. " . '</script>' . "\n";
  58. $out .= '<script>' . "
  59. if (undefined === window.grids) {
  60. window.grids = [];
  61. }
  62. window.grid = jQuery('#{$this->_htmlID}').flexigrid({
  63. url: 'index-ajax.php?_tbl={$this->_tbl}',
  64. dataType: 'json',
  65. // colModel : [
  66. // {display: 'ISO', name : 'iso', width : 40, sortable : true, align: 'center'},
  67. // {display: 'Name', name : 'name', width : 180, sortable : true, align: 'left'},
  68. // {display: 'Printable Name', name : 'printable_name', width : 120, sortable : true, align: 'left'},
  69. // {display: 'ISO3', name : 'iso3', width : 130, sortable : true, align: 'left', hide: true},
  70. // {display: 'Number Code', name : 'numcode', width : 80, sortable : true, align: 'right'}
  71. // ],
  72. colModel : " . json_encode($colModel) . ",
  73. buttons : [
  74. {name : 'Add', bclass : 'add', onpress : test },
  75. {name : 'Delete', bclass : 'delete', onpress : test },
  76. {separator : true}
  77. ],
  78. searchitems : [
  79. {display: 'ISO', name : 'iso'},
  80. {display: 'Name', name : 'name', isdefault: true}
  81. ],
  82. sortname: 'iso',
  83. sortorder: 'asc',
  84. usepager: true,
  85. title: '{$this->_tbl}',
  86. useRp: true,
  87. rp: 15,
  88. showTableToggleBtn: true,
  89. onSubmit: addFormData,
  90. width: 700,
  91. height: 200
  92. });
  93. function test(com, grid) {
  94. if (com == 'Delete') {
  95. confirm('Delete ' + $('.trSelected', grid).length + ' items?')
  96. } else if (com == 'Add') {
  97. alert('Add New Item');
  98. }
  99. }
  100. //This function adds paramaters to the post of flexigrid. You can add a verification as well by return to false if you don't want flexigrid to submit
  101. function addFormData(){
  102. //passing a form object to serializeArray will get the valid data from all the objects, but, if the you pass a non-form object, you have to specify the input elements that the data will come from
  103. var dt = $('#{$this->_htmlID}Frm').serializeArray();
  104. $('#{$this->_htmlID}').flexOptions({params: dt});
  105. return true;
  106. }
  107. $('#{$this->_htmlID}Frm').submit(function (){
  108. $('#{$this->_htmlID}').flexOptions({newp: 1}).flexReload();
  109. return false;
  110. });
  111. " . '</script>' . "\n";
  112. return $out;
  113. }
  114. }