Delete from Datagrid with composite key

I am using ASP.NET Core and have a table in an ejs-grid with a composite key of two columns.  I saw this example of how to 'push' the additional key via the 'headers', but it is not working for me, perhaps because the example is with ej-grid not ejs-grid.  I need the additional key to perform deletes in my controller.  The function below gives the error: 

TypeError: Cannot read property 'dataSource' of undefined

    function actionBegin(args) {
        if (args.requestType == "delete") {
            args.model.dataSource.dataSource.headers = [];
            args.model.dataSource.dataSource.headers.push({ "DomainCode": args.data.DomainCode });
        }
    }

I commented out the push statement to confirm it was the first statement that was failing.

Any help would be appreciated.

3 Replies 1 reply marked as answer

HK Harvey Kravis November 20, 2020 09:48 PM UTC

Related to this problem I also need to populate my composite key during an add.  I see examples for a single key, and for different platforms but not ejs.


AG Ajith Govarthan Syncfusion Team November 23, 2020 12:12 PM UTC

Hi Harvey, 
 
Thanks for contacting Syncfusion support. 
 
Query:  I need the additional key to perform deletes in my controller and also need to populate my composite key during an add.  I see examples for a single key, and for different platforms but not ejs 
 
Based on your query you want to send additional params while perform the add and delete operation in the grid component. By default in EJ2 grid we do not have support to composite keys and we will perform all the CRUD actions with primaryKey data only. To achieve your requirement we suggest you to use addParams property. 
 
Using the addParams property you can send the additional params to the server side. We have sent the addition params for add and delete operations. At the server side you can get them and make your own composite keys to perform the actions. For your convenience we have attached the sample so please refer the sample for your reference. 
 
Code Example: 
Index.cshtml 
 
    function actionBegin(args) { 
        if (args.requestType == "save" && args.action == "add") { 
            var grid = document.getElementsByClassName('e-grid')[0].ej2_instances[0]; 
            grid.query = new ej.data.Query().addParams('insertKey', "12"); // send your own keys 
        } 
 
        if (args.requestType == "delete") { 
            var grid = document.getElementsByClassName('e-grid')[0].ej2_instances[0]; 
            grid.query = new ej.data.Query().addParams('deleteKey', "13"); // send your own keys 
        } 
    } 
 
 
 
 
 
 
 
 
 
 
Please get back to us if you need further assistance. 
 
Regards, 
Ajith G. 
 


Marked as answer

HK Harvey Kravis November 23, 2020 08:57 PM UTC

See the end of this to see a resolution I found.

This is a step in the right direction but does not get me all of the way there.  In the example you are hard-coding the parameter that is being passed.  In my case I am deleting a row from a child grid that has a composite key with 2 columns.  Guess I should have pointed that out earlier.  For a delete I need a way to reference those two columns from the child grid and pass them as parameters.  I will have composite keys with 3 or 4 columns in other situations, but if we can solve the 2-column key I can extrapolate to more columns obviously.  So it seems that this line needs to be modified to reference the child grid, but I have no clue:

            var grid = document.getElementsByClassName('e-grid')[0].ej2_instances[0];

And this line needs to be modified to reference a column name DomainCode in the child grid:

            grid.query = new ej.data.Query().addParams('DomainCode', );

For now I am handling adds by defaulting the parent key value using this technique:

    function actionBegin(args) {
        if (args.requestType === "add") {
            args.data.DomainType = this.parentDetails.parentKeyFieldValue;
        }
    }

This is great if my composite key is only two columns.  How can I reference the columns I need in the selected row of the parent?

Also, it works because my parent does not have an actionBegin.  If both have an actionBegin how can I differentiate the two actionBegins?

You provided a link to documentation for data binding.  It has this example, but I'm not sure whether it is useful in this case or not: 

query="new ej.data.Query().addParams('ej2grid', 'true')"

I found a sample with a syntax for adding parameters that works with hard-coded values, but not with my args.data...

        if (args.requestType == "delete") {
            this.query.params = [];
            this.query.addParams('DomainType', args.data.DomainType); 
            this.query.addParams('DomainCode', args.data.DomainCode); 
        }

Apparently it does not like the reference to args.data.DomainType and DomainCode in this context.  This syntax is much cleaner.  Just need to be able to reference the columns in the row being deleted.

Thanks so much for the quick response.

I was able to resolve this by declaring variables, populating them on rowselected, and then referencing them from there.


Also need rowSelected in the child grid definition:

    var ChildGrid = new Syncfusion.EJ2.Grids.Grid()
    {
        DataSource = new Syncfusion.EJ2.DataManager() { Url = "DomainsGeneric/DomainsGeneric", Adaptor = "UrlAdaptor", InsertUrl = "/DomainsGeneric/Insert", UpdateUrl = "/DomainsGeneric/Update", RemoveUrl = "/DomainsGeneric/Delete" },
        QueryString = "DomainType",
        EditSettings = new Syncfusion.EJ2.Grids.GridEditSettings() { AllowAdding = true, AllowEditing = true, AllowDeleting = true },
        Toolbar = new List() { "Add", "Edit", "Delete", "Update", "Cancel" },
        DataBound = "dataBound",
        ActionFailure = "actionFailure",
        ActionBegin = "actionBegin",
        AllowSelection = true,
        AllowResizing = true,
        AllowSorting = true,
        RowSelected = "rowSelected",
        Columns = new List
{
            new Syncfusion.EJ2.Grids.GridColumn() {Field="DomainType", HeaderText="Domain Type", IsPrimaryKey=true },
            new Syncfusion.EJ2.Grids.GridColumn() {Field="DomainCode", HeaderText="Domain Code", IsPrimaryKey=true },
...

And in the controller:

        public async Task Delete([FromBody] CRUDModel param)
        {
            DomainsGeneric value = _context.DomainsGeneric.Where(e => e.DomainType == Int32.Parse(param.Params["DomainType"].ToString()) && e.DomainCode == param.Params["DomainCode"].ToString()).FirstOrDefault();
            _context.Remove(value);
            await _context.SaveChangesAsync();
            return Json(value);
        }


Loader.
Up arrow icon