This is a migrated thread and some comments may be shown as answers.

Advanced Form: using RadGrid for multi-valued resource

2 Answers 44 Views
Scheduler
This is a migrated thread and some comments may be shown as answers.
Ellie
Top achievements
Rank 1
Ellie asked on 28 Feb 2020, 10:21 AM

Hi all,

I have 2 resource types in my scheduler that need to be multi-select: Student Group & Room. I've implemented Student Group as per the MultipleValuesResourceControl from the demo using a DBProvider & all is working fine.

However, I'm attempting to show Rooms as a RadGrid because I need to show room number, description, capacity & room type, which is rather too long to concatenate into a list item. The RadGrid is showing the rooms correctly & on sample appointments where I've pre-populated the rooms in the database they are being ticked/selected correctly - although I had to move that into ItemDataBound.

When stepping through the code I can see that the data key values are being pulled from the grid into the array & into ResRoom.Value; however the resource doesn't get attached to the appointment. When the SchedulerDBProvider is reached for insert or update only student groups are in the resource list.

 Is this due to the javascript? I saw that AdvancedForm.js only handles multi-valued resources as checkboxes & modified as below, but still have the issue.

01.this._scheduler.get_resourceTypes().forEach(function (resourceType) {
02.    var resourceTypeName = resourceType.get_name();
03.    var baseName = template._templateId + "_Res" + resourceTypeName + resourceControlSuffix;
04.    var resourcesOfThisType = schedulerResources.getResourcesByType(resourceTypeName);
05.                          
06.    if (resourceType.get_allowMultipleValues()) {
07.        if (resourceTypeName = "Room") {
08.            var masterTable = $find(baseName).get_MasterTableView();
09.            var items = masterTable.get_dataItems();
10.            if (items.length > 0)
11.                apt.get_resources().removeResourcesByType(resourceTypeName);
12. 
13.            for (var i = 0; i < count.length; i++) {                                   
14.                if (items[i].selected && resourcesOfThisType.get_count() >= i) {                                      
15.                    apt.get_resources().add(resourcesOfThisType.getResource(i));
16.                }
17.            };
18.        }
19.        else {
20.            var checkBoxes = $(String.format("input[id*='{0}']", baseName), this._formElement);
21. 
22.            if (checkBoxes.length > 0)
23.                apt.get_resources().removeResourcesByType(resourceTypeName);
24. 
25.            for (var i = 0; i < checkBoxes.length; i++) {
26.                if (checkBoxes[i].checked && resourcesOfThisType.get_count() >= i)
27.                    apt.get_resources().add(resourcesOfThisType.getResource(i));
28.            };
29.        }
30.    }

I'm using SchedulerDBProvider rather than a WebService

thanks,

Ellie

2 Answers, 1 is accepted

Sort by
0
Ellie
Top achievements
Rank 1
answered on 12 Mar 2020, 03:09 PM

In case this topic is useful to anyone here is my (now working) resource control for a radgrid

GridValuesResourceControl.ascx

01.<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="GridValuesResourceControl.ascx.cs" Inherits="Scheduler.GridValuesResourceControl" %>
02.<asp:Label runat="server" ID="ResourceLabel" AssociatedControlID="ResourceValuesPlaceHolder" Text='<%# DisplayLabel %>' CssClass="rfbLabel" />
03.<asp:PlaceHolder runat="server" ID="ResourceValuesPlaceHolder">
04.    <telerik:RadGrid ID="ResourceValue" runat="server" AutoGenerateColumns="false" AllowMultiRowSelection="true" OnItemDataBound="ResourceValue_ItemDataBound">
05.        <ClientSettings>
06.          <Selecting AllowRowSelect="true"/>
07.        </ClientSettings>
08.        <MasterTableView DataKeyNames="RoomID" ClientDataKeyNames="RoomID">
09.            <Columns>
10.                <telerik:GridClientSelectColumn UniqueName="ClientSelectColumn" />
11.                <telerik:GridBoundColumn DataField="RoomID" UniqueName="RoomID" Display="false"/>
12.                <telerik:GridBoundColumn DataField="RoomNumber" HeaderText="Room"  />
13.                <telerik:GridBoundColumn DataField="RoomDesc" />
14.                <telerik:GridBoundColumn DataField="Capacity" HeaderText="Capacity" />
15.                <telerik:GridBoundColumn DataField="RoomType" HeaderText="Type" />
16.            </Columns>
17.        </MasterTableView>
18.    </telerik:RadGrid>
19.</asp:PlaceHolder>

 

GridValuesResourceControl.ascx.cs

001.using System;
002.using System.Collections;
003.using System.Collections.Generic;
004.using System.ComponentModel;
005.using System.Web.UI.WebControls;
006.using Telerik.Web.UI;
007. 
008.using Scheduler.Models;
009.using Scheduler.Models.DAL;
010. 
011. 
012.namespace Scheduler
013.{
014.    public partial class GridValuesResourceControl : System.Web.UI.UserControl
015.    {
016.        private string _type;
017. 
018.        protected Appointment Appointment
019.        {
020.            get
021.            {
022.                SchedulerFormContainer container = (SchedulerFormContainer)BindingContainer;
023.                return container.Appointment;
024.            }
025.        }
026. 
027.        protected RadScheduler Owner
028.        {
029.            get  { return Appointment.Owner; }
030.        }
031. 
032.        [Bindable(BindableSupport.Yes, BindingDirection.TwoWay)]
033.        public object Value
034.        {
035.            get  { return ExtractResourceValues(); }
036.            set { }
037.        }
038. 
039.        [Bindable(BindableSupport.Yes, BindingDirection.OneWay)]
040.        public string DisplayLabel
041.        {
042.            get { return ResourceLabel.Text; }
043. 
044.            set { ResourceLabel.Text = value; }
045.        }
046. 
047.        [Bindable(BindableSupport.Yes, BindingDirection.OneWay)]
048.        public string Type
049.        {
050.            get { return _type; }
051.            set {  _type = value; }
052.        }
053. 
054.        protected void Page_Load(object sender, EventArgs e)
055.        {
056.            RadGrid resourceValue = (RadGrid)ResourceValuesPlaceHolder.FindControl("ResourceValue");
057. 
058.            if (resourceValue.Items.Count == 0)
059.            {
060.                PopulateResources();
061.                // MarkSelectedResources(); - moved to ResourceValue_ItemDataBound
062.            }
063.        }
064.         
065.        /// Populates the resource options.       
066.        private void PopulateResources()
067.        {
068.            List<string> roomIDs = new List<string>();
069. 
070.            foreach (Resource res in GetResources(Type))
071.            {               
072.                roomIDs.Add(res.Key.ToString());
073.            }
074. 
075.            DAL_RoomActivity getRooms = new DAL_RoomActivity();
076.            List<Room> availableRooms = getRooms.GetRoomsByIDList(roomIDs);
077. 
078.            ResourceValue.DataSource = availableRooms;
079.            ResourceValue.DataBind();
080.        }
081. 
082.        /// <summary>
083.        /// Gets a list of the resources of the specified type.
084.        /// </summary>
085.        /// <param name="resType">Type of the resources to search for.</param>
086.        /// <returns>A list of the resources of the specified type.</returns>
087.        private IEnumerable<Resource> GetResources(string resType)
088.        {
089.            List<Resource> availableResources = new List<Resource>();
090.            IEnumerable<Resource> resources = Owner.Resources.GetResourcesByType(resType);
091. 
092.            foreach (Resource res in resources)
093.            {
094.                if (IncludeResource(res))
095.                {
096.                    availableResources.Add(res);
097.                }
098.            }
099.            return availableResources;
100.        }
101. 
102.        /// <summary>
103.        /// Returns a boolean value, indicating if a resource should be included in the list.
104.        /// You can use this method to define your custom filtering logic.
105.        /// </summary>
106.        /// <param name="res">The resource to filter.</param>
107.        /// <returns>A boolean value, indicating if a resource should be included in the list.</returns>
108.        private bool IncludeResource(Resource res)
109.        {
110.            return res.Available || ResourceIsInUse(res);
111.        }
112. 
113.        /// <summary>
114.        /// Returns a boolean value, indicating if a resource is already assigned to the appointment.
115.        /// </summary>
116.        /// <param name="res">The resource to check.</param>
117.        /// <returns>A boolean value, indicating if a resource is already assigned to the appointment.</returns>
118.        private bool ResourceIsInUse(Resource res)
119.        {
120.            foreach (Resource appRes in Appointment.Resources)
121.            {
122.                if (res == appRes)
123.                {
124.                    return true;
125.                }
126.            }
127. 
128.            return false;
129.        }
130. 
131.        /// <summary>
132.        /// Extracts the resource values from the RadGrid.
133.        /// </summary>
134.        /// <returns>
135.        /// Depending the number of selected resources it returns either
136.        /// a single key or an array of resource keys.
137.        /// </returns>
138.        private object ExtractResourceValues()
139.        {
140.            ArrayList resourceKeys = new ArrayList();
141. 
142.            foreach (GridItem item in ResourceValue.MasterTableView.Items)
143.            {
144.                if (item is GridDataItem)
145.                {
146.                    GridDataItem dataItem = (GridDataItem)item;
147.                    if (dataItem.Selected)
148.                    {
149.                        int dataItemKey = Convert.ToInt32(dataItem.GetDataKeyValue("RoomID"));//.ToString();
150.                        resourceKeys.Add(dataItemKey);
151.                    }
152.                }
153.            }
154. 
155.            switch (resourceKeys.Count)
156.            {
157.                case 0:
158.                    return string.Empty;
159. 
160.                case 1:
161.                    return resourceKeys[0];
162. 
163.                default:
164.                    return resourceKeys.ToArray();
165.            }
166.        }
167. 
168.        //replaces MarkSelectedResources - selects relevant rows in RadGrid
169.        protected void ResourceValue_ItemDataBound(object sender, GridItemEventArgs e)
170.        {
171.            if (e.Item is GridDataItem)
172.            {
173.                GridDataItem item = (GridDataItem)e.Item;
174.                foreach (Resource res in Appointment.Resources.GetResourcesByType(Type))
175.                {
176.                    string displayvalue = item.GetDataKeyValue("RoomID").ToString();
177.                    string displaykey = res.Key.ToString();
178.                    if (item.GetDataKeyValue("RoomID").ToString() == res.Key.ToString())
179.                    {
180.                        item.Selected = true;
181.                        TableCell cell = item["ClientSelectColumn"];
182.                        CheckBox checkBox = (CheckBox)cell.Controls[0];
183.                        checkBox.Checked = true;
184.                    }
185.                }
186.            }           
187.        }
188.    }
189.}

 

Serialisation not used because the grid is not added at run time

 

0
Peter Milchev
Telerik team
answered on 17 Mar 2020, 01:05 PM

Hello Ellie,

Thank you for sharing your solution with the community!

As a token of gratitude for providing your approach and implementation to the public, we have updated your Telerik points.

Regards,
Peter Milchev
Progress Telerik

Get quickly onboarded and successful with UI for ASP.NET AJAX with the Virtual Classroom technical trainings, available to all active customers. Learn More.
Tags
Scheduler
Asked by
Ellie
Top achievements
Rank 1
Answers by
Ellie
Top achievements
Rank 1
Peter Milchev
Telerik team
Share this question
or