Implement code based on JQ editable table content

Source: Internet
Author: User
Tags current time json jquery library

This example applies to the scenario: when viewing the details, such as user details, found that some of the field information needs to be modified, you can directly click on the field content to modify, save the user time, (traditional approach is to enter an edit page, list all the edited field information, even if you only need to edit one or two of the field content, Then click Submit to improve the Web response speed, thus improving the front-end user experience.

This example relies on the jquery library and is based on the Jeditable plug-in, with the following features:

Real-time editing, background real-time response, and immediate completion of local refresh.
You can customize the input form type, currently jeditable provides Text,select,textarea type.
Responds to the keyboard's return and ESC keys.
Plug-in mechanism, this example provides consolidation of the DatePicker Calendar control with the jquery UI.
Here we go step-by-step to explain the implementation process.

The code is as follows Copy Code
<table width= "100%" border= "0" cellspacing= "0" cellpadding= "0" >


<thead>


<tr class= "Table_title" >


<TD colspan= "4" ><span class= "open" ></span> customer information </td>


</tr>


</thead>


<tbody>


<tr>


<TD width= "20%" class= "Table_label" > Name </td>


<TD width= "30%" class= "edit" id= "username" > Li Xiaosan </td>


<TD width= "20%" class= "Table_label" > Office Tel </td>


<TD width= "30%" class= "edit" id= "Phone" >021-12345678</td>


</tr>


<tr>


<TD class= "Table_label" > Appellation </td>


<TD class= "edit" id= "solutation" > Mr. </td>


<TD class= "Table_label" > Mobile </td>


<TD class= "edit" id= "mobile" >13800000000</td>


</tr>


<tr>


<TD class= "Table_label" > Company name </td>


<TD class= "edit" id= "Company" > Chang Fung Group </td>


<TD class= "Table_label" > E-mail </td>


<TD class= "edit" id= "email" >web@shuro.cn</td>


</tr>


<tr>


<TD class= "Table_label" > Potential customer Source </td>


<TD class= "Edit_select" id= "source" > PR </td>


<TD class= "Table_label" > Limited period </td>


<TD class= "DatePicker" id= "Sdate" >2011-11-30</td>


</tr>


<tr>


<TD class= "Table_label" > Position </td>


<TD class= "edit" id= "job" > Department manager </td>


<TD class= "Table_label" > website </td>


<TD class= "edit" id= "Web" >www.shuro.cn</td>


</tr>


<tr>


<TD class= "Table_label" > Creation time </td>


<td>2011-12-04 21:11:59</td>


<TD class= "Table_label" > Modification Time </td>


<TD id= "Modifiedtime" >2011-12-05 09:42:52</td>


</tr>


<tr>


<TD class= "Table_label" > Notes </td>


<TD class= "textarea" id= "note" colspan= "3" > Memo info </td>


</tr>


</tbody>


</table>


This is a table of user information, from the code can find the response of the field information TD has given a class and id attribute, and assigned a value. It is worth mentioning that the TD in the table corresponding to the value of the ID and the database in the field name one by one corresponds to do so in order to edit the background to get the corresponding field information, the following PHP code will be mentioned.

The code is as follows Copy Code

table{width:96%; margin:20px auto; border-collapse:collapse;}
Table td{line-height:26px; padding:2px; padding-left:8px; border:1px solid #b6d6e6;
. table_title{height:26px line-height:26px Background:url (btn_bg.gif) Repeat-x bottom; Text-indent:.3em; outline:0;}
. Table_label{background: #e8f5fe; text-align:right;}

CSS renders the table style, making the table look more comfortable.

Jquery
When it comes to jquery, be sure to refer to the jquery and jeditable plug-ins between the

The code is as follows Copy Code

<script type= "Text/javascript" src= ". /script/jquery.js "></script>
<script type= "Text/javascript" src= "/script/jquery.jeditable.js" ></script>

Then start calling the plug-in.

The code is as follows Copy Code

$ (function ( {
     $ ('. Edit '). Editable (' save.php ', { 
         : width    ,
         height    :
        //onblur   : ' Ignore ',
& nbsp;        cancel   : ' Cancel ',
          submit   : ' OK ',
         Indicator: ' ',
         tooltip   : ' Click to edit ... '
    });
});

The Jeditable plug-in provides a number of calls to properties and methods. You can set the width, height, text information of the button, the loading picture when submitting, the hint information on the mouse slide and so on. Save.php is the address of the backend program after the edited information is finally submitted. Now see if the information in the table can be edited oh.

Jeditable also provides select,textarea type of editing and provides plug-in API interfaces.

To see the processing of the dropdown selection box select:

The code is as follows Copy Code

$ ('. Edit_select '). Editable (' save.php '), {
Loadurl: ' json.php ',
Type: "Select",
});

Type Specifies the select type, and the data that is contained in the Select is from json.php,json.php provides the data source required for the Drop-down box.

The code is as follows Copy Code

$array [' old customers '] = ' old customers ';
$array [' develop alone '] = ' develop alone ';
$array [' partner '] = ' partner ';
$array [' public relations '] = ' public Relations ';
$array [' exhibition '] = ' exhibition ';
Print Json_encode ($array);

The data is directly in the json.php file, and you can, of course, read the database information and generate the JSON data.

$ ('. Edit_select '). Editable (' save.php '), {
Data: "{' Old customers ': ' Old customers ', ' Develop alone ': ' Develop alone ', ' partner ': ' partner ', ' exhibition ': ' Exhibition '}",
Type: "Select",
});
It's not hard to see that the data in the above code is a string of JSON data.

TextArea type is no longer a majority, it is OK to change type to textarea. PS: The default type is text.

When working with date types, I have access to a DatePicker calendar plugin for the jquery UI, and of course don't forget to introduce Juqery UI Plug-ins and styles:

The code is as follows Copy Code

<link rel= "stylesheet" type= "Text/css" href= "/style/jquery-ui.css"./>
<script type= "Text/javascript" src= "/script/jquery-ui.js" ></script>

DatePicker Calendar plugin with jquery UI

  code is as follows copy code

$.editable.addinputtype (' DatePicker ', {
    element: function (settings, original) {
        var input = $ (' <input class= ' input '/& gt; ');
        input.attr ("ReadOnly", "ReadOnly");
        $ (this). append (input);
        return (input);
   },
    plugin:function (Settings, original) {
    & nbsp;   var form = this;
        $ ("input", this). DatePicker ();
   }
});

The code invoked directly specifies that the type is datepicker.

The code is as follows Copy Code

$ (". DatePicker"). Editable (' save.php '), {
WIDTH:120,
Type: ' DatePicker ',
onblur: "Ignore",
});

Now see if the date in the table with the "Deadline" field can be modified. Well, there are other additional plugins to look forward to your joining.

Php
The edited field information is sent to the background program save.php program processing. Save.php needs to do is to receive the field information data submitted by the front-end, and to filter and verify the necessary, and then update the corresponding field contents in the datasheet, and return the results.

  code is as follows copy code

include_once ("connect.php");//Connection Database
$field =$_post[' id ']; // Gets the field name submitted by the front end
$val =$_post[' value '];//get the corresponding content of the field submitted by the front-end
$val = Htmlspecialchars ($val, ent_quotes);//filter Processing
&nbs P
$time =date ("y-m-d h:i:s");//Get system Current time
if (empty ($val)) {
    echo cannot be empty;
}else{
   //update field information
    $query =mysql_query ("Update customer Set $field = ' $ Val ', modifiedtime= ' $time ' where id=1 ');
    if ($query) {
       echo $val;
   }else{
       echo "Data error";    
   }
}

The complete example is as follows

The code is as follows Copy Code

&lt;link rel= "stylesheet" type= "Text/css" href= "/style/jquery-ui.css"./&gt;


&lt;style type= "Text/css" &gt;


table{width:96%; margin:20px auto; border-collapse:collapse;}


Table td{line-height:26px; padding:2px; padding-left:8px; border:1px solid #b6d6e6;


. table_title{height:26px line-height:26px; Background:url ("./images/btn_bg.gif") Repeat-x bottom; font-weight:bold ; Text-indent:.3em; outline:0;}


. Table_label{background: #e8f5fe; text-align:right;}


. btn{height:24px Background:url ("./images/btn_bg.gif") repeat-x; padding:0 4px; font-size:12px; border:1px solid #ccc; Cursor:pointer; MARGIN-LEFT:3PX}


. input{border:1px solid #369; background: #ffc; padding:2px}


. msg{padding-left:30px margin-top:20px}


TEXTAREA{FONT-SIZE:12PX}


&lt;/style&gt;


&lt;script type= "Text/javascript" src= "Http://www.shuro.cn/demo/Script/jquery.js" &gt;&lt;/script&gt;


&lt;script type= "Text/javascript" src= "/script/jquery.jeditable.min.js" &gt;&lt;/script&gt;


&lt;script type= "Text/javascript" src= "/script/jquery-ui.min.js" &gt;&lt;/script&gt;


&lt;script type= "Text/javascript" &gt;


$ (function () {


$ ('. Edit '). Editable (' save.php '), {


WIDTH:120,


Height:18,


onblur: "Ignore",


Cancel: ' Cancellation ',


Submit: ' OK ',


Indicator: ' &lt;img src= './images/loader.gif "&gt;",


ToolTip: ' Click to edit ... ',


Callback:function (value, settings) {


$ ("#modifiedtime"). HTML ("just");


}

});


$ ('. Edit_select '). Editable (' save.php '), {


Loadurl: ' json.php ',


Type: "Select",


Cancel: ' Cancellation ',


Submit: ' OK ',


Indicator: ' &lt;img src= './images/loader.gif "&gt;",


ToolTip: ' Click to edit ... ',


Style: ' Display:inline '


});


$ (". DatePicker"). Editable (' save.php '), {


WIDTH:120,


Type: ' DatePicker ',


onblur: "Ignore",


Cancel: ' Cancellation ',


Submit: ' OK ',


Indicator: ' &lt;img src= './images/loader.gif "&gt;",


ToolTip: ' Click to edit ... ',


Style: ' Display:inline '


});


$ (". TextArea"). Editable (' save.php '), {


Type: ' TextArea ',


Rows:6,


COLS:50,


onblur: "Ignore",


Cancel: ' Cancellation ',


Submit: ' OK ',


Indicator: ' &lt;img src= './images/loader.gif "&gt;"


});


});


DatePicker Calendar Plug-ins that invoke the jquery UI


$.editable.addinputtype (' DatePicker ', {


Element:function (Settings, original) {


var input = $ (' &lt;input class= "input"/&gt; ");


Input.attr ("ReadOnly", "ReadOnly");


$ (this). append (input);


return (input);


},


Plugin:function (Settings, original) {


var form = this;


$ ("input", this). DatePicker ();


}


});


&lt;/script&gt;


&lt;/head&gt;


&lt;body&gt;

&lt;div id= "Main" &gt;


&lt;div class= "msg" &gt;&lt;strong&gt; tips &lt;/strong&gt: Click on the field in the table to edit the contents of the online. &lt;/div&gt;


&lt;table width= "100%" border= "0" cellspacing= "0" cellpadding= "0" &gt;


&lt;thead&gt;


&lt;tr class= "Table_title" &gt;


&LT;TD colspan= "4" &gt;&lt;span class= "open" &gt;&lt;/span&gt; customer information &lt;/td&gt;


&lt;/tr&gt;


&lt;/thead&gt;


&lt;tbody&gt;


&lt;tr&gt;


&LT;TD width= "20%" class= "Table_label" &gt; Name &lt;/td&gt;


&LT;TD width= "30%" class= "edit" id= "username" &gt; Lee &lt;/td&gt;


&LT;TD width= "20%" class= "Table_label" &gt; Office Tel &lt;/td&gt;


&LT;TD width= "30%" class= "edit" id= "Phone" &gt;021-22072698&lt;/td&gt;


&lt;/tr&gt;


&lt;tr&gt;


&LT;TD class= "Table_label" &gt; Appellation &lt;/td&gt;


&LT;TD class= "edit" id= "Solutation" Mr. &gt;2


&LT;TD class= "Table_label" &gt; Mobile &lt;/td&gt;


&LT;TD class= "edit" id= "mobile" &gt;123&lt;/td&gt;


&lt;/tr&gt;


&lt;tr&gt;


&LT;TD class= "Table_label" &gt; Company name &lt;/td&gt;


&LT;TD class= "edit" id= "Company" &gt; cannot be &lt;/td&gt;


&LT;TD class= "Table_label" &gt; E-mail &lt;/td&gt;


&LT;TD class= "edit" id= "email" &gt;hilder.liang@gmail.com&lt;/td&gt;


&lt;/tr&gt;


&lt;tr&gt;


&LT;TD class= "Table_label" &gt; Potential customer Source &lt;/td&gt;


&LT;TD class= "Edit_select" id= "source" &gt; Developed alone &lt;/td&gt;


&LT;TD class= "Table_label" &gt; Limited period &lt;/td&gt;


&LT;TD class= "DatePicker" id= "Sdate" &gt;2011-11-30&lt;/td&gt;


&lt;/tr&gt;


&lt;tr&gt;


&LT;TD class= "Table_label" &gt; Position &lt;/td&gt;


&LT;TD class= "edit" id= "job" &gt; Department manager &lt;/td&gt;


&LT;TD class= "Table_label" &gt; website &lt;/td&gt;


&LT;TD class= "edit" id= "Web" &gt;www.shuro.cn&lt;/td&gt;


&lt;/tr&gt;


&lt;tr&gt;


&LT;TD class= "Table_label" &gt; Creation time &lt;/td&gt;


&lt;td&gt;2010-12-04 21:11:59&lt;/td&gt;


&LT;TD class= "Table_label" &gt; Modification Time &lt;/td&gt;


&LT;TD id= "Modifiedtime" &gt;2012-02-13 20:13:36&lt;/td&gt;


&lt;/tr&gt;


&lt;tr&gt;


&LT;TD class= "Table_label" &gt; Notes &lt;/td&gt;


&LT;TD class= "textarea" id= "note" colspan= "3" &gt; Memo information gfdfgsdgteftge44&lt;/td&gt;


&lt;/tr&gt;


&lt;/tbody&gt;


&lt;/table&gt;


&lt;br/&gt;


&lt;br/&gt;


&lt;br/&gt;


&lt;br/&gt;


&lt;br/&gt;


&lt;/div&gt;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.