Soft Edit

Soft Edit

dpanscikdpanscik Posts: 202Questions: 47Answers: 0

There are a couple hints on this forum indicating there is a easy way to enable a soft edit. I need more conclusive information than the hints I found elsewhere on the forum.

Essentially what I want to do is mark a existing row as bool named "OldVersion", and otherwise leave it unchanged and create a new row with the edited information.

This would give me a very nice history of edits.

Here is my database insert part of my controller.

            using (var db = new Database(settings.DbType, settings.DbConnection))
            {

                var response = new Editor(db, "GR_AlternativeBilling", "TableID")
                    .Model<GR_AlternativeBilling>()
                    .Where("OldVersion", false, "=")
                    .TryCatch(false)
                    .Process(requestCreate)
                    .Data();

                return Json(response, JsonRequestBehavior.AllowGet);
            }

This question has an accepted answers - jump to answer

Answers

  • rf1234rf1234 Posts: 3,004Questions: 87Answers: 421
    edited July 28 Answer ✓

    Essentially what I want to do is mark a existing row as bool named "OldVersion", and otherwise leave it unchanged and create a new row with the edited information.

    Here is a button that allows you to create a new record based on an existing record:

    //custom button for edit / create = Allan's "duplicate" button = ctrManagement table
    $.fn.dataTable.ext.buttons.duplicate = {
        extend: "selected",
        text: "Copy & New",
        action: function ( e, dt, node, config ) {
            editor
                .title($.fn.dataTable.Editor.defaults.i18n.create.title)
                .buttons({
                        label: $.fn.dataTable.Editor.defaults.i18n.create.submit,
                        className: 'btn-showall-color',
                        fn: function () {
                            this.submit();
                        }
                    })
                .edit( dt.rows( {selected: true} ).indexes() )
                .mode( 'create' );
            var table = dt;
            editor
                .one('close', function(){ //important to do it only once! because code is executed on every button click!!
                    table.rows().deselect(); //otherwise the source record remains selected
                });
        },
        className: "duplicateButton"
    };
    

    This is how you would use the button in the "buttons" section of your data table:

    {   extend: "duplicate", editor: yourEditor  },
    

    And this is what you need to do to send a variable called "selected_id" to the server as a POST variable.

    var editor = new $.fn.dataTable.Editor( {
       ajax: {
            url: 'yourDataTableURL',
            type: 'POST',
            data: function ( d ) {
                var selected = table.row( {selected: true} ); 
                if (selected.any()) {
                    // send id of the copied record
                    d.selected_id = selected.data().id;
                }
            }
        },
    

    With that posted value you can update the copied record on "writeCreate" of the new record, for example.
    https://editor.datatables.net/manual/net/events

    Edit: Since I do the "deselect" of the copied record on "close" of the editor, you might need to save the existing record's id in a global variable on "select" of the existing record, so that you can send it to the server. You will figure that out I guess :smile:

  • rf1234rf1234 Posts: 3,004Questions: 87Answers: 421
    edited July 28

    You could do the update of "oldVersion" in one go using the client side Editor on "submitSuccess" like this. No need to do additonal server side updates in that case. No need for "ajax.data" in that case either.

    $.fn.dataTable.ext.buttons.duplicate = {
        extend: "selected",
        text: "Copy & New",
        className: "duplicateButton",
        action: function ( e, dt, node, config ) {
            var selRowsInd = dt.rows( {selected: true} ).indexes();
            editor
                .title($.fn.dataTable.Editor.defaults.i18n.create.title)
                .buttons({
                        label: $.fn.dataTable.Editor.defaults.i18n.create.submit,
                        fn: function () {
                            this.submit();
                        }
                    })
                .edit( selRowsInd )
                .mode( 'create' );
             editor
                //just once every time the button is clicked, important!
                .one( 'submitSuccess', function ( e, json, data, action ) { 
                   if ( action === 'create' ) {
                      //update the selected record to be "oldVersion"
                       editor
                           .edit( selRowsInd, false )
                           .set( 'oldVersion', 1 )
                           .submit();
                       dt.rows().deselect(); //otherwise the source record remains selected
                    }
                });
        }
    };
    
    
  • dpanscikdpanscik Posts: 202Questions: 47Answers: 0

    Thanks RF.

    I'm not strong with javascript so instead of struggling on the javascript side I decided to take your idea and do all the trickery serverside in C#. I made a deep copy of the form and edited the original form to only update "oldVersion" with a edit db function and used the new fresh copy of the form to do a create db function.

Sign In or Register to comment.