Skip to main content

DevExpress v24.2 Update — Your Feedback Matters

Our What's New in v24.2 webpage includes product-specific surveys. Your response to our survey questions will help us measure product satisfaction for features released in this major update and help us refine our plans for our next major release.

Take the survey Not interested

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

#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));