Code Monkey home page Code Monkey logo

simpletablecelleditor's Introduction

SimpleTableCellEditor

Simple Jquery based table td editor

SimpleTableCellEditor requires JQuery

Allow table content to be edited clientside, with a click inside editable cell or by keyboard navigation. A 'cell:edited' event is triggered if the cell content has been edited and the content changed.

Quick example : https://codepen.io/HugRob/pen/WNbKNZP

Usage example

    <table id="myTableId">
	    <tr>
	      <td class="editMe">Editable text</td>
	      <td>Uneditable text</td>
	      <td class="feedMeNumbers">Numbers only</td>
	    </tr>
    </table>
    <script>
      editor = new SimpleTableCellEditor("myTableId");
      editor.SetEditableClass("editMe");
      editor.SetEditableClass("feedMeNumbers", { validation: $.isNumeric }); //If validation return false, value is not updated
   
      $('#myTableId').on("cell:edited", function (event) {              
        console.log(`Cell edited : ${event.oldValue} => ${event.newValue}`);
      });               
    </script>

Navigation

Use tab or [shift + arrow] to navigate around editable cells.

Thanks jaysin586 for you work !

Events

  • "cell:edited" : Cell has been edited with new value
    • evt.element (JQuery node object)
    • evt.oldValue
    • evt.newValue
  • "cell:onEditEnter" : Before edition mode is entered
    • evt.element (JQuery node object)
  • "cell:onEditEntered" : After edition mode has been entered
    • evt.element (JQuery node object)
    • evt.oldValue
  • "cell:onEditExit" : Before edition mode is exited
    • evt.element (JQuery node object)
    • evt.oldValue
  • "cell:onEditExited" : After edition mode has been exited
    • evt.element (JQuery node object)
    • evt.oldValue
    • evt.newValue
    • evt.applied (boolean, will the edition be applied)

Methods

  • SimpleTableCellEditor : constructor(tableId, tableCellEditorParams)
  • SimpleTableCellEditor : SetEditable(element, cellEditorParams)
  • SimpleTableCellEditor : SetEditableClass(className, cellEditorParams)
  • SimpleTableCellEditor : Toggle(toggled)

Parameters

  • tableCellEditorParams
    • inEditClass : class used to flag td in edit mode
    • navigation : true|false enables/disables navigation with arrows and tab
  • cellEditorParams
    • validation : method used to validate new value
    • formatter : method to format new value
    • keys : keys handling validation and cancellation. Default value : { validation : [13], cancellation : [37] }

Full parameters exemple :

    <table id="myTableId">
	    <tr>
	      <td class="editMe">editable numbers</td>
	    </tr>
    </table> 
    <script>
    
      editor = new SimpleTableCellEditor("myTableId", { inEditClass: "busy" } );
      
      editor.SetEditableClass("editMe", {  //tds with .editMe class will be editable
            validation: $.isNumeric,  //value entered must be numeric
            formatter: (val) => { return val * 10; },  //value entered will be multiplied by 10
            keys: {
                validation: [13, 107, 35], //these keys will trigger validation (evt.which)
                cancellation: [27, 109] //these keys will trigger cancellation (evt.which)
            }
	    
        });            
    </script>

DataTable Support

  • DataTable table redrawn is supported, active cell editor will reappear after a table reload

Advanced options

cellParams.behaviour

Default value for cellParams.behaviour :

    behaviour: {
                tabKeyCauseCursorMove: true, //Allow user to move through editable fields using tab key. Circular rotation
                arrowKeyCauseCursorMove: true, //Allow user to move through editable fields using arrow key. Circular rotation
                outsideTableClickCauseCancellation: false, //if user end edition by clicking outside the table, cancel edition or save the value ?
                anotherCellClickCauseCancellation: false //if user end edition by clicking another cell, cancel edition or save the value ?
            }
    

By default, outsideTableClick and anotherCellClick are set to "false", the values are saved

cellParams.internals

cellParams.internals can be overridden.
Default value for cellParams.internals :

      {
    		renderValue: (elem, formattedNewVal) => { $(elem).text(formattedNewVal); },
    		renderEditor: (elem, oldVal) => {
    			$(elem).html(`<input type='text' style="width:100%; max-width:none">`);
    			var input = $(elem).find('input');
    			input.focus();
    			input.val(oldVal);
    		},
    		extractEditorValue: (elem) => { return $(elem).find('input').val(); },
    		extractValue: (elem) => { return $(elem).text(); }
    };

simpletablecelleditor's People

Contributors

unwild avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.