Editable Grid
To make a grid editable you need to enable the editable
property and configure the ezEditTable configuration object (ezEditTable_config).
This demo shows how to :
- configure the cell editors
- configure the server-side actions for insert, update and delete operations
- plug a third-party date picker component and use the editor's delegates
- use custom cell data validators
Please note that is a live demo and your tests will be recorded in a database!
Id | Name | Start Date | Salary | Active | |
---|---|---|---|---|---|
35969 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | |
35968 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | |
35967 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | |
35966 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | |
35965 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | |
35964 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | |
35963 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | |
35962 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | |
35961 | asdfasdfasdfasdfadfs | sdfasdfasdfasdfas@email.com | 1993-04-04 | 44444.00 | |
35960 | ss | ssss@email.com | 2020-01-01 | 44.00 | |
35954 | sdf | employee@email.com | 2011-01-01 | 0.00 | |
35953 | Employee name...kiki | employee@email.com | 2011-01-01 | 0.00 | |
35952 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | |
35951 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | |
35950 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | |
35949 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | |
35948 | Employee name... | employee@email.com | 2011-02-01 | 0.00 | |
35947 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | |
35946 | Pis'ka | employee@email.com | 2011-01-01 | 0.00 | |
35945 | sfd | employee@email.com | 2011-01-01 | 0.00 | |
35944 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | |
35943 | Pis'ka | employee@email.com | 2011-01-01 | 0.00 | |
35942 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | |
35941 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | |
35940 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | |
35939 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | |
35938 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | |
35937 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | |
35936 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | |
35935 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | |
35934 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | |
35933 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | |
35932 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | |
35931 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | |
35930 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | |
35929 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | |
35928 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | |
35927 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | |
35926 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | |
35925 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | |
35922 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | |
35920 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | |
35919 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | |
35918 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | |
35917 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | |
35916 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | |
35915 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | |
35914 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | |
35913 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | |
35912 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | |
35911 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | |
35910 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | |
35909 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | |
35908 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | |
35907 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | |
35906 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | |
35903 | test | employee@email.com | 2011-01-01 | 0.00 | |
35901 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | |
35900 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | |
35899 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | |
35898 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | |
35895 | D | employee@email.com | 2011-01-01 | 0.00 | |
35894 | Employee name... | jhkjhkjhemployee@email.com | 2011-01-01 | 0.00 | |
35893 | Employee name... | klklklemployee@email.com | 2011-01-01 | 0.00 | |
35892 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | |
35890 | ikke | employee@email.com | 2011-01-01 | 1.22 | |
35889 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | |
35888 | a | employee@email.com | 2011-01-01 | 0.00 | |
35887 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | |
35886 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | |
35885 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | |
35884 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | |
35883 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | |
35882 | dfdfhfgh | employee@email.com | 2011-01-01 | 0.00 | |
35879 | dokter afadf dfa | dokter_dark@email.com | 2011-01-01 | 1000.00 | |
35878 | 1212121 | employee@email.com | 2011-01-01 | 0.00 | |
35877 | é&21 | employee@email.com | 2011-01-01 | 0.00 | |
35875 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | |
35874 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | |
35873 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | |
35872 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | |
35871 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | |
35870 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | |
35869 | employee@email.com | 2012-01-01 | 12.36 | ||
35865 | titi | employee@email.fr | 2011-01-01 | 10.00 | |
35864 | employee@email.com | 2011-01-01 | 0.00 | ||
35860 | employee@email.com | 2011-01-01 | 2.33 | ||
35859 | employee@email.com | 2011-01-01 | 25.55 | ||
35851 | free@email.com | 2011-01-01 | 0.00 | ||
35848 | asd | employee@email.com | 2011-01-01 | 0.00 | |
35846 | 1 | employee@email.com | 2011-01-01 | 0.00 | |
35845 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | |
35844 | 1 | employee@email.com | 2011-01-01 | 0.00 | |
35843 | fdgdfg | employee@email.com | 2011-01-01 | 0.00 | |
35842 | fdg | employee@email.com | 2011-01-01 | 0.00 | |
35841 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | |
35840 | sdf | employee@email.com | 2011-01-01 | 0.00 | |
35839 | fdgfdgdfg | employee@email.com | 2011-01-01 | 0.00 | |
35838 | Employee name... | employee@email.com | 2011-01-01 | 0.00 | |
35837 | asdfasdf | 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 = { btn_reset: true, alternate_rows: true, col_0: 'none', col_5: 'none', rows_counter: true, col_width:['50px', '280px', '220px', '100px', '100px', '70px'], col_date_type: [null,null,null,'ymd',null,null], sort_config: { sort_types: ['number','string','string','ymd','number','none'] }, grid_layout: true, grid_width: '860px', grid_height: '300px', selectable: true, editable: true, ezEditTable_config: { loadStylesheet: true, default_selection: 'both', editor_model: 'cell', 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' } ], 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', ''] }, 'delete': { uri: 'php/script.deleteEmployee.php', submit_method: 'script', bulk_delete: false, param_names: ['id'] } }, //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'); }, //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(); } ?>