Thanks for that, it works just fine. I now need to know how to set the selected index of the menu once I have removed an item from the collection, or adding a new one. This manipulation will be done through a direct event or direct method. Once I delete an item I would like to set the menu index back to zero.
Thanks!
Here's some sample code:
Markup:
<ext:Panel ID="pnlProducts" runat="server" Icon="Package" Title="Products" Padding="10" Border="false" Layout="BorderLayout">
<Items>
<ext:MenuPanel ID="mpProducts" runat="server" Selectable="true" SelectedIndex="0" Region="West" Width="200">
<Menu ID="mItems" runat="server" ShowSeparator="false">
<Listeners>
<ItemClick Fn="menuItemClick" />
</Listeners>
</Menu>
</ext:MenuPanel>
<ext:Panel ID="pnlProductDetail" runat="server" Title="Product Info" AutoScroll="true" Region="Center">
<TopBar>
<ext:Toolbar ID="toolProduct" runat="server">
<Items>
<ext:ToolbarFill ID="ToolbarFill4" runat="server" />
<ext:Button ID="btnAddProduct" runat="server" Icon="Add" Text="Add">
<Listeners>
<Click Handler="#{productWindow}.show();" />
</Listeners>
</ext:Button>
<ext:Button ID="btnDeleteProduct" runat="server" Icon="Delete" Text="Delete Selected">
<DirectEvents>
<Click OnEvent="btnDeleteProduct_Click">
<Confirmation ConfirmRequest="true" Message="Are you sure you want to delete the selected product?" />
<ExtraParams>
<ext:Parameter Name="productId" Value="menu_getSelectedId(#{mpProducts})" Mode="Raw">
</ext:Parameter>
</ExtraParams>
</Click>
</DirectEvents>
</ext:Button>
</Items>
</ext:Toolbar>
</TopBar>
</ext:Panel>
</Items>
</ext:Panel>
Script:
var menu_getSelectedId = function (m) {
var selectedIds = [];
var index = parseInt(m.getSelIndexField().getValue());
var item;
if (!isNaN(index)) {
item = m.menu.items.get(index);
selectedIds.push(item.productid);
}
return Ext.encode(selectedIds);
}
Code Behind:
protected void btnDeleteProduct_Click(object sender, DirectEventArgs e)
{
int[] ids = JSON.Deserialize<int[]>(e.ExtraParams["productId"]);
DeleteProduct(ids);
}
public bool DeleteProduct(int[] ProductIds)
{
bool returnVal = false;
if (ProductIds.IsNotNull())
{
try
{
foreach (int ProductId in ProductIds)
{
RxGroupProductService.Instance.Delete(ProductId);
}
LoadData();
LoadProductsMenu();
mpProducts.SetSelectedIndex(mpProducts.Menu.Items.Count - 1);
pnlProductDetail.UpdateContent();
mpProducts.Menu.UpdateContent();
returnVal = true;
}
catch (Exception ex)
{
Ext.Net.X.Msg.Show(new MessageBoxConfig{
Title = "Error deleting product",
Message = ex.Message,
Icon = MessageBox.Icon.ERROR,
Buttons = MessageBox.Button.OK
});
}
}
return returnVal;
}
private void LoadProductsMenu()
{
mpProducts.Menu.Items.Clear();
pnlProductDetail.Items.Clear();
RxGroupData.RxGroupProductCollection.ForEach(rxp =>
{
RxGroupProductService.Instance.DeepLoad(rxp, false, DAL.DeepLoadType.IncludeChildren,
typeof(Product));
Ext.Net.MenuItem mi = new Ext.Net.MenuItem();
mi.Text = rxp.ProductIdSource.Name;
mi.CustomConfig.Add(new ConfigItem("productid", rxp.RxGroupProductId.ToString()));
mpProducts.Menu.Items.Add(mi);
});
if (mpProducts.Menu.Items.Count > 0)
{
RxGroupProduct rxp = RxGroupProductService.Instance.GetByRxGroupProductId(mpProducts.Menu.Items[0].CustomConfig[0].Value.ChangeType<int>());
ProductDetailForm pdf = new ProductDetailForm(rxp.ProductId, ItemIdAsInt32);
pnlProductDetail.Items.Add(pdf);
}
else
{
Ext.Net.MenuItem mi = new Ext.Net.MenuItem();
mi.Text = "No Products";
mi.CustomConfig.Add(new ConfigItem("productid", "0"));
mpProducts.Menu.Items.Add(mi);
ProductDetailForm pdf = new ProductDetailForm(0, ItemIdAsInt32);
pnlProductDetail.Items.Add(pdf);
}
}