Custom Field in Segments

A question about this code. If I do note want "Segment N", but "Segment SegmentName" as segment labels, how should I modify this line? Is it I can shall write a query myself to get a segment's name based on SegmentIndex and TaskId?

 string textContent = "Segment " + (segment.SegmentIndex + 1);

I got SegmentName by adding the following line to the demo code's SegmentModel class, and then of course adding segment names data to GetSegmentCollection():

public string SegmentName {get; set;}

If there is no built-in methods to get the names, I would rather hope you can guide me a bit to write long custom codes to temporary solve the problem. This time I cannot wait for the feature voting process, though I can wait for a quickly scheduled release that includes the above custom segment column feature.


8 Replies 1 reply marked as answer

DE desmond August 1, 2024 01:20 PM UTC

Is there any updates?



AG Ajithkumar Gopalakrishnan Syncfusion Team August 1, 2024 02:26 PM UTC

Hi Desmond,

Greetings from Syncfusion Support,

Regarding your query, we need to clarify the behavior of the Gantt chart. When a split or merge action is performed on a segment, the existing data for that segment is removed and replaced with new data for the affected segments. For example, if Segment 2 is split or merged, the data for Segment 2 is removed, and new tasks for Segment 2 and Segment 3 are added in its place. As a result, we can’t maintain the external property of the segment name value, and that segment becomes empty.

Due to this behavior, it is not feasible to treat the segment name value as a feature in this context. Therefore, the current approach to managing split and merge actions will not retain the segment name values in the updated collection.

For scenarios where split or merge actions are not performed, you can use the following code snippet and sample to maintain the segment names:

public class SegmentModel

{

   

   public string SegmentName { get; set; }

}

<SfGantt @ref="gantt">

   

   <GanttTemplates TValue="TaskData">

        <TaskbarTemplate>

            @{

                var task = (context as TaskData);

               

               @if (segments != null && segments.Count() > 1)

                {

                    foreach (var segment in segments)

                    {

                        List<SegmentModel> sortedSegments = this.segmentCollection.Where(segment => segment.TaskId == task.TaskId).OrderBy(segment => segment.SegmentStartDate).ToList();

                        string textLabel = sortedSegments[segment.SegmentIndex].SegmentName;

                       

                   }

                }

                else

                {

                   

               }

            }

        </TaskbarTemplate>

    </GanttTemplates>

</SfGantt>


Modified sample link: https://www.syncfusion.com/downloads/support/directtrac/general/ze/SplitTask300972300.zip

If you have any further questions or need additional assistance, please let me know!

Regards,
Ajithkumar G



DE desmond August 2, 2024 02:11 PM UTC

Thanks a lot for your great answer.



AG Ajithkumar Gopalakrishnan Syncfusion Team August 2, 2024 02:32 PM UTC

Hi Desmond,

 

You are welcome.

 

We are glad that our solution worked for you.

 

Please contact us if you require any further assistance.

 

Regards,

Ajithkumar G
 



DE desmond February 21, 2025 04:21 PM UTC

Basically the same code you gave me, only difference is the label column of this.segmentCollection is being used as segment text labels. The problem is: after I added or deleted rows in this.segmentCollection through sfgrid, I can see the same number of segments, but labels are missing or empty if number of segments is not enough or too much. If I only updated or changed a segment label, the gantt chart can automatically change that segment's label.

After some debugging, it is obvious that List<TaskData> segments = taskModel.Segments; does not update. So segments is storing the old segments. Below is the code:


                var task = (context as TaskData);

                if (task == null)

                {

                    return;

                }

                var taskModel = gantt.GetRowTaskModel(task);

                List<GanttSegmentData> segments = taskModel.Segments;

                @if (segments != null && segments.Count() > 1)

                {

                    foreach (var segment in segments)

                    {

                        List<TaskData> sortedSegments = this.segmentCollection.Where(segment => segment.taskid == task.taskid).OrderBy(segment => segment.segmentstartdate).ToList();

                        string textLabel = (segment.SegmentIndex > sortedSegments.Count - 1) ? "" : " " + sortedSegments[segment.SegmentIndex].segmentlabel;

                        <div class="e-gantt-child-taskbar-inner-div e-gantt-child-taskbar e-segmented-taskbar" style=@("background-color: #00ff00 !important;height:24px;position: absolute;left:" + segment.Left + "px; width:" + segment.Width + "px;") tabindex=-1 data-segment-index="@(segment.SegmentIndex)">

                            <div class="e-taskbar-left-resizer e-icon" style="margin-top: 5px; left:2px">

                            </div>

                            <div class="e-gantt-child-progressbar-inner-div e-gantt-child-progressbar" style="height:24px;width:@(segment.ProgressWidth + "px");border-radius: 0px;text-align: left;">

                                <div style=@("height:22px;position: absolute;line-height:21px;font-size: 11px;color: #fff;text-overflow:ellipsis;overflow-x:hidden;")>

                                    <span>@textLabel</span>

                                </div>

                            </div>

                            <div class="e-taskbar-right-resizer e-icon" style="margin-top: 5px;left:@((segment.Width) - 15)px">

                            </div>


                        </div>

                    }

                }

                else

                {

                    string textLabel = " " + task.tasklabel;

                    <div class="e-gantt-child-taskbar e-gantt-child-taskbar-inner-div" style="background-color: #00ff00 !important");height:24px;" tabindex=-1>

                        <div class="e-gantt-child-progressbar-inner-div e-gantt-child-progressbar" style="height:24px;width:@(taskModel.ProgressWidth + "px");text-align: left;border-radius: 0px;">

                            <div style=@("height:22px;position: absolute;line-height:21px;font-size: 11px;color: #fff;text-overflow:ellipsis;overflow-x:hidden;")>

                                <span>@textLabel</span>

                            </div>

                        </div>

                    </div>

                }

            }



...



    public void DialogActionBegin(Syncfusion.Blazor.Grids.ActionEventArgs<SegmentData> Args)

    {

        if (Args.RequestType.Equals(Syncfusion.Blazor.Grids.Action.Save))

        {

            if (Args.Action == "Add")

            {

                //add

            }

            else

            {

                //update

            }

        }

        if (Args.RequestType.Equals(Syncfusion.Blazor.Grids.Action.Delete))

        {

            //delete

        }

    }

    public async Task DialogActionComplete(Syncfusion.Blazor.Grids.ActionEventArgs<SegmentData> Args)

    {

        if (Args.RequestType.Equals(Syncfusion.Blazor.Grids.Action.Save)||Args.RequestType.Equals(Syncfusion.Blazor.Grids.Action.Delete))

        {

            //update this.segmentCollection

        }

        await GanttRef.RefreshAsync();

    }



AG Ajithkumar Gopalakrishnan Syncfusion Team February 24, 2025 08:09 AM UTC

Hi Desmond,

We appreciate your patience.

We can replicate the problem our end . We have logged this reported issue, ‘SegmentCollection is not updating when adding or deleting a segment’ as a bug. We are working on a fix for this issue and plan to include it in our upcoming Nuget release which is scheduled to be rolled out on Mar 18, 2025. You can track its status from the feedback link given below. 

You can track the progress of the resolution by visiting the feedback link provided below:


Feedback link: https://www.syncfusion.com/feedback/65705/segmentcollection-is-not-updating-when-adding-or-deleting-a-segment

Disclaimer: Inclusion of this solution in the main release may change due to other factors including but not limited to QA checks and works reprioritization. 

Regards,
Ajithkumar G


Marked as answer

DE desmond March 8, 2025 04:01 AM UTC

Thanks for your help. Good news is that I solved the problem by myself already, may share you my findings if you want.



AG Ajithkumar Gopalakrishnan Syncfusion Team March 10, 2025 10:33 AM UTC

Hi Desmond,

Thanks for the update! Glad to hear that you were able to solve the problem. We’d love to hear about your findings—please feel free to share them, as they could be valuable for us. Also, once we fix the issue on our end and release the update, we will inform you.

Regards,
Ajithkumar G


Loader.
Up arrow icon