PDA

View Full Version : [CLOSED] Handling TreePanel Server Side



jstifel
Dec 19, 2013, 9:39 AM
Hi, I want to write a page which is going to handle a treepanel completely on server side. For this I have written the following code:



<%@ Page Title="" Language="VB" MasterPageFile="~/site_no_ajax.master" AutoEventWireup="false" CodeFile="utmxmenu2.aspx.vb" Inherits="utmxmen2" %>



<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">



<extnet:ResourceManager ID="ResourceManager1" runat="server" />
<extnet:Viewport ID="Viewport1" runat="server" Layout="BorderLayout">
<Items>
<extnet:TreePanel ID="treeMenu" runat="server" Width="400" Icon="BookOpen" Title="Menu del Sistema"
AutoScroll="true" Split="true" Collapsible="false" Region="West"
RootVisible="false">
<Fields>
<extnet:ModelField Name="id" />
<extnet:ModelField Name="desc_text" />
<extnet:ModelField Name="run_text" />
<extnet:ModelField Name="param_text" />
</Fields>
<ColumnModel>
<Columns>
<extnet:TreeColumn runat="server" DataIndex="desc_text" Flex="1" Text="Descripcion"></extnet:TreeColumn>
<extnet:Column runat="server" DataIndex="run_text" Flex="1" Text="Comando"></extnet:Column>
<extnet:Column runat="server" DataIndex="param_text" Flex="1" Text="Parametros"></extnet:Column>
</Columns>
</ColumnModel>
<SelectionSubmitConfig Encode="True" />
<BottomBar>
<extnet:StatusBar ID="StatusBar1" runat="server" AutoClear="1500" />
</BottomBar>
<DirectEvents>
<ItemClick OnEvent="ItemSelected">
<ExtraParams>
<extnet:Parameter Name="id" Value="record.data.id" Mode="Raw" />
<extnet:Parameter Name="index" Value="index" Mode="Raw" />
</ExtraParams>
</ItemClick>
<ItemMove OnEvent="ItemMove">
<ExtraParams>
<extnet:Parameter Name="item" Value="item.data.id" Mode="Raw" Encode="True" />
<extnet:Parameter Name="oldparent" Value="oldParent.data.id" Mode="Raw" Encode="True" />
<extnet:Parameter Name="newparent" Value="newParent.data.id" Mode="Raw" Encode="True" />
<extnet:Parameter Name="index" Value="index" Mode="Raw" />
</ExtraParams>
</ItemMove>
</DirectEvents>
<Root>
<extnet:Node Text="Home" Icon="House" NodeID="Home">
<Children>
</Children>
<CustomAttributes>
<extnet:ConfigItem Name="id" Value="-1"></extnet:ConfigItem>
</CustomAttributes>
</extnet:Node>
</Root>
<View>
<extnet:TreeView>
<Plugins>
<extnet:TreeViewDragDrop runat="server" ContainerScroll="false" AllowLeafDrop="True" AllowContainerDrops="True" />
</Plugins>
</extnet:TreeView>
</View>
<Plugins>
<extnet:CellEditing runat="server" />
</Plugins>
<Buttons>
<extnet:Button ID="Button2" runat="server" Text="Colapsar Menu" Icon="MagifierZoomOut">
<Listeners>
<Click Handler="#{treeMenu}.collapseAll();" />
</Listeners>
</extnet:Button>
<extnet:Button ID="Button1" runat="server" Text="Expandir Menu" Icon="MagnifierZoomIn">
<Listeners>
<Click Handler="#{treeMenu}.expandAll();" />
</Listeners>
</extnet:Button>
<extnet:Button ID="Button4" runat="server" Text="Refrescar" Icon="TableRefresh">
<DirectEvents>
<Click OnEvent="MenuRefresh">
</Click>
</DirectEvents>
</extnet:Button>
</Buttons>

</extnet:TreePanel>
<extnet:FormPanel runat="server"
ID="ItemFormPanel"
Region="Center"
Title="Informacion del Item del Menu" DefaultAnchor="100%"
BodyPadding="5">
<FieldDefaults LabelWidth="150" />
<Items>
<extnet:TextField runat="server" ID="findex" FieldLabel="Indice" ReadOnly="true" />
<extnet:TextField runat="server" ID="fid" FieldLabel="Identificador" ReadOnly="true" />
<extnet:TextField runat="server" ID="fdesc_text" FieldLabel="Descripcion" />
<extnet:TextField runat="server" ID="frun_text" FieldLabel="Comando" />
<extnet:TextField runat="server" ID="fparam_text" FieldLabel="Parametros" />
</Items>
<Buttons>
<extnet:Button ID="newbtn" runat="server" Text="Nuevo" Icon="New">
<DirectEvents>
<Click OnEvent="ItemNew">
</Click>
</DirectEvents>
</extnet:Button>
<extnet:Button ID="updatebtn" runat="server" Text="Modificar" Icon="TableSave">
<DirectEvents>
<Click OnEvent="ItemUpdate">
</Click>
</DirectEvents>
</extnet:Button>
<extnet:Button ID="Button3" runat="server" Text="Borrar" Icon="TableDelete">
<DirectEvents>
<Click OnEvent="ItemDelete">
</Click>
</DirectEvents>
</extnet:Button>
</Buttons>

</extnet:FormPanel>
</Items>
</extnet:Viewport>
</asp:Content>





Imports System.Collections.Generic
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Data

Imports Psa.Data
Imports Psa.Session
Imports Psa.Files
Imports Psa.Application
Imports Psa.Convert
Imports Psa.Configuration
Imports Psa.Errors
Imports Psa.AMath

Imports Ext.Net.Utilities
Imports Ext.Net

Partial Class utmxmen2
Inherits System.Web.UI.Page

Private menu As Ext.Net.TreePanel
Private RootMenuItem As Ext.Net.Node
Private RootItem As Ext.Net.Node

Private Function ProfileId() As String
Return "0"
End Function

Protected Overloads Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
menu = treeMenu
RootMenuItem = menu.Root.Item(0)
If Not IsPostBack Then
buildMenu()
End If
End Sub

Private Sub buildMenu()
PsaData.CreateConnection(PsaSession.ConnectionStri ng())
Dim sql As String
Dim dr As DataRow = Nothing
sql = "select ParentId,Id,Desc_Text,run_text,param_text from menus where profileid = %1"
sql = sql.Replace("%1", ProfileId)
Dim ds As DataSet = PsaData.CreateDataSet(sql)

Dim dbRow As System.Data.DataRow
For Each dbRow In ds.Tables(0).Rows
If dbRow("ParentId").ToString = "0" Then
dbRow("ParentId") = System.DBNull.Value
End If
Next
ds.Relations.Add("NodeRelation", ds.Tables(0).Columns("Id"), ds.Tables(0).Columns("ParentId"))

For Each dbRow In ds.Tables(0).Rows
If (dbRow.IsNull("ParentId")) Then
RootItem = CreateItem(dbRow)
RootMenuItem.Children.Add(RootItem)
PopulateSubMenu(dbRow, RootItem)
RootItem.Icon = Icon.Folder
If RootItem.Children.Count = 0 Then
RootItem.Leaf = True
RootItem.Icon = Icon.Application
End If
End If
Next dbRow
' menu.CollapseAll()
RootMenuItem.Expanded = True
End Sub

Private Sub PopulateSubMenu(ByVal dbRow As System.Data.DataRow, ByVal item As Ext.Net.Node)
Dim childRow As System.Data.DataRow
For Each childRow In dbRow.GetChildRows("NodeRelation")
Dim childItem As Ext.Net.Node = CreateItem(childRow)
item.Children.Add(childItem)
PopulateSubMenu(childRow, childItem)
If childItem.Children.Count = 0 Then
childItem.Leaf = True
childItem.Icon = Icon.Application
End If
Next childRow
End Sub

Private Function CreateItem(ByVal dbRow As System.Data.DataRow) As Ext.Net.Node
Dim item As New Ext.Net.Node()
item.Text = PsaConvert.ObjToString(dbRow("desc_text"))
item.CustomAttributes.Add(New Ext.Net.ConfigItem() With {.Name = "id", .Value = dbRow("id").ToString()})
item.CustomAttributes.Add(New Ext.Net.ConfigItem() With {.Name = "desc_text", .Value = dbRow("desc_text").ToString()})
item.CustomAttributes.Add(New Ext.Net.ConfigItem() With {.Name = "run_text", .Value = dbRow("run_text").ToString()})
item.CustomAttributes.Add(New Ext.Net.ConfigItem() With {.Name = "param_text", .Value = dbRow("param_text").ToString()})
Return item
End Function

Protected Sub MenuRefresh(sender As Object, e As DirectEventArgs)
Try
menu.Root.Clear()
buildMenu()
menu.Refresh()
Notification.Show(New NotificationConfig With {.Title = "Item", .Html = "Menu refrescado"})
Catch ex As Exception
e.ErrorMessage = ex.Message
e.Success = False
End Try
End Sub

Protected Sub ItemSelected(sender As Object, e As DirectEventArgs)
' Notification.Show(New NotificationConfig With {.Title = "Item", .Html = e.ExtraParams("id") + "</br>" + e.ExtraParams("index")})
'Exit Sub
Try
Dim sql As String = "select * from menus where profileId = %1 and Id = %2"
sql = sql.Replace("%1", ProfileId)
sql = sql.Replace("%2", e.ExtraParams("id"))
Dim dr As DataRow = PsaData.CreateDataRow(sql)
findex.Text = e.ExtraParams("index")
fid.Text = e.ExtraParams("id")
fdesc_text.Text = ""
frun_text.Text = ""
fparam_text.Text = ""
If dr IsNot Nothing Then
fdesc_text.Text = PsaConvert.ObjToString(dr("desc_text"))
frun_text.Text = PsaConvert.ObjToString(dr("run_text"))
fparam_text.Text = PsaConvert.ObjToString(dr("param_text"))
End If
Catch ex As Exception
e.ErrorMessage = ex.Message
e.Success = False
End Try
End Sub

Protected Sub ItemMove(sender As Object, e As DirectEventArgs)
Dim i As Integer = 1
End Sub

Protected Sub ItemUpdate(sender As Object, e As DirectEventArgs)
Try
If fdesc_text.Text = "" Then
Throw New Exception("Descripcion es requerida")
End If
Dim sql As String
Dim dr As DataRow
sql = "select * from menus where profileId = %1 and Id = %2"
sql = sql.Replace("%1", ProfileId)
sql = sql.Replace("%2", fid.Text)
dr = PsaData.CreateDataRow(sql)
dr("desc_text") = fdesc_text.Text
dr("run_text") = frun_text.Text
dr("param_text") = fparam_text.Text
PsaData.UpdateTable(dr.Table, "select * from menus where 1=0")
Dim node As Ext.Net.Node = treeMenu.Root.Item(findex.Text)
If node IsNot Nothing Then
node.Text = fdesc_text.Text
End If
Notification.Show(New NotificationConfig With {.Title = "Item", .Html = "Item actualizado"})
Catch ex As Exception
e.ErrorMessage = ex.Message
e.Success = False
End Try
End Sub

Protected Sub ItemDelete(sender As Object, e As DirectEventArgs)
Try
Dim sql As String
sql = "delete from menus where profileId = %1 and Id = %2"
sql = sql.Replace("%1", ProfileId)
sql = sql.Replace("%2", fid.Text)
PsaData.ExecuteNonQuery(sql)
fdesc_text.Text = "Eliminado"
Dim node As Ext.Net.Node = treeMenu.Root.Item(fid.Text)
If node IsNot Nothing Then
node.Text = fdesc_text.Text
End If
Notification.Show(New NotificationConfig With {.Title = "Item", .Html = "Item eliminado"})
Catch ex As Exception
e.ErrorMessage = ex.Message
e.Success = False
End Try
End Sub


Protected Sub ItemNew(sender As Object, e As DirectEventArgs)
Try
If fdesc_text.Text = "" Then
Throw New Exception("Descripcion es requerida")
End If
Dim sql As String
Dim dr As DataRow
sql = "select * from menus where 1=0"
Dim dt As DataTable = PsaData.CreateDataTable(sql)
dr = dt.NewRow
dt.Rows.Add(dr)
sql = "select max(id) from menus where profileId = %1"
sql = sql.Replace("%1", ProfileId)
Dim id As Integer = PsaConvert.ObjToInt(PsaData.ExecuteScalar(sql)) + 1
dr("profileId") = ProfileId()
dr("id") = id
dr("desc_text") = fdesc_text.Text
dr("run_text") = frun_text.Text
dr("param_text") = fparam_text.Text
PsaData.UpdateTable(dr.Table, "select * from menus where 1=0")
If fid.Text = "" Then
Else
End If
RootItem = CreateItem(dr)
RootMenuItem.Children.Add(RootItem)
Notification.Show(New NotificationConfig With {.Title = "Item", .Html = "Item creado"})
Catch ex As Exception
e.ErrorMessage = ex.Message
e.Success = False
End Try
End Sub

End Class


Here on ItemNew i create a new item, added to database an tried to hung it to root menu. Of course I see nothing back at client side.
How can I manage to add,delete and update a node back to treeMenu on client side?

Daniil
Dec 19, 2013, 1:15 PM
Hi @jstifel,

What about the following approach?
http://examples2.ext.net/#/TreePanel/Advanced/Remote_Mode/

jstifel
Dec 20, 2013, 12:23 PM
Yes, I checked it but still want to do it all on server side. Is it there a way? If not, I will go try to do it using remote mode.

Daniil
Dec 20, 2013, 2:58 PM
Hope this helps.

Get a node by id

NodeProxy node = this.treeMenu.GetNodeById("node id");

Update

node.SetText("new text");

Append a child

node.AppendChild(new Node { Text = "new node" };

Remove

node.Remove();