翻譯|使用教程|編輯:況魚杰|2020-10-09 14:19:52.380|閱讀 514 次
概述:MailMerge是一個(gè)強(qiáng)大的,可擴(kuò)展的框架,允許您將自定義邏輯注入合并過程。TXTextControl.DocumentServer.MailMerge.FieldMerged事件可用于處理結(jié)果,但也可以訪問TXTextControl.TableCell類的周圍實(shí)例。這使您可以根據(jù)特定的過濾器指令來實(shí)現(xiàn)諸如條件表單元格顏色之類的功能。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
TX Text Control Server for ASP.NET (incl. WPF)是一個(gè)企業(yè)級(jí)的服務(wù)器端文字處理控件。它為用于ASP.NET服務(wù)器環(huán)境提供一個(gè)完全可編程的文字處理引擎,并且包含一個(gè)WPF客戶端版本。
點(diǎn)擊下載TX Text Control Server for ASP.NET (incl. WPF)最新試用版
MailMerge是一個(gè)強(qiáng)大的,可擴(kuò)展的框架,允許您將自定義邏輯注入合并過程。
TXTextControl.DocumentServer.MailMerge.FieldMerged事件可用于處理結(jié)果,但也可以訪問TXTextControl.TableCell類的周圍實(shí)例。這使您可以根據(jù)特定的過濾器指令來實(shí)現(xiàn)諸如條件表單元格顏色之類的功能。
在此示例中,如果數(shù)量大于10,則表單元格“數(shù)量”應(yīng)突出顯示為紅色,否則應(yīng)為綠色。
該示例顯示了兩個(gè)有趣的方面:
該示例實(shí)現(xiàn)HTML表單元素以設(shè)置表單元格的條件。如果輸入位置在表格單元格內(nèi)部,則以下代碼用于通過將對(duì)象序列化為Json字符串將條件存儲(chǔ)在表格單元格中:
// stores the selected conditions in the cell name function setTableCellConditions(empty = false) { TXTextControl.tables.getItem(function (table) { if (table === null) return; // no table // create a cellFilterInstructions object var cellFilterInstructions = { compareValue: document.getElementById("compareValue").value, operator: document.getElementById("operator").value, trueColor: document.getElementById("trueColor").value, falseColor: document.getElementById("falseColor").value } table.cells.getItemAtInputPosition(function (cell) { if (cell === null) return; // no cell if (empty === true) cell.setName(""); // delete instructions else // sel instructions to cell name cell.setName(JSON.stringify(cellFilterInstructions)); }); }) }
如果將輸入位置更改為另一個(gè)單元格,則會(huì)更新表單元素以反映該單元格的條件:
// check cell status on input position changes TXTextControl.addEventListener("inputPositionChanged", function () { TXTextControl.tables.getItem(function (table) { // table at input pos? if (table === null) { // return if no table available EnableFormElements( ["operator", "compareValue", "trueColor", "falseColor", "enableCondition"], false); return; } // enable form elements EnableFormElements( ["operator", "compareValue", "trueColor", "falseColor", "enableCondition"], true); table.cells.getItemAtInputPosition(function (cell) { // cell at input pos if (cell == null) { // return if more cells are selected enableCellConditions(false); document.getElementById("enableCondition").setAttribute( "disabled", "disabled"); return; } // check the cell name that stores the conditions cell.getName(function (cellName) { if (cellName === "") { enableCellConditions(false); return; } updateSettings(JSON.parse(cellName)); }); }); }) });
最后,當(dāng)單擊合并時(shí),將保存文檔并將其發(fā)送到后端控制器:
function mergeDocument() { TXTextControl.saveDocument(TXTextControl.streamType.InternalUnicodeFormat, function (e) { var serviceURL = "/Home/MergeDocument"; $.ajax({ type: "POST", url: serviceURL, contentType: 'application/json', data: JSON.stringify({ Document: e.data, }), success: successFunc, error: errorFunc }); function successFunc(data, status) { TXTextControl.loadDocument(TXTextControl.streamType.InternalUnicodeFormat, data); } function errorFunc(error) { console.log(error); } }); }
在控制器中,HttpPost方法接受文檔并調(diào)用MergeJsonData方法以將數(shù)據(jù)合并到給定的模板中:
[HttpPost] public string MergeDocument(string Document) { using (TXTextControl.ServerTextControl tx = new TXTextControl.ServerTextControl()) { tx.Create(); tx.Load(Convert.FromBase64String(Document), TXTextControl.BinaryStreamType.InternalUnicodeFormat); using (TXTextControl.DocumentServer.MailMerge mailMerge = new TXTextControl.DocumentServer.MailMerge()) { mailMerge.TextComponent = tx; mailMerge.FieldMerged += MailMerge_FieldMerged; string data = System.IO.File.ReadAllText(Server.MapPath("~/App_Data/data.json")); mailMerge.MergeJsonData(data, false); } byte[] results; tx.Save(out results, TXTextControl.BinaryStreamType.InternalUnicodeFormat); return Convert.ToBase64String(results); } }
附加了FieldMerged事件以處理自定義條件。 如果字段在表格單元格內(nèi),則將TableCell.Name屬性反序列化為CellFilterInstructions對(duì)象,以便根據(jù)定義的指令應(yīng)用表格單元格的顏色。
private void MailMerge_FieldMerged(object sender, TXTextControl.DocumentServer.MailMerge.FieldMergedEventArgs e) { // custom field handling if (e.TableCell == null) return; // if TableCell.Name has instructions, create a CellFilterInstructions object // and evaluate the instructions and set the table cell color if (e.TableCell.Name != "") { CellFilterInstructions instructions = (CellFilterInstructions)JsonConvert.DeserializeObject( e.TableCell.Name, typeof(CellFilterInstructions)); // retrieve the color Color? color = instructions.GetColor(e.MailMergeFieldAdapter.ApplicationField.Text); // apply the color if (color != null) e.TableCell.CellFormat.BackColor = (Color)color; } }
關(guān)注慧聚IT微信公眾號(hào) ???,了解產(chǎn)品的最新動(dòng)態(tài)及最新資訊。
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自: