igniteui-angular icon indicating copy to clipboard operation
igniteui-angular copied to clipboard

During row add, the dummy PK cannot be changed to a user-specified one

Open ChronosSF opened this issue 2 years ago • 2 comments

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

  1. Open the sample
  2. Start adding a row
  3. 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

stoxrz--run.zip

ChronosSF avatar Sep 16 '22 12:09 ChronosSF

@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.

MayaKirova avatar Sep 20 '22 12:09 MayaKirova

@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.

mddragnev avatar Sep 21 '22 08:09 mddragnev

There has been no recent activity and this issue has been marked inactive.

github-actions[bot] avatar Nov 21 '22 00:11 github-actions[bot]

There has been no recent activity and this issue has been marked inactive.

github-actions[bot] avatar Jan 21 '23 00:01 github-actions[bot]

There has been no recent activity and this issue has been marked inactive.

github-actions[bot] avatar Mar 25 '23 00:03 github-actions[bot]

There has been no recent activity and this issue has been marked inactive.

github-actions[bot] avatar May 29 '23 00:05 github-actions[bot]

There has been no recent activity and this issue has been marked inactive.

github-actions[bot] avatar Aug 06 '23 00:08 github-actions[bot]

There has been no recent activity and this issue has been marked inactive.

github-actions[bot] avatar Oct 09 '23 00:10 github-actions[bot]