I am using an ASP.NET MVC EJ GRID to display data.
@(Html.EJ().Grid<object>("ResultGrid")
.Datasource((DataTable)Model)
.ClientSideEvents(events => { events.DataBound("resultDataBound").RecordDoubleClick("doubleClick").Create("create").ToolbarClick("OnToolbarClick"); })
.AllowResizeToFit()
.AllowScrolling(false)
.AllowSorting()
.AllowMultiSorting()
.AllowKeyboardNavigation(true)
.AllowGrouping()
.AllowReordering()
.AllowResizing()
.AllowFiltering()
.FilterSettings(fs => { fs.FilterType(FilterType.Excel); })
.AllowRowDragAndDrop().EnableAltRow(true)
.AllowPaging()
.PageSettings(page => page.PageSize(12))
.ToolbarSettings(toolBar => toolBar.ShowToolbar().ToolbarItems(items =>
{
items.AddTool(ToolBarItems.Search);
items.AddTool(ToolBarItems.ExcelExport);
items.AddTool(ToolBarItems.WordExport);
items.AddTool(ToolBarItems.PdfExport);
})).Mappers(map => map.ExportToExcelAction("/Home/ExportToExcel").ExportToPdfAction("/Home/ExportToPdf").ExportToWordAction("/Home/ExportToWord"))
)
When the grid is rendered, I then use JS to format a specific column in the grid.
The 1st column always contain the ID of the record.
At the moment we are only displaying 5 columns.
With the ID column, we reformat it so it becomes a hyperlink.
function formatGrid(gridObj) {
var columncount = gridObj.getVisibleColumnNames().length;
// Loop through each column and format only the column where the header text is "Id".
for (i = 0; i < columncount; i++) {
var column = gridObj.getColumnByIndex(i);
var header = column.headerText;
// If user returns more than 5 columns, hide the rest.
if (i > 4) {
gridObj.hideColumns(header);
}
// Only do this reformat if it is only an ID and does not contain the <a> tag.
if (header.toLowerCase() == "id") {
column.format = "<a rel='nofollow' href='#' onclick='idClick(this.text);'>{Id}</a>";
}
}
}
QUESTIONS:
1. The ISSUE is that if I allow the display of more than 5 columns, for some reason, the “column.format” fails. The ID column rows are not hyperlinked. If I restrict it to display only 5 columns or less, the “column.format” works and the ID column rows become hyperlink.
2. Instead of “hideColumns”, is there a way to make the unwanted columns not visible?
3. In the above @(Html.EJ().Grid<object>("ResultGrid"), how do you specify “actionComplete”? I want to use this event to call the formatGrid() method.
|
@(Html.EJ().Grid<Object>("FlatGrid")
.Datasource((IEnumerable<object>)ViewBag.DataSource)
.ClientSideEvents(events => { events.DataBound("resultDataBound").ActionComplete("GridActionCompleteEvent"); })
-------
)
<script type="text/javascript">
function resultDataBound(args) {
var len = args.model.columns.length;
for (var i = 0; i < len ; i++) {
if (args.model.columns[i]['headerText'].toLowerCase() == "orderid") {
args.model.columns[i]['format'] = "<a rel='nofollow' href='#' onclick='idClick(this.text);'>{OrderID}</a>"; // setting format the column which we want to set hyperlink.
}
if (i > 4) {
args.model.columns[i]['visible'] = false; // setting the visible property as false for unwanted column in Grid.
}
}
this.refreshContent(true);
}
</script>
|
|
@(Html.EJ().Grid<Object>("FlatGrid")
.Datasource((IEnumerable<object>)ViewBag.DataSource)
.ClientSideEvents(events => {events.ActionComplete("GridActionCompleteEvent"); })
----
)
<script type="text/javascript">
function GridActionCompleteEvent(args) {
// Do your stuff here
}
</script>
|