
# mask editor

You can apply masks in the editor for text and date fields. (Use editFormat for numeric fields)

It does not work in dropDown, multiLine, multiCheck, number editors, etc.

#### Mask editor basic format

For `"9"`, `spaces are allowed`.
If not entered, it is excluded from the value.
If there is a format that does not allow spaces, you must enter them in order to move to another cell.
The default mask pattern characters are as follows, but developers can create their own if necessary.

* `"9"`: new RegExp("[0-9 ]")
* `"0"`: new RegExp("[0-9]")
* `"a"`: new RegExp("[A-Za-z]")
* `"*"`: new RegExp("[A-Za-z0-9]")

#### Using mask editor with TextCellEditor

The format applied to the mask in TextCellEditor is `visible only in the editor`.
mask: If you enter `"0000-0000-0000-0000"`, the editor displays `"____-____-____-____"` according to the entered format.
Therefore, in the case of a text field, in order to display it in a cell, it must be processed appropriately using regular expressions or the displayCallback property.

#### Using mask editor with DateCellEditor

In the case of date fields, the formats of `editMask` and `datetimeFormat` must match, and even the delimiter must be passed to the cell using `includedFormat:true` for the date data to be processed properly.

```js
var fields = [{
  "fieldName": "OrderID",
  "dataType": "text"
},
{
  "fieldName": "CustomerID",
  "dataType": "text"
},
{
  "fieldName": "EmployeeID",
  "dataType": "text"
},
{
  "fieldName": "OrderDate",
  "dataType": "datetime",
  "datetimeFormat": "yyyy-MM-dd",
  "amText": "AM",
  "pmText": "PM"
},
{
  "fieldName": "CompanyName",
  "dataType": "text"
},
{
  "fieldName": "Country",
  "dataType": "text"
},
{
  "fieldName": "Phone",
  "dataType": "text"
},
{
  "fieldName": "ProductName",
  "dataType": "text"
},
{
  "fieldName": "QuantityPerUnit",
  "dataType": "text"
},
{
  "fieldName": "Quantity",
  "dataType": "number"
},
{
  "fieldName": "UnitPrice",
  "dataType": "number"
}];

var columns = [{
  "name": "Country",
  "fieldName": "Country",
  "width": "100",
  "header": {
      "text": "Country"
  }
}, {
  "name": "CompanyName",
  "fieldName": "CompanyName",
  "width": "120",
  "header": {
      "text": "Company Name"
  }
}, {
  "name": "OrderID",
  "fieldName": "OrderID",
  "width": "150",
  "header": {
    "text": "Card Number",
    "styleName": "orange-column"
  }, 
  "editor": {
    "mask": {
      "editMask": "0000-0000-0000-0000"
    }
  },
  "textFormat": "([0-9]{4})([0-9]{4})([0-9]{4})([0-9]{4});$1-$2-$3-$4"
}, {
  "name": "OrderDate",
  "fieldName": "OrderDate",
  "width": "130",
  "header": {
      "text": "Order Date",
      "styleName": "orange-column"
  },
  "editor": {
    "type":"date", 
    "mask": {
        "editMask":"9999-99-99",
        "placeHolder":"yyyy-MM-dd",
        "includedFormat":true
    }
  }, 
  "datetimeFormat":"yyyy-MM-dd"
}, {
  "name": "CustomerID",
  "fieldName": "CustomerID",
  "width": "90",
  "header": {
      "text": "Customer ID"
  }
}, {
  "name": "EmployeeID",
  "fieldName": "EmployeeID",
  "width": "90",
  "header": {
      "text": "Employee ID"
  },
}, {
  "name": "Phone",
  "fieldName": "Phone",
  "width": "90",
  "renderer": {
      "type": "button"
  },
  "header": {
      "text": "Phone"
  }
}, {
  "name": "ProductName",
  "fieldName": "ProductName",
  "width": "200",
  "header": {
      "text": "Product Name"
  }
}, {
  "name": "QuantityPerUnit",
  "fieldName": "QuantityPerUnit",
  "width": "100",
  "header": {
      "text": "Quantity / Unit"
  }
}, {
  "name": "Quantity",
  "fieldName": "Quantity",
  "width": "100",
  "header": {
      "text": "Quantity"
  },
  "footer": {
      "expression": "sum",
      "numberFormat": "#,##0"
  }
}, {
  "name": "UnitPrice",
  "fieldName": "UnitPrice",
  "width": "100",
  "header": {
      "text": "Unit Price"
  },
  "footer": {
      "expression": "avg",
      "numberFormat": "#,##0.00"
  }
}];

const { gridView, dataProvider } = RealGrid.createGrid("realgrid", {
  columns,
  header: {
    height: 40
  },
  display: {
    rowHeight: 36,
    emptyMessage: "No data to display."
  },
  footer: {
    height: 40
  },
  stateBar: {
    width: 16
  },
  onInitialized: (grid) => {
    fetch('/data/en/editor_demoData.json')
      .then(response => response.json())
      .then(data => {
        grid.getDataSource().fillJsonData(data, {count: 10});
        
        grid.setValue(0, "OrderID", "4017956479311942");
        grid.setValue(1, "OrderID", "3537454864902429");
        grid.setValue(2, "OrderID", "3574848246092110");
        grid.setValue(3, "OrderID", "3571523163079340");
        grid.setValue(4, "OrderID", "3746227853102431");
        grid.setValue(5, "OrderID", "5602245880215027");
        grid.setValue(6, "OrderID", "6709735293733863");
        grid.setValue(7, "OrderID", "3573722642979827");
        grid.setValue(8, "OrderID", "6759123799705660");
        grid.setValue(9, "OrderID", "2016571419050795");
        
        grid.refresh();
      });
  }
}, {
  fields
});

window.gridView = gridView;
window.dataProvider = dataProvider;
```

```js
var columns = [{
...
}, {
   "name": "OrderID",
   "fieldName": "OrderID",
   "width": "150",
   "header": {
     "text": "Card Number",
     "styleName": "orange-column"
   },
   "editor": {
     "mask": {
       "editMask": "0000-0000-0000-0000"
     }
   },
   "textFormat": "([0-9]{4})([0-9]{4})([0-9]{4})([0-9]{4});$1-$2- $3-$4"
}, {
   "name": "OrderDate",
   "fieldName": "OrderDate",
   "width": "130",
   "header": {
       "text": "Order Date",
       "styleName": "orange-column"
   },
   "editor": {
     "type": "date",
     "mask": {
         "editMask": "9999-99-99", //displayed format
         "placeHolder": "yyyy-MM-dd", //Format to be displayed in the editor
         "includedFormat": true //The contents displayed in the editor are passed as cell values.
     }
   },
   "datetimeFormat":"yyyy-MM-dd"
}, {
...
}];
```

---

Creating pattern characters using ### definitions

This is a demo of creating a time input machine using definitions.

```js
var fields = [{
  "fieldName": "OrderID",
  "dataType": "text"
},
{
  "fieldName": "CustomerID",
  "dataType": "text"
},
{
  "fieldName": "EmployeeID",
  "dataType": "text"
},
{
  "fieldName": "Time",
},
{
  "fieldName": "CompanyName",
  "dataType": "text"
},
{
  "fieldName": "Country",
  "dataType": "text"
},
{
  "fieldName": "Phone",
  "dataType": "text"
},
{
  "fieldName": "ProductName",
  "dataType": "text"
},
{
  "fieldName": "QuantityPerUnit",
  "dataType": "text"
},
{
  "fieldName": "Quantity",
  "dataType": "number"
},
{
  "fieldName": "UnitPrice",
  "dataType": "number"
}];

var columns = [{
  "name": "Country",
  "fieldName": "Country",
  "width": "100",
  "header": {
      "text": "Country"
  }
}, {
  "name": "CompanyName",
  "fieldName": "CompanyName",
  "width": "120",
  "header": {
      "text": "Company Name"
  }
}, {
  "name": "Time",
  "fieldName": "Time",
  "width": "150",
  "header": {
    "text": "Time",
    "styleName": "orange-column"
  }, 
  "editor": {
    "mask": {
      "definitions": {
          "b": "[0-2]",
          "c": "[0-9]",
          "d": "[0-5]",
          "e": "[0-9]"
      },
      "editMask": "bc:de",
      "includedFormat": true,                
      "overWrite": true, 
      "allowEmpty": true
    }
  },
  "textFormat": "([0-9]{2})([0-9]{2});$1:$2"
}, {
  "name": "EmployeeID",
  "fieldName": "EmployeeID",
  "width": "90",
  "header": {
      "text": "Employee ID"
  },
}, {
  "name": "Phone",
  "fieldName": "Phone",
  "width": "90",
  "renderer": {
      "type": "button"
  },
  "header": {
      "text": "Phone"
  }
}, {
  "name": "ProductName",
  "fieldName": "ProductName",
  "width": "200",
  "header": {
      "text": "Product Name"
  }
}, {
  "name": "QuantityPerUnit",
  "fieldName": "QuantityPerUnit",
  "width": "100",
  "header": {
      "text": "Quantity / Unit"
  }
}, {
  "name": "Quantity",
  "fieldName": "Quantity",
  "width": "100",
  "header": {
      "text": "Quantity"
  },
  "footer": {
      "expression": "sum",
      "numberFormat": "#,##0"
  }
}, {
  "name": "UnitPrice",
  "fieldName": "UnitPrice",
  "width": "100",
  "header": {
      "text": "Unit Price"
  },
  "footer": {
      "expression": "avg",
      "numberFormat": "#,##0.00"
  }
}];

const { gridView, dataProvider } = RealGrid.createGrid("realgrid", {
  columns,
  header: {
    height: 40
  },
  display: {
    rowHeight: 36,
    emptyMessage: "No data to display."
  },
  footer: {
    height: 40
  },
  stateBar: {
    width: 16
  },
  onInitialized: (grid) => {
    fetch('/data/en/editor_demoData.json')
      .then(response => response.json())
      .then(data => {
        grid.getDataSource().fillJsonData(data, {count: 10});
        
        grid.setValue(0, "Time", "00:32");
        grid.setValue(1, "Time", "11:11");
        grid.setValue(2, "Time", "12:42");
        grid.setValue(3, "Time", "06:12");
        grid.setValue(4, "Time", "02:14");
        grid.setValue(5, "Time", "00:42");
        grid.setValue(6, "Time", "10:12");
        grid.setValue(7, "Time", "18:16");
        grid.setValue(8, "Time", "03:17");
        grid.setValue(9, "Time", "23:48");
        
        grid.refresh();
      });
  }
}, {
  fields
});

gridView.onGetEditValue = function (grid, index, editResult) {
  if(index.fieldName === "Time"){
    var time = editResult.value.substring(0,2);
    if(Number(time) >= 24){
      editResult.value = "";
    }
  }
};

window.gridView = gridView;
window.dataProvider = dataProvider;
```

```js
var columns = [
   ...
   }, {
   "name": "Time",
   "fieldName": "Time",
   "width": "150",
   "header": {
     "text": "Time",
     "styleName": "orange-column"
   },
   "editor": {
     "mask": {
       "definitions": {
           "b": "[0-2]",
           "c": "[0-9]",
           "d": "[0-5]",
           "e": "[0-9]"
       },
       "editMask": "bc:de",
       "includedFormat": true,
       "overWrite": true,
       "allowEmpty": true
     }
   },
   "textFormat": "([0-9]{2})([0-9]{2});$1:$2"
}, {
   ...
];
```

When using the time editor, it is not possible to directly restrict the input so that it does not exceed 24 hours. You must process it so that the value is not applied after verification as shown below.

```js
gridView.onGetEditValue = function (grid, index, editResult) {
     if(index.fieldName == "Time"){
         var time = editResult.value.substr(0,2);
         if(Number(time) >= 24){
             editResult.value = ""
         }
     }
}
```