Skip to content

Commit b7d6aa7

Browse files
chore(grid): improve updating logic to avoid copying items
1 parent 4872743 commit b7d6aa7

File tree

5 files changed

+266
-268
lines changed

5 files changed

+266
-268
lines changed

components/grid/editing/incell.md

Lines changed: 76 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -27,105 +27,106 @@ To enable InCell editing mode, set the `EditMode` property of the grid to `incel
2727
<strong>Click a cell, edit it and click outside of the cell to see the change. Editing is prevented for the first two items.</strong>
2828
2929
<TelerikGrid Data=@MyData EditMode="incell" Pageable="true" Height="500px">
30-
<TelerikGridEvents>
31-
<EventsManager OnUpdate="@UpdateHandler" OnEdit="@EditHandler" OnDelete="@DeleteHandler" OnCreate="@CreateHandler"></EventsManager>
32-
</TelerikGridEvents>
33-
<TelerikGridToolBar>
34-
<TelerikGridCommandButton Command="Add" Icon="add">Add Employee</TelerikGridCommandButton>
35-
</TelerikGridToolBar>
36-
<TelerikGridColumns>
37-
<TelerikGridColumn Field=@nameof(SampleData.ID) Title="ID" Editable="false" />
38-
<TelerikGridColumn Field=@nameof(SampleData.Name) Title="Name" />
39-
<TelerikGridCommandColumn>
40-
<TelerikGridCommandButton Command="Save" Icon="save" ShowInEdit="true">Update</TelerikGridCommandButton>
41-
<TelerikGridCommandButton Command="Delete" Icon="delete">Delete</TelerikGridCommandButton>
42-
</TelerikGridCommandColumn>
43-
</TelerikGridColumns>
30+
<TelerikGridEvents>
31+
<EventsManager OnUpdate="@UpdateHandler" OnEdit="@EditHandler" OnDelete="@DeleteHandler" OnCreate="@CreateHandler"></EventsManager>
32+
</TelerikGridEvents>
33+
<TelerikGridToolBar>
34+
<TelerikGridCommandButton Command="Add" Icon="add">Add Employee</TelerikGridCommandButton>
35+
</TelerikGridToolBar>
36+
<TelerikGridColumns>
37+
<TelerikGridColumn Field=@nameof(SampleData.ID) Title="ID" Editable="false" />
38+
<TelerikGridColumn Field=@nameof(SampleData.Name) Title="Name" />
39+
<TelerikGridCommandColumn>
40+
<TelerikGridCommandButton Command="Save" Icon="save" ShowInEdit="true">Update</TelerikGridCommandButton>
41+
<TelerikGridCommandButton Command="Delete" Icon="delete">Delete</TelerikGridCommandButton>
42+
</TelerikGridCommandColumn>
43+
</TelerikGridColumns>
4444
</TelerikGrid>
4545
4646
@code {
47-
public void EditHandler(GridCommandEventArgs args)
48-
{
49-
SampleData item = (SampleData)args.Item;
47+
public void EditHandler(GridCommandEventArgs args)
48+
{
49+
SampleData item = (SampleData)args.Item;
5050
51-
//prevent opening for edit based on condition
52-
if (item.ID < 3)
53-
{
54-
args.IsCancelled = true;//the general approach for cancelling an event
55-
}
51+
//prevent opening for edit based on condition
52+
if (item.ID < 3)
53+
{
54+
args.IsCancelled = true;//the general approach for cancelling an event
55+
}
5656
57-
Console.WriteLine("Edit event is fired for column " + args.Field);
58-
}
57+
Console.WriteLine("Edit event is fired for column " + args.Field);
58+
}
5959
60-
public void UpdateHandler(GridCommandEventArgs args)
61-
{
62-
string fieldName = args.Field;
63-
object newVal = args.Value; //you can cast this, if necessary, according to your model
60+
public void UpdateHandler(GridCommandEventArgs args)
61+
{
62+
string fieldName = args.Field;
63+
object newVal = args.Value; //you can cast this, if necessary, according to your model
6464
65-
SampleData item = (SampleData)args.Item;//you can also use the entire model
65+
SampleData item = (SampleData)args.Item;//you can also use the entire model
6666
67-
//perform actual data source operation here
67+
//perform actual data source operation here
6868
69-
//if you have a context added through an @inject statement, you could call its SaveChanges() method
70-
//myContext.SaveChanges();
69+
//if you have a context added through an @inject statement, you could call its SaveChanges() method
70+
//myContext.SaveChanges();
7171
72-
var matchingItem = MyData.FirstOrDefault(c => c.ID == item.ID);
73-
if (matchingItem != null)
74-
{
75-
matchingItem.Name = item.Name;
76-
}
72+
var index = MyData.FindIndex(i => i.ID == item.ID);
73+
if (index != -1)
74+
{
75+
MyData[index] = item;
76+
// this copies the entire item, consider altering only the needed field
77+
}
7778
78-
Console.WriteLine("Update event is fired for " + args.Field + " with value " + args.Value);
79-
}
79+
Console.WriteLine("Update event is fired for " + args.Field + " with value " + args.Value);
80+
}
8081
81-
public void CreateHandler(GridCommandEventArgs args)
82-
{
83-
SampleData item = (SampleData)args.Item;
82+
public void CreateHandler(GridCommandEventArgs args)
83+
{
84+
SampleData item = (SampleData)args.Item;
8485
85-
//perform actual data source operation here
86+
//perform actual data source operation here
8687
87-
item.ID = MyData.Count;
88-
MyData.Add(item);
88+
item.ID = MyData.Count + 1;
89+
MyData.Insert(0, item);
8990
90-
Console.WriteLine("Create event is fired.");
91-
}
91+
Console.WriteLine("Create event is fired.");
92+
}
9293
93-
public void DeleteHandler(GridCommandEventArgs args)
94-
{
95-
SampleData item = (SampleData)args.Item;
94+
public void DeleteHandler(GridCommandEventArgs args)
95+
{
96+
SampleData item = (SampleData)args.Item;
9697
97-
//perform actual data source operation here
98+
//perform actual data source operation here
9899
99-
//if you have a context added through an @inject statement, you could call its SaveChanges() method
100-
//myContext.SaveChanges();
100+
//if you have a context added through an @inject statement, you could call its SaveChanges() method
101+
//myContext.SaveChanges();
101102
102-
MyData.Remove(item);
103+
MyData.Remove(item);
103104
104-
Console.WriteLine("Delete event is fired.");
105-
}
105+
Console.WriteLine("Delete event is fired.");
106+
}
106107
107-
//in a real case, keep the models in dedicated locations, this is just an easy to copy and see example
108-
public class SampleData
109-
{
110-
public int ID { get; set; }
111-
public string Name { get; set; }
112-
}
108+
//in a real case, keep the models in dedicated locations, this is just an easy to copy and see example
109+
public class SampleData
110+
{
111+
public int ID { get; set; }
112+
public string Name { get; set; }
113+
}
113114
114-
public List<SampleData> MyData { get; set; }
115+
public List<SampleData> MyData { get; set; }
115116
116-
protected override void OnInitialized()
117-
{
118-
MyData = new List<SampleData>();
117+
protected override void OnInitialized()
118+
{
119+
MyData = new List<SampleData>();
119120
120-
for (int i = 0; i < 50; i++)
121-
{
122-
MyData.Add(new SampleData()
123-
{
124-
ID = i,
125-
Name = "Name " + i.ToString()
126-
});
127-
}
128-
}
121+
for (int i = 0; i < 50; i++)
122+
{
123+
MyData.Add(new SampleData()
124+
{
125+
ID = i,
126+
Name = "Name " + i.ToString()
127+
});
128+
}
129+
}
129130
}
130131
````
131132

0 commit comments

Comments
 (0)