igniteui-angular
igniteui-angular copied to clipboard
During row add, the dummy PK cannot be changed to a user-specified one
Description
The most obvious way to support changing the dummy PK is by handling the rowEditStart event (if you don't want for the end-user to see the dummy negative numbers). However, changing the rowData from the event args like this throws an error.
- igniteui-angular version: 12.3.x +
- browser: n/a
Steps to reproduce
- Open the sample
- Start adding a row
- Observe the console
Expected result
The sample should either work or there should be a different mechanism to achieve this such as the generatePK event in jQuery.
Attachments
@ChronosSF
I don't think we should allow changing the PK on rowEditStart or any other editing event. Changing the PK while editing is generally not advisable (could break functionalities that depend on ids like the transactions etc.) so it doesn't make sense to allow it via event args while editing either.
You can change the PK for the adding record in the rowAdd
event, which triggers just before the row is added.
In most real world scenarios the primary key column is not visible, nor editable, since it does not carry any meaningful information for the end-user. So you wouldn't need to change it before it actually is about to be added to the data in most cases.
Let me know if using the rowAdd
event is enough for this use case. If not we could discuss further exposing some other event specifically for adding that exposes PK and/or the whole new row data object before starting edit on it.
@ChronosSF, I agree with @MayaKirova and I have tested this with angular project and Entity Framework using SQLite.
In angular you should have rowAdd
event handler
public rowAdd(event: IGridEditEventArgs) {
console.log(event);
this.cityService.addCity(event.newValue).subscribe(res => {
if (res) {
event.newValue.cityID = res.cityID;
}
}, err=> console.log(err));
}
This will make a request to the controller so that the row can be added to the database and return the newly created object with correct ID (Note that firstly the id is a negative number). In the controller we can do something like this:
[HttpPost("add")]
public ActionResult AddCity([FromBody]City City)
{
var tmp = new City
{
CityName = City.CityName,
Description = City.Description,
HolidayDate = City.HolidayDate,
TrainStation = City.TrainStation,
Population = City.Population
};
var addedCity = db.City.Add(tmp);
try
{
db.SaveChanges();
}
catch (Exception e)
{
return BadRequest(e.Message);
}
return Ok(addedCity.Entity);
}
This way just before the row is added we change the rowID with the correct one which comes from the database.
There has been no recent activity and this issue has been marked inactive.
There has been no recent activity and this issue has been marked inactive.
There has been no recent activity and this issue has been marked inactive.
There has been no recent activity and this issue has been marked inactive.
There has been no recent activity and this issue has been marked inactive.
There has been no recent activity and this issue has been marked inactive.