All docs
V21.2
21.2
21.1
20.2
20.1
The page you are viewing does not exist in version 20.1. This link will take you to the root page.
19.2
The page you are viewing does not exist in version 19.2. This link will take you to the root page.
19.1
The page you are viewing does not exist in version 19.1. This link will take you to the root page.
18.2
The page you are viewing does not exist in version 18.2. This link will take you to the root page.

How to: Track changes to MERGEFIELD fields

  • 2 minutes to read

In this example, the ContentInserted and ContentRemoved events are handled to check if the number, order, or text of the document’s merge fields are changed. If the data is changed, the custom fieldsChanged function is called. The currentDocumentFields variable contains information about the document’s merge fields.

options.mailMerge.dataSource = [
    { firstName: "Alex", birthYear: 1991 },
    { firstName: "Joe", birthYear: 1990 },
    { firstName: "Bob", birthYear: 1995 },
];

var currentDocumentFields = {};
var checkSubDocuments = {};
var checkProcessId = null;

function fieldsChanged() {
    console.log('Mail merge fields were changed. See the currentDocumentFields variable:' + 
    "\n" + JSON.stringify(currentDocumentFields, undefined, 4));
}

function stopProcess() {
    if (checkProcessId !== null) {
        clearTimeout(checkProcessId);
        checkProcessId = null;
    }
    checkSubDocuments = {};
}

function getSubDocumentFieldsInfo(subDocument) {
    var info = [];
    for (var field, ind = 0; field = subDocument.fields.getByIndex(ind); ind++) {
        if (field.name === DevExpress.RichEdit.FieldName.MergeField) {
            info.push({
                // save here other useful data
                text: subDocument.getText(field.codeInterval),
            });
        }
    }
    return info;
}

function getSubDocumentInfo(subDocument) {
    var sdFieldsInfo = currentDocumentFields[subDocument.id];
    if (!sdFieldsInfo)
        currentDocumentFields[subDocument.id] = sdFieldsInfo = getSubDocumentFieldsInfo(subDocument);
    return sdFieldsInfo;
}

function updateProcess(richEdit) {
    function equals(oldInfo, newInfo) {
        var len = oldInfo.length;
        if(len != newInfo.length)
            return false;
        for(var ind = 0; ind < len; ind++) {
            if(oldInfo[ind].text != newInfo[ind].text)
                return false;
        }
        return true;
    }

    var updated = false;
    for (var key in checkSubDocuments) {
        if (Object.prototype.hasOwnProperty.call(checkSubDocuments, key)) {
            var sdId = parseInt(key);
            var subDocument = richEdit.document.subDocuments.getById(sdId);
            var oldInfo = currentDocumentFields[subDocument.id] || [];
            var newInfo = getSubDocumentFieldsInfo(subDocument);
            if(!equals(oldInfo, newInfo)) {
                currentDocumentFields[subDocument.id] = newInfo;
                updated = true;
            }
        }
    }
    stopProcess();
    if(updated)
        fieldsChanged();
}

function documentChanged(richEdit, subDocumentId) {
    checkSubDocuments[subDocumentId] = true;
    if (checkProcessId === null)
        checkProcessId = setTimeout(function () { updateProcess(richEdit); }, 1000);
}

options.events.contentInserted = function (s, e) {
    documentChanged(s, e.subDocumentId);
};

options.events.contentRemoved = function (s, e) {
    documentChanged(s, e.subDocumentId);
};

options.events.documentLoaded = function (s, e) {
    stopProcess();
    currentDocumentFields = {};
    s.document.subDocuments.forEach(getSubDocumentInfo);
    fieldsChanged();
};