editor does not fire postCreate event

editor does not fire postCreate event

mike92117mike92117 Posts: 38Questions: 11Answers: 1

The postCreate event does not seem to fire when a new row is created. The row is created but the view is not updated. I have a work around to make the grid update but I really need to hook into this event.

I verified that I can run the WebApi demos and the event does not fire there also so apparently, it isn't something I'm doing.

Can you help?

Thanks,
Mike #datatablesrocks

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 20,309Questions: 26Answers: 4,769

    Without know more info my guess is that the created row is not being returned as described here:
    https://editor.datatables.net/manual/server#Create

    I verified that I can run the WebApi demos and the event does not fire there also

    I'm not familiar with these demos. Are they on this site or somewhere else?

    Kevin

  • mike92117mike92117 Posts: 38Questions: 11Answers: 1

    these demos are included in the ZIP file download for registered users. There are different versions. I downloaded the .net web api ZIP.

  • allanallan Posts: 61,723Questions: 1Answers: 10,108 Site admin

    Could you let me know specifically which file it is that you are using so I can try it locally, or what code you are using? Also, to confirm is it the server-side .NET postCreate or the client-side postCreate that you are attempting to use?

    I've just tried the following on the server-side:

            public IHttpActionResult Staff()
            {
                var request = HttpContext.Current.Request;
                var settings = Properties.Settings.Default;
    
                using (var db = new Database(settings.DbType, settings.DbConnection))
                {
                    var editor = new Editor(db, "datatables_demo")
                        .Model<StaffModel>()
                        .Field(new Field("first_name")
                            .Validator(Validation.NotEmpty())
                        )
                        ....
                        );
    
                    editor.PostCreate += (sender, args) =>
                    {
                        var allan = 0;
                        allan++;
                    };
    
                    var response = editor
                        .Process(request)
                        .Data();
    
                    return Json(response);
                }
            }
    

    and it does appear to trigger correctly.

    Allan

  • mike92117mike92117 Posts: 38Questions: 11Answers: 1
    edited December 2017

    Hi Allan,

    I am trying to use the client side event. I can reproduce easily by editing the simple.html file and adding this logging code just after the editor is created. Note: I have logging code for preCreate and postCreate (and every other event) but omitted these here for brevity.

    editor.on('create', function (e, json, data) {
        console.log('>>> create >>>');
        console.log('<<< create <<<');
    });
    editor.on('submitComplete', function (e, json, data, action) {
        //ACTION IS create, edit or remove
        console.log('>>> submitComplete >>>');
        console.log('action: ' + action);
        console.log('<<< submitComplete <<<');
    });
    

    The create event never gets fired or any created related event (pre, post, etc.). I have a need for the post create event but the others don't fire as well. And new records only show up on a refresh unless I add code in the submitComplete to force it. Note update and delete work fine.

    Mike

  • allanallan Posts: 61,723Questions: 1Answers: 10,108 Site admin

    Hi Mike,

    It sounds like the server isn't returning the data for the new row when the create Ajax request is submitted. Can you show me the JSON that it is returning?

    Thanks,
    Allan

  • mike92117mike92117 Posts: 38Questions: 11Answers: 1
    edited December 2017

    It isn't returning data from the ajax call (looking at response.data). When I do get the records initially, I see the data in the table and of course it's in the response object. But not on create.

    I am using SQLite - don't know if that matters?

    My C# web api code:
    [Route("api/ShopifyAddresses")]
    [HttpGet]
    [HttpPost]
    public IHttpActionResult ShopifyAddresses()
    {
    var request = HttpContext.Current.Request;
    using (var db = new Database("sqlite", AppSettingsUtil.GetConnectionString("SQLITE")))
    {
    var response = new Editor(db, "ShopifyAddress", "ID")
    .Model<DTShopifyAddress>()
    .Field(new Field("ID"))
    .Field(new Field("CustomerID"))
    .Field(new Field("FirstName"))
    .Field(new Field("LastName"))
    .Field(new Field("Company"))
    .Field(new Field("Address1"))
    .Field(new Field("Address2"))
    .Field(new Field("City"))
    .Field(new Field("Province"))
    .Field(new Field("Zip"))
    .Field(new Field("CountryCode"))
    .Process(request)
    .Data();
    return Json(response);
    }
    }

  • allanallan Posts: 61,723Questions: 1Answers: 10,108 Site admin

    If it isn't returning the row that was created in the JSON, that explains why create isn't being triggered, nor is the row automatically showing. It sounds like the get id for the inserted row is failing. Could you show me your SQL schema for that table so I can try to reproduce it?

    Thanks,
    Allan

  • mike92117mike92117 Posts: 38Questions: 11Answers: 1

    Here you go:

    "cid","name","type","notnull","dflt_value","pk"
    "0","ID","INTEGER","0",NULL,"1"
    "1","CustomerID","INTEGER","0",NULL,"0"
    "2","FirstName","TEXT","0",NULL,"0"
    "3","LastName","TEXT","0",NULL,"0"
    "4","Company","TEXT","0",NULL,"0"
    "5","Address1","TEXT","0",NULL,"0"
    "6","Address2","TEXT","0",NULL,"0"
    "7","City","TEXT","0",NULL,"0"
    "8","Province","TEXT","0",NULL,"0"
    "9","Zip","TEXT","0",NULL,"0"
    "10","CountryCode","TEXT","0",NULL,"0"

  • allanallan Posts: 61,723Questions: 1Answers: 10,108 Site admin

    Thanks - I'll try it out and get back to you. It might be after Christmas though I'm afraid.

    Allan

  • mike92117mike92117 Posts: 38Questions: 11Answers: 1

    Enjoy your Christmas Allan. And thanks for creating a great product.

  • mike92117mike92117 Posts: 38Questions: 11Answers: 1

    Allan,

    Do you have any update on this issue with SQLite?

    Thanks,
    Mike

  • allanallan Posts: 61,723Questions: 1Answers: 10,108 Site admin

    Sorry - I've been bogged down with other things - it is on my list though! I'll post back here when done.

    Allan

  • gohokisgohokis Posts: 1Questions: 0Answers: 1
    edited June 2018 Answer ✓

    I had a problem like this where the record was created, but it wouldn't show up in the table until a page refresh and preCreate() and postCreate() were not firing. I was able to resolve this by using the full column name for the primary key field in the Editor setup versus the shortened name that was specified in the back-end.

    Back-end:

    Field::inst( 'units_id', 'id')

    Front-end:

    }, {
    label: "Units ID:",
    name: "units_id", // have to use the full name instead of just the shortened name, "id", as I had used for other fields
    type: "hidden",
    }, {

This discussion has been closed.