Cell Template in ASP.NET MVC Spreadsheet Control
28 Oct 202511 minutes to read
Cell Template is used for adding HTML elements into Spreadsheet. You can add the cell template in spreadsheet by using the template property and specify the address using the address property inside the ranges property. You can customize the HTML elements similar to Syncfusion® components (TextBox, DropDownList, RadioButton, MultiSelect, DatePicker etc) by using the beforeCellRender event. In this demo, Cell template is applied to C2:C9 and instantiated with HTML input components like TextBox, RadioButton, TextArea. You need to bind the events to perform any operations through HTML elements or Syncfusion® components. Here, we have added change event in to the MultiSelect control, and we have updated the selected data into the spreadsheet cell through that change event.
The following code example describes the above behavior.
@Html.EJS().Spreadsheet("spreadsheet").ShowRibbon(false).ShowFormulaBar(false).AllowEditing(false).Created("createdHandler").BeforeSelect("beforeSelectHandler").BeforeCellRender("beforeCellRenderHandler").ScrollSettings(scrollSettings =>
{
scrollSettings.EnableVirtualization(false).IsFinite(true);
}).Sheets(sheet =>
{
sheet.Name("Registration Form").RowCount(40).ColCount(30).ShowGridLines(false).Rows(row =>
{
row.Height(55).Cells(cell => cell.Index(1).Value("Interview Registration Form").Style(style =>
{
style.FontWeight(FontWeight.Bold).TextAlign(TextAlign.Center).VerticalAlign(VerticalAlign.Middle).FontSize("12pt");
}).Add()).Add();
row.Height(45).Cells(cell => cell.Index(1).Value("Name").Add()).Add();
row.Height(45).Cells(cell => cell.Index(1).Value("Date of Birth:").Add()).Add();
row.Height(45).Cells(cell => cell.Index(1).Value("Gender:").Add()).Add();
row.Height(45).Cells(cell => cell.Index(1).Value("Year of Experience:").Add()).Add();
row.Height(45).Cells(cell => cell.Index(1).Value("Areas of Interest:").Add()).Add();
row.Height(45).Cells(cell => cell.Index(1).Value("Mobile Number:").Add()).Add();
row.Height(45).Cells(cell => cell.Index(1).Value("Email:").Add()).Add();
row.Height(82).Cells(cell => cell.Index(1).Value("Address:").Add()).Add();
}).Columns(column =>
{
column.Index(1).Width(190).Add();
column.Width(350).Add();
}).Ranges(ranges =>
{
ranges.Template("<input />").Address("C2:C3").Add();
ranges.Template("<div><input type='radio' name='gender' value='male' /><input type='radio' name='gender' value='female'/></div>").Address("C4").Add();
ranges.Template("<input />").Address("C5:C8").Add();
ranges.Template("<textarea rows='3'/>").Address("C9").Add();
ranges.Template("<button class='e-btn e-flat' style='float:right'>Add</button>").Address("C11").Add();
}).Add();
}).Render()
<script>
function createdHandler() {
//Applies format to specified range
this.cellFormat({ fontWeight: 'bold' }, 'B2:B9');
}
function beforeSelectHandler(args) {
//Prevents selection
args.cancel = true;
}
function beforeCellRenderHandler(args) {
//Initializing input components before cell rendering
if (this.activeSheetIndex === 0) {
var target = args.element.firstElementChild;
switch (args.address) {
case 'B1':
args.element.colSpan = 2;
break;
case 'C2':
new ej.inputs.TextBox({ placeholder: 'Name' }, target);
break;
case 'C3':
new ej.calendars.DatePicker({ placeholder: 'DOB', }, target);
break;
case 'C4':
new ej.buttons.RadioButton({ label: 'Male' }, args.element.firstElementChild.firstElementChild);
new ej.buttons.RadioButton({ label: 'Female' }, args.element.firstElementChild.lastElementChild);
break;
case 'C5':
var experience = ['0 - 1 year', '1 - 3 years', '3 - 5 years', '5 - 10 years'];
new ej.dropdowns.DropDownList({
placeholder: 'Experience',
dataSource: experience
}, target);
break;
case 'C6':
var languages = ['JAVA', 'C#', 'SQL'];
new ej.dropdowns.MultiSelect({
showClearButton: false,
placeholder: 'Areas of Interest',
dataSource: languages,
change: function(evt) {
if (args.cell) {
debugger
args.cell.value = evt.value.toString();
} else {
var range = ej.spreadsheet.getRangeIndexes(evt.address);
spreadsheet.sheets[spreadsheet.activeSheetIndex].rows[range[0]].cells[range[1]] = { value: evt.value.toString() };
}
}
}, target);
break;
case 'C7':
new ej.inputs.TextBox({
placeholder: 'Mobile Number'
}, target);
break;
case 'C8':
new ej.inputs.TextBox({
placeholder: 'Email'
}, target);
break;
case 'C9':
new ej.inputs.TextBox(null, target);
break;
}
}
}
</script>
<style>
.e-spreadsheet .e-tab .e-tab-text {
display: inherit;
}
.e-spreadsheet .e-cell .e-radio-wrapper {
margin: 5px;
}
.e-spreadsheet .e-cell .e-radio-wrapper:first-child {
margin-left: 0;
}
.e-spreadsheet .e-cell .e-radio + label .e-label {
color: rgba(0, 0, 0, 0.87);
}
</style>public ActionResult Index()
{
return View();
}