Nov 25, 2013, 3:53 PM
Updating Static Tree Panel through Direct Events (MVC, C#, Razor)
In order to push back a lot of code as far back as possible (into the Model), we've had to use a lot of direct events in order to refresh info gained from that model. So the example here: Refresh Static Tree will not work - you really can't post back to your model from a Direct Method
After trying numerous configurations of returning nodes, node collections, node collections without a root and many different JavaScripts handle the resulting returns, this post: http://forums.ext.net/showthread.php...l-to-get-nodes got it very close. The real trick was the differences in what object the Direct Event got back and what the Direct Method
got.
If you want to refresh a Tree Panel with a Direct Event, here is the relevant bits:
.cshtml (incl. Javascript)
controller.cs
I wish there were more examples in the MVC examples explorer were direct events were utilized for this very reason.
After trying numerous configurations of returning nodes, node collections, node collections without a root and many different JavaScripts handle the resulting returns, this post: http://forums.ext.net/showthread.php...l-to-get-nodes got it very close. The real trick was the differences in what object the Direct Event got back and what the Direct Method
got.
If you want to refresh a Tree Panel with a Direct Event, here is the relevant bits:
.cshtml (incl. Javascript)
<script>
var UpdateTreeSuccess = function (tree, node) {
if (!Ext.isEmpty(node)) {
tree.setRootNode( node.result[0] ); //note the node.result[0] - this is where the root object you want is in the Direct Result!
tree.getRootNode().expand( true );
}
else {
tree.getRootNode().removeAll();
}
};
</script>
Html.X().TreePanel()
.ID("TreePanel1")
.Icon(Icon.BookOpen)
.Tools(
Html.X().Tool()
.Type(ToolType.Refresh)
.ID("hiddenRefreshTree")
.DirectEvents(de =>
{
de.Click.Url = Url.Action("UpdateTree");
de.Click.Success = "UpdateTreeSuccess(App.TreePanel1, result);";
de.Click.Failure = "alert('No Tree');";
}
)
.ToolTip("Refresh")
)
.Title("Tournament Dates")
.Root(
Html.X().Node()
.NodeID("TreeRoot")
.Text("RootText")
.Children(r =>
{
//do your static tree work here
}
)
)
controller.cs
public ActionResult UpdateTree( modelType ModelName )
{
return this.Direct(ModelName.ExtNetNodeCollection);
}
model (in this case looking through data in an array to separate out by months)public Ext.Net.NodeCollection ExtNetNodeCollection
{
get
{
Ext.Net.NodeCollection oNodeCollection = new Ext.Net.NodeCollection ( );
Ext.Net.Node oRootNode = new Ext.Net.Node();
oRootNode.Text = "Some Text"
oRootNode.NodeID = "TreeRoot";
oRootNode.Leaf = false;
oNodeCollection.Add ( oRootNode );
for (int month = 1; month < 13; month++)
{
if (this.SomeData.Where(p => p.Data_Date.Month == month).Count() > 0)
{
Node oMonthNode = new Node();
oMonthNode.Text = (Convert.ToDateTime( month + "/1/" + Year)).ToString("MMMM");
oMonthNode.Leaf = false;
oRootNode.Children.Add(oMonthNode);
foreach (var rows in this.SomeData.Where(c => c.Data_Date.Month == month))
{
Node oDataNode = new Node();
oTourneyNode.Text = rows.Data_Desc + " - " + rows.Data_Date.ToString ( "MMM dd, yyyy" );
oDataNode .Leaf = true;
oMonthNode.Children.Add(oDataNode);
}
}
}
return oNodeCollection;
}
}
There are definitely disadvantages to using the Direct Events, but when you are working with a lot of information pulled data Stores that are only going to get their information through the model, rather than create new instances of that model, Direct Events are the easy route to getting your data. I wish there were more examples in the MVC examples explorer were direct events were utilized for this very reason.
Last edited by dangerlinto; Nov 26, 2013 at 12:17 PM.