@(Html.EJ().Grid<object>("Grid")
.AllowPaging()
.Datasource(ds =>
{
ds.URL("/Grid/UrlDataSource");
ds.DataType("xml");
ds.Offline(true);
ds.Adaptor(AdaptorType.UrlAdaptor);
})
.AllowSorting()
.Columns(col =>
{
col.Field("OrderID").Add();
col.Field("CustomerID").Add();
col.Field("url").Add();
})
.ClientSideEvents(events => events.Load("gridLoad").DataBound("bound"))
)
<script>
var json
function bound(args) {
defineColumns(json.Order.ColumnDetails); // call a funciton to define the column details
}
function gridLoad(args) {
this.model.dataSource.adaptor = new xmlAdaptor();
}
//for webforms use ej.WebMethodAdaptor
var xmlAdaptor = new ej.UrlAdaptor().extend({
//Overriding default processResponse function
processResponse: function (data, ds, query, xhr, request, changes) {
return processXML(data);
}
});
function processXML(ele) {
json = ConvertToJSON(ele);
return json.Order.OrderDetails;
}
function defineColumns(column) {
var Obj = $("#Grid").ejGrid("instance"); // take the Grid instance using Grid ID
Obj.model.columns = []; // empty the existing column details
for (var i = 0; i < column.length; i++) {
Obj.model.columns.push({ field: column[i].position, width: parseInt(column[i].width), headerText: column[i].position, visible: column[i].visibility == "true" })
}
Obj.columns(Obj.model.columns) // update the column with new value
}
//Function to convert XML document to JSON object
function ConvertToJSON(ele) {
. . . . .
return json;
}
</script>
|
[HttpPost]
public XDocument UrlDataSource(DataManager dm)
{
string json = @"<?xml version='1.0' standalone='no'?>
<Order>
<OrderDetails id='1'>
<OrderID>10248</OrderID>
<EmpoyeeID>1</EmpoyeeID>
<CustomerID>Alan</CustomerID>
<url>http://www.google.com</url>
</OrderDetails>
. . . . . . .
<ColumnDetails>
<position>url</position>
<width>20</width>
<visibility>true</visibility>
</ColumnDetails>
</Order>";
Response.ContentType = "text/xml";
XmlDocument doc1 = new XmlDocument();
doc1.LoadXml(json);
var doc = DocumentToXDocument(doc1);
return doc;
}
private static XDocument DocumentToXDocument(XmlDocument doc)
{
return XDocument.Parse(doc.OuterXml);
}
|
@(Html.EJ().Grid<object>("Grid")
…
.AllowSorting()
.Columns(col =>
{
..
})
.ClientSideEvents(events => events.Load("gridLoad").DataBound("bound"))
)
<script>
var json
function bound(args) {
defineColumns(json.Order.ColumnDetails); // call a funciton to define the column details
}
function defineColumns(column) {
var Obj = $("#Grid").ejGrid("instance"); // take the Grid instance using Grid ID
Obj.model.columns = []; // empty the existing column details
for (var i = 0; i < column.length; i++) {
Obj.model.columns.push({ field: column[i].position, width: parseInt(column[i].width), headerText: column[i].position, visible: column[i].visibility =="true" })
}
Obj.columns(Obj.model.columns) // update the column with new value
}
}
</script>
|
@(Html.EJ().Grid<object>("Grid")
.AllowPaging()
.Datasource(ds => ds.Json((IEnumerable<object>)ViewBag.dataSource).Adaptor(AdaptorType.RemoteSaveAdaptor))
.AllowSorting()
.Columns(col =>
{
col.Field("OrderID").Add();
col.Field("CustomerID").Add();
col.Field("Freight").Add();
col.Field("EmployeeID").Add();
})
.ClientSideEvents(events => events.Load("gridLoad").DataBound("bound"))
)
<script>
var json
function bound(args) {
$.ajax({ // ajax post to retireve the xml serialized data in string format
url: "/Grid/UrlDataSource",
type: "POST",
success: function (data) {
processXML(data); // to deserialize the string
defineColumns(json.SfDataGrid.Columns.GridColumn); // update the column.
}
})
}
function processXML(ele) {
json = ConvertToJSON(ele);
}
function defineColumns(column) {
var Obj = $("#Grid").ejGrid("instance"); // take the Grid instance using Grid ID
for(var j=0;j<Obj.model.columns.length;j++){
for (var i = 0; i < column.length; i++) {
if (Obj.model.columns[j].field == column[i].HeaderText)
Obj.model.columns[j].visible = column[i].IsHidden != "true";
}
}
Obj.columns(Obj.model.columns) // update the column with new value
}
//Function to convert XML document to JSON object
function ConvertToJSON(ele) {
var json = {}, e, ch;
var addItem = function (parent, name, value) {
if (!parent[name])
parent[name] = value;
else {
if (parent[name].constructor != Array)
parent[name] = [parent[name]];
parent[name][parent[name].length] = value;
}
}
for (e = 0, ch = ele.childNodes; e < ch.length; e++) {
if (ch[e].nodeType == 1) {
if (ch[e].childNodes.length == 1 && ch[e].firstChild.nodeType == 3)
addItem(json, ch[e].nodeName, ch[e].firstChild.nodeValue);
else if (ch[e].childNodes.length != 0)
addItem(json, ch[e].nodeName, ConvertToJSON(ch[e]));
}
}
return json;
}
</script>
|
[HttpPost]
public XDocument UrlDataSource(DataManager dm)
{
string json = @"<?xml version='1.0' encoding='UTF-8'?>
<SfDataGrid xmlns='http://schemas.datacontract.org/2004/07/Syncfusion.UI.Xaml.Grid' xmlns:i='http://www.w3.org/2001/XMLSchema-instance'>
<AllowDraggingColumns>true</AllowDraggingColumns>
<AllowGrouping>true</AllowGrouping>
<AllowResizingColumns>true</AllowResizingColumns>
<ColumnSizer>Star</ColumnSizer>
<Columns>
<GridColumn i:type='GridTextColumn'>
<AllowBlankFilters>true</AllowBlankFilters>
<AllowFocus>true</AllowFocus>
<AllowResizing>false</AllowResizing>
<ColumnSizer>Auto</ColumnSizer>
<FilterRowCondition>BeginsWith</FilterRowCondition>
<FilterRowEditorType>TextBox</FilterRowEditorType>
<FilteredFrom>None</FilteredFrom>
<GridValidationMode>None</GridValidationMode>
<IsHidden>false</IsHidden>
<HeaderText>OrderID</HeaderText>
<HorizontalHeaderContentAlignment>Center</HorizontalHeaderContentAlignment>
<MappingName>WBS</MappingName>
<MaximumWidth>NaN</MaximumWidth>
<MinimumWidth>NaN</MinimumWidth>
<Width>20.78</Width>
<TextWrapping>NoWrap</TextWrapping>
</GridColumn>
<GridColumn i:type='GridTextColumn'>
<AllowBlankFilters>true</AllowBlankFilters>
<AllowFocus>true</AllowFocus>
<FilterRowCondition>BeginsWith</FilterRowCondition>
<FilterRowEditorType>TextBox</FilterRowEditorType>
<FilteredFrom>None</FilteredFrom>
<GridValidationMode>None</GridValidationMode>
<HeaderText>CustomerID</HeaderText>
<HorizontalHeaderContentAlignment>Center</HorizontalHeaderContentAlignment>
<MappingName>Label</MappingName>
<MaximumWidth>NaN</MaximumWidth>
<MinimumWidth>NaN</MinimumWidth>
<Width>161</Width>
<TextWrapping>Wrap</TextWrapping>
</GridColumn>
<GridColumn i:type='GridNumericColumn'>
<AllowBlankFilters>true</AllowBlankFilters>
<AllowFocus>true</AllowFocus>
<FilterRowCondition>Equals</FilterRowCondition>
<FilterRowEditorType>Numeric</FilterRowEditorType>
<FilteredFrom>None</FilteredFrom>
<GridValidationMode>None</GridValidationMode>
<HeaderText>Freight</HeaderText>
<HorizontalHeaderContentAlignment>Center</HorizontalHeaderContentAlignment>
<IsHidden>true</IsHidden>
<MappingName>Start</MappingName>
<MaximumWidth>NaN</MaximumWidth>
<MinimumWidth>NaN</MinimumWidth>
<TextAlignment>Right</TextAlignment>
<Width>NaN</Width>
<MaxValue>79228162514264337593543950335</MaxValue>
<MinValue>-79228162514264337593543950335</MinValue>
<NullText />
<NumberDecimalDigits>2</NumberDecimalDigits>
<NumberDecimalSeparator>,</NumberDecimalSeparator>
<NumberGroupSeparator>.</NumberGroupSeparator>
<NumberGroupSizes xmlns:a='http://schemas.microsoft.com/2003/10/Serialization/Arrays'>
<a:int>0</a:int>
</NumberGroupSizes>
<NumberNegativePattern>1</NumberNegativePattern>
</GridColumn>
<GridColumn i:type='GridNumericColumn'>
<AllowBlankFilters>true</AllowBlankFilters>
<AllowFocus>true</AllowFocus>
<FilterRowCondition>Equals</FilterRowCondition>
<FilterRowEditorType>Numeric</FilterRowEditorType>
<FilteredFrom>None</FilteredFrom>
<GridValidationMode>None</GridValidationMode>
<HeaderText>EmployeeID</HeaderText>
<HorizontalHeaderContentAlignment>Center</HorizontalHeaderContentAlignment>
<IsHidden>true</IsHidden>
<MappingName>Finish</MappingName>
<MaximumWidth>NaN</MaximumWidth>
<MinimumWidth>NaN</MinimumWidth>
<TextAlignment>Right</TextAlignment>
<Width>NaN</Width>
<MaxValue>79228162514264337593543950335</MaxValue>
<MinValue>-79228162514264337593543950335</MinValue>
<NullText />
<NumberDecimalDigits>2</NumberDecimalDigits>
<NumberDecimalSeparator>,</NumberDecimalSeparator>
<NumberGroupSeparator>.</NumberGroupSeparator>
<NumberGroupSizes xmlns:a='http://schemas.microsoft.com/2003/10/Serialization/Arrays'>
<a:int>0</a:int>
</NumberGroupSizes>
<NumberNegativePattern>1</NumberNegativePattern>
</GridColumn>
. . . . . .
</Columns>
</SfDataGrid>";
Response.ContentType = "text/xml";
XmlDocument doc1 = new XmlDocument();
doc1.LoadXml(json);
var doc = DocumentToXDocument(doc1);
return doc;
}
private static XDocument DocumentToXDocument(XmlDocument doc)
{
return XDocument.Parse(doc.OuterXml);
} |