Articles in this section
Category / Section

How to load & save resource unit for each resources of a task

2 mins read

In Gantt, it is possible to save resource unit of each resources for a task. Resource unit was mapped from data source by using ResourceUnitMapping property. The resources of each task are mapped with resource id alone or resource id with resource unit value.

When the resource unit value is not equals to 100% then the resource value was assigned as object with ResourceIdMapping and ResourceUnitMapping properties to the task.

In SQL table we can’t save resource collection as array object, so we must save this in string format. Hence we need to parse this value before load into Gantt control, similarly while saving the resource value in database, we must convert resource array collection in string format.

Please refer following code snippet.

@(Html.EJ().Gantt("Gantt").
.ResourceInfoMapping("ResourceID")
.ResourceNameMapping("ResourceName")
.ResourceIdMapping("ResourceId")
.ResourceUnitMapping("ResourceUnit")
.ShowResourceNames(true)
.ClientSideEvents(eve =>
{
eve.ActionComplete("actionComplete");            
})
)
// In this sample we had mapped resource collection in below structure in SQL table
// “1” – Single resource without unit value
// “1,2-50” – Resources without unit value and with unit value
// If resource id only given, unit value will be considered as 100

We have prepared a sample to save resource id and resource unit values of multiple resources as array of string in the “ActionComplete” client-side event by using “updateResourceID” method. On loading data from server-side it should be convert as resource collection by using “GetResourceValues” method.

function UpdateResourceID(resourceId) {
var res = [],
length = resourceId.length;
for (var i = 0; i < length; i++) {
if (typeof resourceId[i] === 'object') {
res.push(resourceId[i].ResourceId + "-" + resourceId[i].ResourceUnit);
}
else
res.push(resourceId[i].toString());
}
return res;
}
function actionComplete(args) {
if (args.requestType == "indent" || args.requestType == "outdent" || args.requestType == "recordUpdate" || (args.requestType === 'save' && args.modifiedRecord) || args.requestType == "drawConnectorLine") {
var ganttRec = [];
// Dialog Editing
if (args.requestType == "save")
data = args.modifiedRecord;
else if (args.requestType == "drawConnectorLine")
data = args.currentRecord;
else
data = args.data; // Cell Editing
if (!ej.isNullOrUndefined(data.ResourceID))
data.ResourceID = UpdateResourceID(data.ResourceID);
ganttRec.push(data);
if (args.updatedRecords && args.updatedRecords.length)
ganttRec = ganttRec.concat(args.updatedRecords);
updateModifiedGanttRecords(ganttRec);
}
//Newly Added Record is obtained here , which can be updated to database
else if (args.requestType == "save" && args.addedRecord) {
var data = args.addedRecord.item;
if (!ej.isNullOrUndefined(data.ResourceID))
data.ResourceID = UpdateResourceID(data.ResourceID);
$.ajax({
type: "POST",
url: '/Gantt/Add',
contentType: "application/json; charset=utf-8",
data: JSON.stringify(data),
dataType: "json",
});
// args.updatedRecords will have the array of dependent hierarchy of modified
//parents record when add new record as child of another record.
if (args.updatedRecords && args.updatedRecords.length)
updateModifiedGanttRecords(args.updatedRecords);
}
//To update the database on delete action
else if (args.requestType == "delete") {
var data = args.data.item;
if (!ej.isNullOrUndefined(data.ResourceID))
data.ResourceID = UpdateResourceID(data.ResourceID);
$.ajax({
type: "POST",
url: '/Gantt/Delete',
contentType: "application/json; charset=utf-8",
data: JSON.stringify(data),
dataType: "json",
});
if (args.data.hasChildRecords) {
deleteChildRecords(args.data);
}
if (args.updatedRecords && args.updatedRecords.length)
updateModifiedGanttRecords(args.updatedRecords);
}
}

 

C#

public List<object> GetResourceValues(string resourceArr)
{
List<object> Arr = new List<object>();
string[] resourceId = resourceArr.Split(',');
foreach (var id in resourceId)
{
string[] temp = id.Split('-');
if (temp.Length > 1)
{
TaskResource CurrentResource = new TaskResource();
CurrentResource.ResourceId = temp[0];
CurrentResource.ResourceUnit = Convert.ToDouble(temp[1]);
Arr.Add(CurrentResource);
}
else
{
Arr.Add(temp[0]);
}
}
return Arr; // Return the resource string value as list of objects at load time 
}

A simple sample to load and save resource unit value using remote data is available here

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied