Editable Grid with command buttons
To make a grid editable you need to enable the editable property and configure the ezEditTable configuration object (ezEditTable_config)
nested in the table filter configuration object. This demo shows how to :
- configure the cell editors and enable the command buttons
- configure the server-side actions for insert, update and delete operations
- plug a custom date picker component and use the editor's delegates
- use custom cell data validators
- use server activity callbacks to display a loader
- use the db actions callbacks to reload the page after insert and delete operatoions
- customise the script's messages
Please note that is a live demo and your tests will be recorded in a database!
| Id | Name | Start Date | Salary | Active | Actions | |
|---|---|---|---|---|---|---|
| 36797 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | ||
| 36796 | Employee name... | employee@email.com | 0000-00-00 | 0.00 | ||
| 36795 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | ||
| 36794 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | ||
| 36793 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | ||
| 36792 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | ||
| 36791 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | ||
| 36790 | Employee name... | employee@email.com | 0000-00-00 | 0.00 | ||
| 36789 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | ||
| 36788 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | ||
| 36787 | Employee name... | employee@email.com | 0000-00-00 | 0.00 | ||
| 36786 | Employee name... | employee@email.com | 0000-00-00 | 0.00 | ||
| 36785 | Employee name... | employee@email.com | 2011-01-10 | 0.00 | ||
| 36784 | Employee name... | employee@email.com | 0000-00-00 | 0.00 | ||
| 36783 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | ||
| 36782 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | ||
| 36781 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | ||
| 36780 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | ||
| 36779 | Employee name... | employee@email.com | 2011-01-10 | 0.00 | ||
| 36778 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | ||
| 36777 | Employee name... | employee@email.com | 0000-00-00 | 0.00 | ||
| 36776 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | ||
| 36775 | Employee name... | employee@email.com | 0000-00-00 | 0.00 | ||
| 36774 | Employee name... | employee@email.com | 0000-00-00 | 0.00 | ||
| 36773 | Employee name... | employee@email.com | 0000-00-00 | 0.00 | ||
| 36772 | Employee name... | employee@email.com | 0000-00-00 | 0.00 | ||
| 36771 | Employee name... | employee@email.com | 0000-00-00 | 0.00 | ||
| 36770 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | ||
| 36769 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | ||
| 36768 | Employee name... | employee@email.com | 0000-00-00 | 0.00 | ||
| 36767 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | ||
| 36766 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | ||
| 36765 | Employee name... | employee@email.com | 0000-00-00 | 0.00 | ||
| 36764 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | ||
| 36763 | Employee name... | employee@email.com | 0000-00-00 | 0.00 | ||
| 36762 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | ||
| 36761 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | ||
| 36760 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | ||
| 36759 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | ||
| 36758 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | ||
| 36757 | Employee name... | employee@email.com | 2011-01-10 | 0.00 | ||
| 36756 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | ||
| 36755 | Employee name... | employee@email.com | 2011-01-10 | 0.00 | ||
| 36754 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | ||
| 36753 | Employee name... | employee@email.com | 0000-00-00 | 0.00 | ||
| 36752 | Employee name... | employee@email.com | 2011-01-01 | 0.01 | ||
| 36751 | Employee name... | employee@email.com | 0000-00-00 | 0.00 | ||
| 36750 | Employee name... | employee@email.com | 0000-00-00 | 0.00 | ||
| 36749 | Employee name... | employee@email.com | 0000-00-00 | 0.00 | ||
| 36748 | Employee name... | employee@email.com | 0000-00-00 | 0.00 | ||
| 36747 | Employee name... | employee@email.com | 0000-00-00 | 0.00 | ||
| 36746 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | ||
| 36745 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | ||
| 36744 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | ||
| 36743 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | ||
| 36742 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | ||
| 36741 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | ||
| 36740 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | ||
| 36739 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | ||
| 36738 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | ||
| 36737 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | ||
| 36736 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | ||
| 36735 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | ||
| 36734 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | ||
| 36733 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | ||
| 36732 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | ||
| 36731 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | ||
| 36730 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | ||
| 36729 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | ||
| 36728 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | ||
| 36727 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | ||
| 36726 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | ||
| 36725 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | ||
| 36724 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | ||
| 36723 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | ||
| 36722 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | ||
| 36721 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | ||
| 36720 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | ||
| 36719 | Employee name... | employee@email.com | 2011-01-01 | -1.00 | ||
| 36718 | Employee name... | employee@email.com | 2011-01-01 | -1.00 | ||
| 36717 | Employee name... | employee@email.com | 2011-01-01 | -1.00 | ||
| 36716 | Employee name... | employee@email.com | 2011-01-01 | -1.00 | ||
| 36715 | Employee name... | employee@email.com | 2011-01-01 | -1.00 | ||
| 36714 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | ||
| 36713 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | ||
| 36712 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | ||
| 36711 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | ||
| 36710 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | ||
| 36709 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | ||
| 36708 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | ||
| 36707 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | ||
| 36706 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | ||
| 36705 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | ||
| 36704 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | ||
| 36703 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | ||
| 36702 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | ||
| 36701 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | ||
| 36700 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | ||
| 36699 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | ||
| 36698 | Employee name... | employee@email.com | 2011-01-01 | 0.00 |
//Validation functions
function et_ValidateEmail(email){
var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
return reg.test(email);
}
function et_IsNumber(val){
var isNumber = /^\d+\.\d{2}$/;
return isNumber.test(val);
}
var tfConfig = {
enable_default_theme: true,
btn_reset: true,
alternate_rows: true,
popup_filters: true,
col_0: 'none', col_5: 'none', col_6: 'none',
rows_counter: true,
paging: true,
col_width:['50px', '240px', '200px', '100px', '100px', '70px', '130px'],
col_date_type: [null,null,null,'ymd',null,null],
sort_config: { sort_types: ['number','string','string','ymd','number','none', 'none'] },
grid_layout: true, grid_width: '930px', grid_height: '300px', grid_enable_cols_resizer: false,
selectable: true,
editable: true,
ezEditTable_config: {
loadStylesheet: true,
auto_save: false,
form_submit_interval: 999,
cell_editors:[
{ type: 'none' },
{ type: 'input', attributes:[['title', 'First name and last name']] },
{ type: 'input', attributes:[['title', 'email address']] },
{ type: 'custom', target: 'dateEditor' },
{ type: 'input', attributes:[['maxLength', 10], ['title', '10 digits max.\n numbers with 2 decimal places only']], css:'alignRight' },
{ type: 'boolean' },
{ type: 'command',
buttons:{
enable: ['update', 'insert', 'delete', 'submit', 'cancel'],
'update': { title:'Edit row' },
'insert': { title:'Add row' },
'delete': { title:'Delete row' },
'submit': { text:'Save', title:'Save' },
'cancel': { text:'Close', title:'Close' }
}
}
],
actions:{
'update': {
uri: 'php/employeeUpdate.php', submit_method: 'form', form_method: 'POST',
param_names: ['id','name','email','startdate','salary','active']
},
'insert': {
uri: 'php/employeeInsert.php', submit_method: 'form', form_method: 'POST',
param_names: ['id','name','email','startdate','salary','active'],
default_record: ['', 'Employee name...', 'employee@email.com', '2011-01-01','0.00', '', ''],
on_after_submit: function(o, modRows){ setTimeout(function(){ location.reload(); }, 15); }
},
'delete': {
uri: 'php/script.deleteEmployee.php', submit_method: 'script', bulk_delete: false,
param_names: ['id'],
on_after_submit: function(o, modRows){ setTimeout(function(){ location.reload(); }, 15); }
}
},
//Data validation delegate, the script does not provide data validation tools
validate_modified_value: function(o, colIndex, oldVal, newVal, cell, editor){
if(colIndex==2 && oldVal != newVal){
if(!et_ValidateEmail(newVal)){
alert('Please insert a valid email!');
return false;
} else return true;
}
else if(colIndex == 4 && oldVal != newVal){
if(!et_IsNumber(newVal)){
alert('Please insert a valid number with 2 decimal places!');
return false;
} else return true;
}
else return true;
},
//Custom editor functions
open_custom_editor: function(o, cell, editor){
cell.appendChild(editor);
editor.style.display = '';
},
close_custom_editor: function(o, cell, editor){
editor.style.display = 'none';
document.body.appendChild(editor);
var d = o.Get('daySlc'), m = o.Get('monthSlc'), y = o.Get('yearSlc');
d.value = '';
m.value = '';
y.value = '';
},
get_custom_editor_value: function(o, editor, colIndex){
var strDate;
var d = o.Get('daySlc'), m = o.Get('monthSlc'), y = o.Get('yearSlc');
strDate = y.value+'-'+m.value+'-'+d.value;
return strDate;
},
set_custom_editor_value: function(o, editor, colIndex, val){
o.config.SetDateEditorValue(o, val, 'YMD');
},
//Server activity callback events
on_server_activity_start: function(o, row){ setTimeout(function(){ o.Get('divLoader').style.display = 'block';}, 1); },
on_server_activity_stop: function(o, row){ setTimeout(function(){ o.Get('divLoader').style.display = 'none';}, 1); },
//Messages
msg_submit_ok: 'Data processed successfully!',
msg_confirm_delete_selected_rows: 'Are you sure you want to delete the selected record?',
msg_error_occured: 'Error!',
msg_submit_unsuccessful: 'Data could not be saved correctly!',
//Custom date editor function, does not belong to ezEditTable script
SetDateEditorValue: function(o, val, dateType){
var date = tf_FormatDate(val, dateType);
var d = o.Get('daySlc'), m = o.Get('monthSlc'), y = o.Get('yearSlc');
var day = date.getDate(), month = date.getMonth()+1, year = date.getFullYear();
d.value = day.toString().length==1 ? '0'+day.toString() : day;
m.value = month.toString().length==1 ? '0'+month.toString() : month;
y.value = year;
}
}
};
Below the server-side PHP codes for DB operations used in this demo:
Insert:
<?php
if(isset($_POST["id"]) && isset($_POST["name"]) && isset($_POST["email"])){
$id = $_POST["id"];
$name = echappement(utf8_urldecode($_POST["name"]));
$email = echappement(utf8_urldecode($_POST["email"]));
$startdate = $_POST["startdate"];
$salary = $_POST["salary"];
$active = $_POST["active"];
$ip = isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR'];
if($active == 'true') $active = 1;
else $active = 0;
connect_db();
$query = "INSERT INTO ".TBL_DEMO_EMPLOYEE." ".
" VALUES('0','$name','$email','$startdate','$salary',".
"'$ip', null, null, NOW(), null, null, $active, 0)";
mysql_query($query);
close_db();
}
?>
Update:
<?php
if(isset($_POST["id"]) && isset($_POST["name"]) && isset($_POST["email"])){
$id = $_POST["id"];
$name = echappement(utf8_urldecode($_POST["name"]));
$email = echappement(utf8_urldecode($_POST["email"]));
$startdate = $_POST["startdate"];
$salary = $_POST["salary"];
$active = $_POST["active"];
$ip = isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR'];
if($active == 'true') $active = 1;
else $active = 0;
connect_db();
$query = "UPDATE ".TBL_DEMO_EMPLOYEE." SET ".
"NAME='$name',EMAIL='$email',STARTDATE='$startdate',SALARY='$salary'".
",MODIP='$ip',MODDATE=NOW(),ACTIVE=$active ".
"WHERE ID='$id'";
mysql_query($query);
close_db();
}
?>
Delete:
Records deleting is performed in this case via <script> tag inclusion method, ... submit_method: 'script', ...
(GET http request), therefore do not forget to specify the javascript content-type in your server-side code:
<?php
header("content-type: application/x-javascript");
if(isset($_GET["id"])){
$id = $_GET["id"];
$ip = isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR'];
connect_db();
$query = "UPDATE ".TBL_DEMO_EMPLOYEE." SET ".
"DELIP='$ip',DELDATE=NOW(),DELETED=1 ".
"WHERE ID='$id'";
mysql_query($query);
close_db();
}
?>