In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article will explain in detail how to use jQuery to design the design table base class. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.
Step 1 Design the base class for generating the table
We want to design a base class for generating tables, which can automatically generate corresponding data tables for any table in any database. For example, we only need to pass in the table name in the database or the index field of the table to generate the table. The code will be written around this base class for most of this article, and here is a snippet definition of the code:
Class Datagrid {private $hide_pk_col = true; private $hide_cols = array (); private $tbl_name =''; private $pk_col =''; private $headings = array (); private $tbl_fields = array ();}? >
Some attribute variables are defined first, such as whether to hide the primary key column, the table name $tbl_name, the header column $headings, and the table field array $tbl_fields. Here, we define this base class as the helper helper class in CI, because if it is defined as library in CI, it is not easy to pass parameters to its constructor.
Next, write its constructor as code such as:
Public function _ construct ($tbl_name, $pk_col = 'id') {$this- > CI = & get_instance (); $this- > CI- > load- > database (); $this- > tbl_fields = $this- > CI- > db- > list_fields ($tbl_name); if (! in_array ($pk_col,$this- > tbl_fields)) {throw new Exception ("Primary key column' $pk_col' not found in table'$tbl_name'");} $this- > tbl_name = $tbl_name $this- > pk_col = $pk_col; $this- > CI- > load- > library ('table');}
In the constructor of the above code, you receive two parameters, the name of the database table and the primary key ($id by default here). Then initialize and instantiate the CI object, and then call its load database and load table help methods, resulting in $this- > tbl_fields is the field of its database. Then determine whether the primary key $pk_col is in the data table, throw an exception if it doesn't exist, and use the member variables tbl_name and pk_col to save the table name and primary key of the data, respectively, so that you don't have to access the database next time. *, use the help table class of $this- > CI- > load- > library ('table') to generate the fields of the database into a simple table of HTML.
In order to customize the column title, there is also a method as follows:
Public function setHeadings (array $headings) {$this- > headings = array_merge ($this- > headings, $headings);}
For example, we can redefine the name of the column of the original table to display, such as changing the regdate field to "Registration Date". The specific code will be explained below.
In the process of data presentation, sometimes not all columns need to be displayed. To hide or show them according to the actual situation, you can write the relevant method implementation as follows:
Public function ignoreFields (array $fields) {foreach ($fields as $f) {if ($favored details-> pk_col) $this- > hide_cols [] = $f;}}
Where $fields is an array of names of columns that need to be hidden. The primary key is also judged in the code because the primary key must be obtained from the database. If you do not want the primary key to be displayed in the user interface, you can set it in the following ways:
Public function hidePkCol ($bool) {$this- > hide_pk_col = (bool) $bool;}
The $bool passed in here is a Boolean value indicating whether the primary key needs to be displayed in the interface.
Next, take a look at another method, the code is as follows:
Private function _ selectFields () {foreach ($this- > tbl_fields as $field) {if (! in_array ($field,$this- > hide_cols)) {$this- > CI- > db- > select ($field); / / determine whether the primary key if ($field==$this- > pk_col & & $this- > hide_pk_col) continue; $headings [] = isset ($this- > headings [$field])? $this- > headings [$field]: ucfirst ($field) }} if (! empty ($headings)) {array_unshift ($headings, "); $this- > CI- > table- > set_heading ($headings);}}
Here is a helper class method for helper. Notice that the helper class in CI is named private, with an underscore + method name. This method is used in generate () below, where the main function is to cycle through whether each field in $this- > tbl_fields belongs to a hidden field, and if not, through $this- > CI- > db- > select ($field); take out the relevant field. Another thing to note is that
Array_unshift ($headings, "")
In this code, the actual function is to add a checkbox to the * * column of the data table, which represents the "Select all / reverse" function, which can be used when selecting or deleting data.
Next, the generate () method is used to generate the data table, with the following code:
Public function generate () {$this- > _ selectFields (); $rows = $this- > CI- > db-> from ($this- > tbl_name)-> get ()-> result_array (); foreach ($rows as & $row) {$id = $row [$this- > pk_col]; array_unshift ($row, "); if ($this- > hide_pk_col) {unset ($row [$this- > pk_col]);} return $this- > CI- > table- > generate ($rows);}
In this method, the above $this- > _ selectFields () is called first.
Method to determine which fields in the database specified table are displayed. Then use the method in CI to get the data table record to get the dataset ($rows). Then in the loop, a checkbox (array_unshift sentence) is generated before each record. * to determine whether to block the display of the primary key, and if so, to block the display (unset sentence).
Next, add a form submit button to the data table. For the sake of generality, we expect to be able to specify what type of button to generate according to the user's requirements. For example, in this example, you expect to generate a deleted button, so we write a method to generate the button as follows:
Public static function createButton ($action_name, $label) {return ";}
In this static method, $action_name is the name of the method to be generated. For example, if we want to generate the Delete method, the $action_name parameter passed in is delete, and label is the button's signature.
And what if you know that this button has been clicked by the user and submitted? It can be judged in the following ways
Public static function getPostAction () {if (isset ($_ POST ['dg_action'])) {return key ($_ POST [' dg_action']);}}
If the user selects multiple rows in the data table and submits them, you can use the following methods to get the
Public static function getPostItems () {if (! empty ($_ POST ['dg_item'])) {return $_ POST [' dg_item'];} return array ();}
An array representing how many records the user has selected is returned. This example involves the ability to delete the button, so write a method to delete the data selected by the user, as follows:
Public function deletePostSelection () {if (! empty ($_ POST ['dg_item'])) return $this- > CI- > db-> from ($this- > tbl_name)-> where_in ($this- > pk_col,$_POST [' dg_item'])-> delete ();}
For example, if the user selects several records in the table and clicks the delete button to submit, the deletePostSelection method will be equivalent to executing the following SQL statement:
DELETE FROM my_table WHERE id IN (1, 5, 7, 3, ETC...).
*. Let's sort out the complete data table to generate classes, as shown in the following code:
Class Datagrid {private $hide_pk_col = true; private $hide_cols = array (); private $tbl_name ='; private $pk_col =''; private $headings = array (); private $tbl_fields = array (); function _ construct ($tbl_name, $pk_col = 'id') {$this- > CI = & get_instance (); $this- > CI- > load- > database (); $this- > tbl_fields = $this- > CI- > db- > list_fields ($tbl_name) If (! in_array ($pk_col,$this- > tbl_fields)) {throw new Exception ("Primary key column'$pk_col' not found in table' $tbl_name'");} $this- > tbl_name = $tbl_name; $this- > pk_col = $pk_col; $this- > CI- > load- > library ('table');} public function setHeadings (array $headings) {$this- > headings = array_merge ($this- > headings, $headings) } public function hidePkCol ($bool) {$this- > hide_pk_col = (bool) $bool;} public function ignoreFields (array $fields) {foreach ($fields as $f) {if ($favored examples-> pk_col) $this- > hide_cols [] = $f;} private function _ selectFields () {foreach ($this- > tbl_fields as $field) {if (! in_array ($field,$this- > hide_cols)) {$this- > CI- > db- > select ($field) If ($field==$this- > pk_col & & $this- > hide_pk_col) continue; $headings [] = isset ($this- > headings [$field])? $this- > headings [$field]: ucfirst ($field);}} if (! empty ($headings)) {array_unshift ($headings, "); $this- > CI- > table- > set_heading ($headings);} public function generate () {$this- > _ selectFields () $rows = $this- > CI- > db-> from ($this- > tbl_name)-> get ()-> result_array (); foreach ($rows as & $row) {$id = $row [$this- > pk_col]; array_unshift ($row, "); if ($this- > hide_pk_col) {unset ($row [$this- > pk_col]);}} return $this- > CI- > table- > generate ($rows);} public static function createButton ($action_name, $label) {return" } public static function getPostAction () {if (isset ($_ POST ['dg_action'])) {return key ($_ POST [' dg_action']);}} public static function getPostItems () {if (! empty ($_ POST ['dg_item'])) {return $_ POST [' dg_item'];} return array () } public function deletePostSelection () {if (! empty ($_ POST ['dg_item'])) return $this- > CI- > db-> from ($this- > tbl_name)-> where_in ($this- > pk_col,$_POST [' dg_item'])-> delete ();}}
We save this class as datagrid_helper.php and save it in the application/helper directory.
On "how to use jQuery design data table design table base class" this article is shared here, I hope the above content can be of some help to you, so that you can learn more knowledge, if you think the article is good, please share it out for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.