How to add custom HTML attributes to buttons in the command column?
By default, the htmlAttributes property is available in Button. By using this property, you can add the custom attribute in the JavaScript Grid command column button.
The following code example demonstrates how to set the custom attribute to the Grid command column.
JS
<div id="Grid"></div>
<div id="commanddialog"></div>
<script>
$(function () {
$("#Grid").ejGrid({
dataSource: window.employeeView,
enableRowHover: false,
allowSelection: false,
columns: [
{ field: "EmployeeID", headerText: "Employee ID", isPrimaryKey: true, editType: ej.Grid.EditingType.NumericEdit, textAlign: ej.TextAlign.Right, width: 100 },
{ field: "FirstName", headerText: "First Name", width: 100 },
{ field: "LastName", headerText: "Last Name", width: 100 },
{ field: "HireDate", headerText: "Hire Date", width: 100, format: "{0:MM/dd/yyyy}", textAlign: ej.TextAlign.Right },
{ field: "Country", headerText: "Country", width: 100 },
{
headerText: "Employee Details",
commands: [
{
type: "details",
buttonOptions: {
htmlAttributes: { id: "CustomButton", class: "btn-primary", style: "height: 28px; width: 100px;" },
text: "Details",
click: "onClick"
}
}
],
isUnbound: true,
textAlign: ej.TextAlign.Center,
width: 150
}
]
});
MVC
@{
ViewBag.Title = "GridFeatures";
Dictionary<string, object> btn = new Dictionary<string, object>();
btn.Add("id", "CustomButton");
btn.Add("class", "btn-primary");
btn.Add("style", "height: 28px; width: 100px;");
}
@(Html.EJ().Grid<MVCGrid.EmployeeView>("CustomGrid")
.Datasource((IEnumerable<object>)ViewBag.datasource)
.EnableRowHover(false)
.AllowSelection(false)
.Columns(col =>
{
col.Field("EmployeeID").HeaderText("Employee ID").IsPrimaryKey(true).TextAlign(TextAlign.Right).Width(100).Add();
col.Field("FirstName").HeaderText("First Name").Width(100).Add();
col.Field("LastName").HeaderText("Last Name").Width(100).Add();
col.Field("HireDate").HeaderText("Hire Date").TextAlign(TextAlign.Right).Width(100).Format("{0:MM/dd/yyyy}").Add();
col.Field("Country").Width(100).HeaderText("Country").Add();
col.HeaderText("Employee Details").Commands(command =>
{
command.Type("detail")
.ButtonOptions(new Syncfusion.JavaScript.Models.ButtonProperties()
{
HtmlAttributes = btn,
Text = "Details",
Click = "onClick"
}).Add();
})
.IsUnbound(true)
.TextAlign(TextAlign.Center)
.Width(150)
.Add();
})
)
The following screenshot displays the Command column button in the Grid:
Displays the Command column button in the Grid:
Conclusion
I hope you enjoyed learning about how to add custom HTML attributes to buttons in the command column.
You can refer to our JavaScript Grid feature tour
page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration
specifications.
You can also explore
our JavaScript Grid example
to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!
Hi ILYAS,
Thanks for providing the useful information. According to your request, we will create a KnowledgeBase documentation and it will be refreshed in online sooner.
Regards,
Prasanna Kumar N.S.V