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

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();
};