Writing custom cell editors

Source: Internet
Author: User
Writing custom cell editors
  • Page history
Basic Interface
function IEditor(args) {    // initialize the UI    /*********** REQUIRED METHODS ***********/    this.destroy = function() {        // remove all data, events & dom elements created in the constructor    };    this.focus = function() {        // set the focus on the main input control (if any)    };    this.isValueChanged = function() {        // return true if the value(s) being edited by the user has/have been changed        return false;    };    this.serializeValue = function() {        // return the value(s) being edited by the user in a serialized form        // can be an arbitrary object        // the only restriction is that it must be a simple object that can be passed around even        // when the editor itself has been destroyed        return "";    };    this.loadValue = function(item) {        // load the value(s) from the data item and update the UI        // this method will be called immediately after the editor is initialized        // it may also be called by the grid if if the row/cell being edited is updated via grid.updateRow/updateCell    };    this.applyValue = function(item,state) {        // deserialize the value(s) saved to "state" and apply them to the data item        // this method may get called after the editor itself has been destroyed        // treat it as an equivalent of a Java/C# "static" method - no instance variables should be accessed    };    this.validate = function() {        // validate user input and return the result along with the validation message, if any        // if the input is valid, return {valid:true,msg:null}        return { valid: false, msg: "This field is required" };            };    /*********** OPTIONAL METHODS***********/    this.hide = function() {        // if implemented, this will be called if the cell being edited is scrolled out of the view        // implement this is your UI is not appended to the cell itself or if you open any secondary        // selector controls (like a calendar for a datepicker input)    };    this.show = function() {        // pretty much the opposite of hide    };    this.position = function(cellBox) {        // if implemented, this will be called by the grid if any of the cell containers are scrolled        // and the absolute position of the edited cell is changed        // if your UI is constructed as a child of document BODY, implement this to update the        // position of the elements as the position of the cell changes        //         // the cellBox: { top, left, bottom, right, width, height, visible }    };}
Compound editors

In most cases, there will be a one-to-one relationship between the fields of your data objects and cells in the grid.
Occasionally, though, you may want to combine several fields into one cell in order to improve the usability of your application. A custom formatter can easily pull information from multiple fields of your data object and present it in one cell. slickgrid handles edition those cells by usingloadValue(),serializeValue()AndapplyValue()Methods of your editor. While it may seem more complicated than a simplegetValue()AndsetValue(), Without them, compound editors wocould not have been possible.

A sample compound editor is implemented in example 3A (source, demo ).

Intercepting cell edits

You can easily useonCellChangeEvent to run some code after the cell has been edited. You can also intercept all cell edits and have complete control over how and when those edits are committed by specifying a Custom Handler by settingeditCommandHandlerGrid option. Due to the disconnected natureapplyValue()AndserializeValue(), You can even undo your changes after you commit them. this can be especially handy if you are editing a remote data source-you can apply the changes and make an Ajax call passing the Undo callback as the error handler, so that your data doesn't get out of sync if the server cannot save the values. you can also queue up the edit commands and implement Undo/Redo functionality in just a few lines of code.

TheeditCommandHandlerIs called right after the editor'sdestroy()Method has been called, and is passed the item being edited, the column definition, and the edit command:

function editCommandHandler(item,column,editCommand) {}

TheeditCommandConsists:

  • row: The row of the cell being edited
  • cell: The column of the cell being edited
  • editor: A reference to the cell Editor
  • serializedValue: Serialized value; the result of calling editor. serializevalue () Right before destroying the editor
  • prevSerializedValue: The result of calling editor. serializevalue () before the changes have been made by the user
  • execute(): A callback to apply the changes using editor. applyvalue (item, serializedvalue) and update the row
  • undo(): A callback to undo the changes using editor. applyvalue (item, prevserializedvalue) and update the row

    A Sample spreadsheet with Undo is implemented in the example 3B (source, demo ).

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.