📜 ⬆️ ⬇️

Plugin to display a table with data. Flexigrid

The table we will do using jQuery- plugin " FlexiGrid ".
I am more than confident that experienced programmers are already familiar with this plugin, but as experience shows, some still do not know about such things.

This is actually an analogue of the well-known jqGrid plugin for jQuery .

Here are its main functions described by the creator:


At its core, the same as the popular jqGrid , only in a different interface. Here, the choice depends on which plugin more eyes pleases =) although there are still differences, you can find out about them if you use any more complex functions or need more subtle customization.
The plugin has been tested in the following browsers: IE6 / IE7, Firefox 2, Opera 9.x, Safari 3.0
')
image

I really liked the ability to generate a FlexGrid from a regular HTML table:
For example, we have one mogilnichok one table:
< table class ="our_tbl" >
< thead >
< tr >
< th > ID </ th >
< th > NAME </ th >
< th > CATEGORY </ th >
< th > DATE </ th >
</ tr >
</ thead >
< tbody >
< tr >
< td > This is data 1 with overflowing content </ td >
< td > This is data 2 </ td >
< td > This is data 3 </ td >
< td > This is data 4 </ td >
</ tr >
</ tbody >
</ table >


* This source code was highlighted with Source Code Highlighter .


For this, such code is enough
$( '.our_tbl' ).flexigrid();

* This source code was highlighted with Source Code Highlighter .


To use this feature you need:


Well, here's a working PHP implementation example.

The PHP code there is rather trivial and far from ideal, but the general principle is visible.

  1. $ ( "# flex1" ) .flexigrid
  2. (
  3. {
  4. url: 'post2.php' ,
  5. dataType: 'json' ,
  6. colModel: [
  7. {display: 'ID' , name: 'id' , width: 40, sortable: true , align: 'center' },
  8. {display: 'ISO' , name: 'iso' , width: 40, sortable: true , align: 'center' },
  9. {display: 'Name' , name: 'name' , width: 180, sortable: true , align: 'left' },
  10. {display: 'Printable Name' , name: 'printable_name' , width: 120, sortable: true , align: 'left' },
  11. {display: 'ISO3' , name: 'iso3' , width: 130, sortable: true , align: 'left' , hide: true },
  12. {display: 'Number Code' , name: 'numcode' , width: 80, sortable: true , align: 'right' }
  13. ],
  14. buttons: [
  15. {name: 'Add' , bclass: 'add' , onpress: test},
  16. {name: 'Delete' , bclass: 'delete' , onpress: test},
  17. {separator: true },
  18. {name: 'A' , onpress: sortAlpha},
  19. {name: 'B' , onpress: sortAlpha},
  20. {name: 'C' , onpress: sortAlpha},
  21. {name: 'D' , onpress: sortAlpha},
  22. {name: 'E' , onpress: sortAlpha},
  23. {name: 'F' , onpress: sortAlpha},
  24. {name: 'G' , onpress: sortAlpha},
  25. {name: 'H' , onpress: sortAlpha},
  26. {name: 'I' , onpress: sortAlpha},
  27. {name: 'J' , onpress: sortAlpha},
  28. {name: 'K' , onpress: sortAlpha},
  29. {name: 'L' , onpress: sortAlpha},
  30. {name: 'M' , onpress: sortAlpha},
  31. {name: 'N' , onpress: sortAlpha},
  32. {name: 'O' , onpress: sortAlpha},
  33. {name: 'P' , onpress: sortAlpha},
  34. {name: 'Q' , onpress: sortAlpha},
  35. {name: 'R' , onpress: sortAlpha},
  36. {name: 'S' , onpress: sortAlpha},
  37. {name: 'T' , onpress: sortAlpha},
  38. {name: 'U' , onpress: sortAlpha},
  39. {name: 'V' , onpress: sortAlpha},
  40. {name: 'W' , onpress: sortAlpha},
  41. {name: 'X' , onpress: sortAlpha},
  42. {name: 'Y' , onpress: sortAlpha},
  43. {name: 'Z' , onpress: sortAlpha},
  44. {name: '#' , onpress: sortAlpha}
  45. ],
  46. searchitems: [
  47. {display: 'ISO' , name: 'iso' },
  48. {display: 'Name' , name: 'name' , isdefault: true }
  49. ],
  50. sortname: "id" ,
  51. sortorder: "asc" ,
  52. usepager: true
  53. title: 'Countries' ,
  54. useRp: true
  55. rp: 10,
  56. showTableToggleBtn: true
  57. width: 700,
  58. height: 255
  59. }
  60. );
  61. });
  62. function sortAlpha (com)
  63. {
  64. jQuery ( '# flex1' ) .flexOptions ({newp: 1, params : [{name: 'letter_pressed' , value: com}, {name: 'qtype' , value: $ ( 'select [name = qtype]' ) .val ()}]});
  65. jQuery ( "# flex1" ) .flexReload ();
  66. }
  67. function test (com, grid)
  68. {
  69. if (com == 'Delete' )
  70. {
  71. if ($ ( '.trSelected' , grid) .length> 0) {
  72. if (confirm ( 'Delete' + $ ( '.trSelected' , grid) .length + 'items?' )) {
  73. var items = $ ( '.trSelected' , grid);
  74. var itemlist = '' ;
  75. for (i = 0; i <items.length; i ++) {
  76. itemlist + = items [i] .id.substr (3) + "," ;
  77. }
  78. $ .ajax ({
  79. type: "POST" ,
  80. dataType: "json" ,
  81. url: "delete.php" ,
  82. data: "items =" + itemlist,
  83. success: function (data) {
  84. alert ( "Query:" + data.query + "- Total affected rows:" + data.total);
  85. $ ( "# flex1" ) .flexReload ();
  86. }
  87. });
  88. }
  89. } else {
  90. return false ;
  91. }
  92. }
  93. else if (com == 'Add' )
  94. {
  95. alert ( 'Add New Item Action' );
  96. }
  97. }
* This source code was highlighted with Source Code Highlighter .


and PHP code:

  1. <? php
  2. function countRec ($ fname , $ tname , $ where ) {
  3. $ sql = "SELECT count ($ fname) FROM $ tname $ where" ;
  4. $ result = runSQL ($ sql );
  5. while ($ row = mysql_fetch_array ($ result )) {
  6. return $ row [ 0 ];
  7. }
  8. }
  9. $ page = $ _POST [ 'page' ];
  10. $ rp = $ _POST [ 'rp' ];
  11. $ sortname = $ _POST [ 'sortname' ];
  12. $ sortorder = $ _POST [ 'sortorder' ];
  13. if (! $ sortname ) $ sortname = 'name' ;
  14. if (! $ sortorder ) $ sortorder = 'desc' ;
  15. if ($ _POST [ 'query' ]! = '' ) {
  16. $ where = " WHERE` " . $ _POST [ 'qtype' ]. "` LIKE '% " . $ _POST [ ' query ' ]. "% '" ;
  17. } else {
  18. $ where = '' ;
  19. }
  20. if ($ _POST [ 'letter_pressed' ]! = '' ) {
  21. $ where = " WHERE` " . $ _POST [ 'qtype' ]. "` LIKE '" . $ _POST [ ' letter_pressed ' ]. "% '" ;
  22. }
  23. if ($ _POST [ 'letter_pressed' ] = = '#' ) {
  24. $ where = " WHERE` " . $ _POST [ 'qtype' ]. "REGEXP '[[:::]]'" ;
  25. }
  26. $ sort = "ORDER BY $ sortname $ sortorder" ;
  27. if (! $ page ) $ page = 1 ;
  28. if (! $ rp ) $ rp = 10 ;
  29. $ start = (($ page-1 ) * $ rp );
  30. $ limit = "LIMIT $ start, $ rp" ;
  31. $ sql = "SELECT id, iso, name, printable_name, iso3, numcode FROM country $ where $ sort $ limit" ;
  32. $ result = runSQL ($ sql );
  33. $ total = countRec ( 'iso' , 'country' , $ where );
  34. header ( "Expires: Mon, 26 Jul 1997 05:00:00 GMT" );
  35. header ( "Last-Modified:" . gmdate ( "D, d MYH: i: s" ). "GMT" );
  36. header ( "Cache-Control: no-cache, must-revalidate" );
  37. header ( "Pragma: no-cache" );
  38. header ( "Content-type: text / x-json" );
  39. $ json = "" ;
  40. $ json . = "{\ n" ;
  41. $ json . = "page: $ page, \ n" ;
  42. $ json . = "total: $ total, \ n" ;
  43. $ json . = "rows: [" ;
  44. $ rc = false ;
  45. while ($ row = mysql_fetch_array ($ result )) {
  46. if ($ rc ) $ json . = "," ;
  47. $ json . = "\ n {" ;
  48. $ json . = "id: '" . $ row [ ' id ' ]. "'," ;
  49. $ json . = "cell: ['" . $ row [ ' id ' ]. "','" . $ row [ 'iso' ]. "'" ;
  50. $ json . = ", '" . addslashes ($ row [ 'name' ]). "'" ;
  51. $ json . = ", '" . addslashes ($ row [ 'printable_name' ]). "'" ;
  52. $ json . = ", '" . addslashes ($ row [ 'iso3' ]). "'" ;
  53. $ json . = ", '" . addslashes ($ row [ 'numcode' ]). "']" ;
  54. $ json . = "}" ;
  55. $ rc = true ;
  56. }
  57. $ json . = "] \ n" ;
  58. $ json . = "}" ;
  59. echo $ json ;
  60. ? >
* This source code was highlighted with Source Code Highlighter .


I took the code from the example, did not alter it myself, a lot of things can be redone, especially the inconvenient assembly of the json string. It is much more convenient to use the json_encode () function.

This kind of tables are usually not very extensible, but for example, admin panel pages are very convenient to do, and most importantly quickly;)

PS: Crosspost from my blog

Source: https://habr.com/ru/post/79935/


All Articles