Skip to main content

Field

  • 2 minutes to read

Get all fields in the main sub-document

var mainSubDocumentFields = richEdit.document.fields;

Create a field at the cursor position

var subDocument = richEdit.selection.activeSubDocument;
var field = subDocument.fields.create(richEdit.selection.active, 'time');
field.update(function(self) {
    console.log('Result: ' + subDocument.getText(self.resultInterval));
});

Delete a field but keep the field result

var subDocument = richEdit.selection.activeSubDocument;
richEdit.beginUpdate();
richEdit.history.beginTransaction();
// creates and updates the 'time' field, and then calls the callback function
subDocument.fields.create(richEdit.selection.active, 'time').update(function(field) {
    var pos = field.interval.start;
    // gets the field result value
    var text = subDocument.getText(field.resultInterval);
    // deletes the field
    subDocument.deleteText(field.interval);
    // inserts the field result value
    subDocument.insertText(pos, text);
    richEdit.history.endTransaction();
    richEdit.endUpdate();
});

Remove a field

richEdit.document.fields.getByIndex(0).delete();

Update all fields

var doInAllSubDocuments = true;
var updateTocFields = true;
var options = new DevExpress.RichEdit.UpdateFieldsOptions(doInAllSubDocuments, updateTocFields);
richEdit.document.fields.updateAllFields(function() {
    console.log('Updated');
}, options);

Update all fields once the editor is shown

@(Html.DevExpress().RichEdit("richEdit")
    .OnDocumentLoaded("function(s, e) { s.document.fields.updateAllFields(); }")
    // ...

Update fields by name

rich.beginUpdate();
rich.history.beginTransaction();
for(var i = 0, field; field = rich.document.fields.getByIndex(i); i++)
    if (field.name == DevExpress.RichEdit.FieldName.FillIn)
        field.update();
rich.history.endTransaction();
rich.endUpdate();

Update all fields and highlight the result

richEdit.history.beginTransaction();
richEdit.beginUpdate();
richEdit.document.fields.updateAllFields(function () {  
    richEdit.document.subDocuments.forEach(function (subDocument) {
        for (var i = 0, field; field = subDocument.fields.getByIndex(i); i++)
            richEdit.document.setCharacterProperties(field.resultInterval, { bold: true });
    });
    richEdit.endUpdate();
    richEdit.history.endTransaction();  
}, new DevExpress.RichEdit.UpdateFieldsOptions(true, false));