question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Prevent Item Inserting after sent to ajax insertItem

See original GitHub issue

Hi, I read issues #95 and #54 and i still don’t know how to put things work.

Here is my scenario: I use in-line grid insertion and I implement onItemInserting to validate some blank fields. Then, i send data to my service via ajax. In service, i do some validations in database, if true i insert ok, else i have to cancel insertion and alert user. This is ajax call:

insertItem: function (item) {
$.ajax({type: "POST",url: '@Url.Action("InsertBox", "UnidadeBox")',data: item, dataType: "json"
}).done(function (response) { 
     if (response.Success){ 
     }
     else{  
     'what i supouse to do here to set value to insertFailed and use on strategies defined below? '  }
});
},

and this is Controller called by ajax:

  [HttpPost]
  public JsonResult InsertBox(UnidadeBoxViewModel model)
        {
            try
            {
               model.Id = _service.Add(box);
                var jsonResult = new
                {
                    Success = true,
                    Model = model
                };
                return Json(jsonResult, JsonRequestBehavior.AllowGet);
            }
            catch (Exception error)
            {
                var jsonResult = new
                {
                    Success = false,
                    Message = error.InnerException != null ? error.InnerException.Message : error.Message
                };
                return Json(jsonResult, JsonRequestBehavior.AllowGet);
            }
        }

I define a finish strategy and its works but i dont know how to declare and put result on ‘insertFailed’!

var origFinishInsert = jsGrid.loadStrategies.DirectLoadingStrategy.prototype.finishInsert;
jsGrid.loadStrategies.DirectLoadingStrategy.prototype.finishInsert = function(insertedItem) {
    if(insertFailed) { // define insertFailed on done of delete ajax request in insertFailed of controller
        return;
    }
origFinishInsert.apply(this, arguments);
}

Please, help! thanks

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
tabalinascommented, Feb 18, 2016

Thank you for the good example. The reason why it happens: insertion is asynchronous, so you need to return a promise, so that finishInsert can be called, when loading is finished.

In your example it could be:

insertItem: function (item) {
    var d = $.Deferred();

    setTimeout(function () {
        console.log('insertItem');
    grid.insertFailed = true;
        d.resolve();
    },2);
    return d.promise();
}

Note: resolving the promise with actually inserted item allows to redefine data on the server and display the actual item.

0reactions
bala29000commented, Mar 8, 2018

I need to get the edited value when I click Save button which is out of grid. For Eg : I have 10 rows and 5 columns in each row (2 columns filled and remaining unfilled) When i click a particular row, 3 remaining columns will change into text boxes where i need to enter some values in it.

Now A Save button is placed outside of Grid, So when clicked I need to get the edited values in a list.

Please help out.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Use AJAX to prevent refresh when adding to a database
After submitting the form you have to use event.preventDefault() $("#submit-insert").click( function(event) { event.
Read more >
Disable Edit after Insert in UI for ASP.NET AJAX | Telerik Forums
In my grid, I have a column that I want to show a combobox for on Insert so that the user can select...
Read more >
Behavior to use .insertAfter() on load + after each ajax call?
On load it using .insertAfter() to randomly insert that block between view content element in the grid. The problem is it works on...
Read more >
PHP AJAX and MySQL - W3Schools
Explanation: When the query is sent from the JavaScript to the PHP file, the following happens: PHP opens a connection to a MySQL...
Read more >
Core AJAX Callback Commands - Drupal
The 'insert/after' command instructs the client to use jQuery's after() method to insert the given HTML content after each element matched ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found