Editors remove function doesn't remove it from database.

Editors remove function doesn't remove it from database.

valkarinvalkarin Posts: 3Questions: 1Answers: 0

Hi,

I'm using the editor with .net. Somehow I can add, edit but cannot remove. Editor removes the row from the table but not from the database. There is no error on both sides. Spend hours trying to figure out. Any help would be nice.
This is the client side.

<script type="text/javascript" src="https://cdn.datatables.net/v/bs-3.3.7/dt-1.10.13/b-1.2.4/r-2.1.0/se-1.2.0/datatables.min.js"></script>
    <script src="~/Scripts/editor/dataTables.editor.js" type="text/javascript"></script>
    <script src="~/Scripts/editor/editor.bootstrap.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        var editor; // use a global for the submit and return data rendering in the examples
        var table;

        // Activate an inline edit on click of a table cell
        $('#products').on('click',
            'tbody td.editable',
            function (e) {
                editor.inline(this,
                    {
                        onBlur: 'submit'
                    });
            });

        // Delete a record
        $('#products').on('click',
            'a.editor_remove',
            function (e) {
                e.preventDefault();
                editor.remove($(this).closest('tr'),
                    {
                        title: 'Delete record',
                        message: function (evt, dt) {
                            var rows = dt.rows(evt.modifier()).data().pluck('Name');
                            return 'Are you sure you want to delete the entries for the ' +
                                'following record(s)? <ul><li>' +
                                rows.join('</li><li>') +
                                '</li></ul>';
                        },
                        buttons: [{ label: 'No', fn: function () { this.close(); } }, 'Yes']
                    });
            });

        $(document).ready(function() {
            editor = new $.fn.dataTable.Editor({
                ajax: {
                    create: {
                        type: 'POST',
                        url: '@Url.Content("~/api/article/create/")'
                    },
                    edit: {
                        type: 'PUT',
                        url: '@Url.Content("~/api/article/edit/")'
                    },
                    remove: {
                        type: 'DELETE',
                        url: '@Url.Content("~/api/article/remove/")'
                    }
                },
                table: "#products",
                fields: [
                    {
                        label: "ID",
                        name: "Id",
                        type: "hidden"
                    },
                    {
                        label: "Name",
                        name: "Name"
                    },
                    {
                        label: "Created",
                        name: "CreationTimeStamp",
                        type: "hidden"
                    }
                ]
            });

            editor.disable('CreationTimeStamp');

            table = $('#products').DataTable({
                dom: "Bfrtip",
                paging: true,
                lengthChange: false,
                select: false,
                searching: true,
                info: true,
                autoWidth: false,
                responsive: true,
                language: {
                    "emptyTable": "Currently, there are no products in the database."
                },
                orderFixed: [2, 'desc'],
                ajax: '@Url.Content("~/api/article/get")',
                columns: [
                    {
                        data: "Id",
                        searchable: false,
                        orderable: false
                    },
                    { data: "Name", orderable: false, className: 'editable' },
                    {
                        data: "CreationTimeStamp",
                        visible: false,
                        searchable: false,
                        orderable: true
                    },
                    {
                        data: null,
                        defaultContent:
                            '<a href="" class="btn icon-btn btn-danger editor_remove"><span class="glyphicon btn-glyphicon glyphicon-trash img-circle text-danger"></span> Delete</a>',
                        searchable: false,
                        orderable: false
                    }
                ],
                buttons: []
            });

            $('#article-box').on({
                keypress: function(e) {
                    if (e.which == 13) {
                        addRow(e.target);
                        event.preventDefault();
                    }
                },
                keyup: function(e) {
                    if (e.keyCode == 27) {
                        $('#newArticleField').val('');
                        $('#newArticleField').focus();
                        event.preventDefault();
                    }
                }
            });
            $('#newArticleField').on({
                blur: function(e) {
                    addRow(e.target);
                }
            });
        });

        function addRow(element) {
            var textValue = $.trim($(element).val());
            if (textValue !== '') {
                editor
                    .create(false)
                    .set('Name', textValue).submit();

                //success
                $('#newArticleField').val('');
                $('#newArticleField').focus();
            }
        }
    </script>

And this is the server side

public class ArticleController : ApiController
    {
        public IHttpActionResult ArticleRest(HttpRequest request)
        {
            IHttpActionResult response;
            using (var context = new ApplicationDbContext())
            {
                using (var db = new Database("sqlserver", context.Database.Connection))
                {
                    var editor = new Editor(db, "Articles", "Id")
                        .Model<Article>()
                        .Field(new Field("Id").Set(false))
                        .Field(new Field("Name").Validator(Validation.NotEmpty()))
                        .Field(new Field("CreationTimeStamp")
                            .Set(Field.SetType.Create)
                            .SetValue(DateTime.Now))
                        .Process(request)
                        .Data();
                    var settings = new JsonSerializerSettings {DateFormatString = "MM/dd/yyyy HH:mm:ss"};

                    response = Json(editor, settings);
                }
            }
            return response;
        }

        [System.Web.Http.HttpGet]
        public IHttpActionResult GetArticles()
        {
            IHttpActionResult response = NotFound();
            using (var context = new ApplicationDbContext())
            {
                var result = new List<Select2Model>();
                context.Articles.ToList().ForEach(x =>
                {
                    result.Add(new Select2Model()
                    {
                        id = x.Id,
                        text = x.Name
                    });
                });
                response = Json(result);
            }
            return response;
        }

        [System.Web.Http.HttpGet]
        public IHttpActionResult Get()
        {
            var request = HttpContext.Current.Request;
            return ArticleRest(request);
        }

        [System.Web.Http.HttpPost]
        public IHttpActionResult Create()
        {
            var request = HttpContext.Current.Request;
            return ArticleRest(request);
        }

        [System.Web.Http.HttpPut]
        public IHttpActionResult Edit()
        {
            var request = HttpContext.Current.Request;
            return ArticleRest(request);
        }

        [System.Web.Http.HttpDelete]
        public IHttpActionResult Remove([FromBody] FormDataCollection formData)
        {
            var request = HttpContext.Current.Request;
            return ArticleRest(request);
        }

Here is the debugger code as well if needed: iboyoc.

Thank you for your help.

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    Could you enable Editor's debug mode and show me the JSON that is returned when you send the delete request?

    Thanks,
    Allan

  • valkarinvalkarin Posts: 3Questions: 1Answers: 0

    Hi Allan,

    Thank you for your quick response.

    {"draw":null,"data":[{"DT_RowId":"row_1","Id":1,"Name":"Asparagus","CreationTimeStamp":"01/30/2017 05:29:53"},{"DT_RowId":"row_2","Id":2,"Name":"Arugula","CreationTimeStamp":"01/30/2017 05:29:53"},{"DT_RowId":"row_3","Id":3,"Name":"Avocado","CreationTimeStamp":"01/30/2017 05:29:53"},{"DT_RowId":"row_4","Id":4,"Name":"Beats","CreationTimeStamp":"01/30/2017 05:29:53"},{"DT_RowId":"row_5","Id":5,"Name":"Basil","CreationTimeStamp":"01/30/2017 05:29:53"},{"DT_RowId":"row_6","Id":6,"Name":"Broccoli","CreationTimeStamp":"01/30/2017 05:29:53"},{"DT_RowId":"row_7","Id":7,"Name":"Cabbage Red","CreationTimeStamp":"01/30/2017 05:29:53"},{"DT_RowId":"row_8","Id":8,"Name":"Cabbage Savoy","CreationTimeStamp":"01/30/2017 05:29:53"},{"DT_RowId":"row_9","Id":9,"Name":"Carrots","CreationTimeStamp":"01/30/2017 05:29:53"},{"DT_RowId":"row_10","Id":10,"Name":"Cauliflower","CreationTimeStamp":"01/30/2017 05:29:53"},{"DT_RowId":"row_11","Id":11,"Name":"Celery","CreationTimeStamp":"01/30/2017 05:29:53"},{"DT_RowId":"row_12","Id":12,"Name":"Cilantro","CreationTimeStamp":"01/30/2017 05:29:53"},{"DT_RowId":"row_13","Id":13,"Name":"Cucumber","CreationTimeStamp":"01/30/2017 05:29:53"},{"DT_RowId":"row_14","Id":14,"Name":"H. cucumber","CreationTimeStamp":"01/30/2017 05:29:53"},{"DT_RowId":"row_15","Id":15,"Name":"Dill","CreationTimeStamp":"01/30/2017 05:29:53"},{"DT_RowId":"row_16","Id":16,"Name":"Eggplant","CreationTimeStamp":"01/30/2017 05:29:53"},{"DT_RowId":"row_17","Id":17,"Name":"Eggplant Holland","CreationTimeStamp":"01/30/2017 05:29:53"},{"DT_RowId":"row_18","Id":18,"Name":"Garlic","CreationTimeStamp":"01/30/2017 05:29:53"},{"DT_RowId":"row_19","Id":19,"Name":"Ginger","CreationTimeStamp":"01/30/2017 05:29:53"},{"DT_RowId":"row_20","Id":20,"Name":"Kale","CreationTimeStamp":"01/30/2017 05:29:53"},{"DT_RowId":"row_21","Id":21,"Name":"Kirby","CreationTimeStamp":"01/30/2017 05:29:53"},{"DT_RowId":"row_22","Id":22,"Name":"Lemon","CreationTimeStamp":"01/30/2017 05:29:53"},{"DT_RowId":"row_23","Id":23,"Name":"Leeks","CreationTimeStamp":"01/30/2017 05:29:53"},{"DT_RowId":"row_24","Id":24,"Name":"Iceberg","CreationTimeStamp":"01/30/2017 05:29:53"},{"DT_RowId":"row_25","Id":25,"Name":"Romaine","CreationTimeStamp":"01/30/2017 05:29:53"},{"DT_RowId":"row_26","Id":26,"Name":"Romaine Heart","CreationTimeStamp":"01/30/2017 05:29:53"},{"DT_RowId":"row_27","Id":27,"Name":"Lime-Small","CreationTimeStamp":"01/30/2017 05:29:53"},{"DT_RowId":"row_28","Id":28,"Name":"Lime-Large","CreationTimeStamp":"01/30/2017 05:29:53"},{"DT_RowId":"row_29","Id":29,"Name":"Mint","CreationTimeStamp":"01/30/2017 05:29:53"},{"DT_RowId":"row_30","Id":30,"Name":"Mushrooms","CreationTimeStamp":"01/30/2017 05:29:53"},{"DT_RowId":"row_31","Id":31,"Name":"Mushrooms Oyster","CreationTimeStamp":"01/30/2017 05:29:53"},{"DT_RowId":"row_32","Id":32,"Name":"Mushrooms Shitake","CreationTimeStamp":"01/30/2017 05:29:53"},{"DT_RowId":"row_33","Id":33,"Name":"Mushrooms Portobello","CreationTimeStamp":"01/30/2017 05:29:53"},{"DT_RowId":"row_34","Id":34,"Name":"Mix Salad","CreationTimeStamp":"01/30/2017 05:29:53"},{"DT_RowId":"row_35","Id":35,"Name":"Onion Spanish","CreationTimeStamp":"01/30/2017 05:29:53"},{"DT_RowId":"row_36","Id":36,"Name":"Onion Red","CreationTimeStamp":"01/30/2017 05:29:53"},{"DT_RowId":"row_37","Id":37,"Name":"Parsley","CreationTimeStamp":"01/30/2017 05:29:53"},{"DT_RowId":"row_38","Id":38,"Name":"Pepper Cubana","CreationTimeStamp":"01/30/2017 05:29:53"},{"DT_RowId":"row_39","Id":39,"Name":"Pepper Green","CreationTimeStamp":"01/30/2017 05:29:53"},{"DT_RowId":"row_40","Id":40,"Name":"Pepper Red","CreationTimeStamp":"01/30/2017 05:29:53"},{"DT_RowId":"row_41","Id":41,"Name":"Pepper Long Hot","CreationTimeStamp":"01/30/2017 05:29:53"},{"DT_RowId":"row_42","Id":42,"Name":"Pepper Jalapenos","CreationTimeStamp":"01/30/2017 05:29:53"},{"DT_RowId":"row_43","Id":43,"Name":"Potato Idaho #40","CreationTimeStamp":"01/30/2017 05:29:53"},{"DT_RowId":"row_44","Id":44,"Name":"Potato Idaho","CreationTimeStamp":"01/30/2017 05:29:53"},{"DT_RowId":"row_45","Id":45,"Name":"Potato Red B","CreationTimeStamp":"01/30/2017 05:29:53"},{"DT_RowId":"row_46","Id":46,"Name":"Potato 2","CreationTimeStamp":"01/30/2017 05:29:53"},{"DT_RowId":"row_47","Id":47,"Name":"Yam","CreationTimeStamp":"01/30/2017 05:29:53"},{"DT_RowId":"row_48","Id":48,"Name":"Yukon","CreationTimeStamp":"01/30/2017 05:29:53"},{"DT_RowId":"row_49","Id":49,"Name":"Spinach","CreationTimeStamp":"01/30/2017 05:29:53"},{"DT_RowId":"row_50","Id":50,"Name":"Spinach Clip","CreationTimeStamp":"01/30/2017 05:29:53"},{"DT_RowId":"row_51","Id":51,"Name":"Spinach Bunch","CreationTimeStamp":"01/30/2017 05:29:53"},{"DT_RowId":"row_52","Id":52,"Name":"Squash Green","CreationTimeStamp":"01/30/2017 05:29:53"},{"DT_RowId":"row_53","Id":53,"Name":"Squash Yellow","CreationTimeStamp":"01/30/2017 05:29:53"},{"DT_RowId":"row_54","Id":54,"Name":"Squash Batuna","CreationTimeStamp":"01/30/2017 05:29:53"},{"DT_RowId":"row_55","Id":55,"Name":"Skyline","CreationTimeStamp":"01/30/2017 05:29:53"},{"DT_RowId":"row_56","Id":56,"Name":"Tomato Plum","CreationTimeStamp":"01/30/2017 05:29:53"},{"DT_RowId":"row_57","Id":57,"Name":"Tomato Regular","CreationTimeStamp":"01/30/2017 05:29:53"},{"DT_RowId":"row_58","Id":58,"Name":"Tomato Beef","CreationTimeStamp":"01/30/2017 05:29:53"},{"DT_RowId":"row_59","Id":59,"Name":"Tomato Stem","CreationTimeStamp":"01/30/2017 05:29:53"},{"DT_RowId":"row_60","Id":60,"Name":"Tomato Grape","CreationTimeStamp":"01/30/2017 05:29:53"},{"DT_RowId":"row_61","Id":61,"Name":"Apple Red","CreationTimeStamp":"01/30/2017 05:29:53"},{"DT_RowId":"row_62","Id":62,"Name":"Apple Green","CreationTimeStamp":"01/30/2017 05:29:53"},{"DT_RowId":"row_63","Id":63,"Name":"Banana","CreationTimeStamp":"01/30/2017 05:29:53"},{"DT_RowId":"row_64","Id":64,"Name":"Blackberry","CreationTimeStamp":"01/30/2017 05:29:53"},{"DT_RowId":"row_65","Id":65,"Name":"Blueberry","CreationTimeStamp":"01/30/2017 05:29:53"},{"DT_RowId":"row_66","Id":66,"Name":"Raspbery","CreationTimeStamp":"01/30/2017 05:29:53"},{"DT_RowId":"row_67","Id":67,"Name":"Strawberry","CreationTimeStamp":"01/30/2017 05:29:53"},{"DT_RowId":"row_68","Id":68,"Name":"Grape Green","CreationTimeStamp":"01/30/2017 05:29:53"},{"DT_RowId":"row_69","Id":69,"Name":"Grape Red","CreationTimeStamp":"01/30/2017 05:29:53"},{"DT_RowId":"row_70","Id":70,"Name":"Grapefruit","CreationTimeStamp":"01/30/2017 05:29:53"},{"DT_RowId":"row_71","Id":71,"Name":"Grapefruit #23","CreationTimeStamp":"01/30/2017 05:29:53"},{"DT_RowId":"row_72","Id":72,"Name":"Grapefruit #32","CreationTimeStamp":"01/30/2017 05:29:53"},{"DT_RowId":"row_73","Id":73,"Name":"Coconut","CreationTimeStamp":"01/30/2017 05:29:53"},{"DT_RowId":"row_74","Id":74,"Name":"Kiwi","CreationTimeStamp":"01/30/2017 05:29:53"},{"DT_RowId":"row_75","Id":75,"Name":"Mango","CreationTimeStamp":"01/30/2017 05:29:53"},{"DT_RowId":"row_76","Id":76,"Name":"Melon","CreationTimeStamp":"01/30/2017 05:29:53"},{"DT_RowId":"row_77","Id":77,"Name":"Orange","CreationTimeStamp":"01/30/2017 05:29:53"},{"DT_RowId":"row_78","Id":78,"Name":"Orange #113","CreationTimeStamp":"01/30/2017 05:29:53"},{"DT_RowId":"row_79","Id":79,"Name":"Pineapple","CreationTimeStamp":"01/30/2017 05:29:53"},{"DT_RowId":"row_80","Id":80,"Name":"Pineapple #8","CreationTimeStamp":"01/30/2017 05:29:53"},{"DT_RowId":"row_81","Id":81,"Name":"Watermelon","CreationTimeStamp":"01/30/2017 05:29:53"},{"DT_RowId":"row_82","Id":82,"Name":"a","CreationTimeStamp":"01/30/2017 11:09:42"}],"recordsTotal":null,"recordsFiltered":null,"error":null,"fieldErrors":[],"id":null,"meta":{},"options":{},"files":{},"upload":{"id":null},"debugSql":[{"Query":"SELECT  [Id] as 'Id', [Name] as 'Name', [CreationTimeStamp] as 'CreationTimeStamp' FROM  [Articles] ","Bindings":[]}],"cancelled":[]}
    

    I just checked the request and seems like when the delete request comes to the api the HttpRequest.FormData is empty.

  • valkarinvalkarin Posts: 3Questions: 1Answers: 0

    Allan,

    I solved the problem. It happens because when the editor is sending the delete request it removes the data and adds the add to url. Since it is not requested with data parameter the api sees the requests formdata empty and doesn't read the data sent from the editor.
    When I took off this part from the dataTables.editor.js, it started to work

        if ( opts.type === 'DELETE' ) {
            var params = $.param( opts.data );
    
            opts.url += opts.url.indexOf('?') === -1 ?
                '?'+params :
                '&'+params;
    
            delete opts.data;
        }
    
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Answer ✓

    Ah! Yes. Thanks for digging into that. The reason Editor does that is that not all HTTP servers support data in the request body when sending a DELETE.

    I'm going to add a flag to the next version to make that optional though.

    Regards,
    Allan

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    Editor 1.6.2 which should be out later this week contains a new ajax.deleteBody option which can be set to false to disable this default action.

    Allan

This discussion has been closed.