.. or how I learned to love the bomb
| Don't reinvent the wheel (Photo credit: Wiertz Sébastien) |
And that brings you into inventing another even cleverer mouse trap or re-inventing the wheel. And that mouse trap or wheel has to be cross browser and multi-functional, databound et cetera et cetera et cetera.
Me, I don't like re-inventing wheels, that has been done so many times already.
So, clear the stage for third party controls
| Telerik (Photo credit: martin.linkov) |
I must admit that there is a bit of a learning curve, but with the help of some good examples on the demo site and the ever helpful support desk of Telerik you get everything up and running in no time.
As easy as .. whatever is easy.
But it is not all easy
So here is our SaveAll Javascript function.
function SaveAll(button) {
showLoader();
$.ajaxSetup({
async: false,
cache: false
});
$('.t-grid').each(function (index) {
var grid = $(this).data('tGrid');
grid.submitChanges();
});
$('form').not('.t-edit-form').each(function (index) {
$.ajax({
url: $(this).attr('action'),
data: $(this).serializeArray(),
type: $(this).attr('method')
});
});
hideLoader();
$.ajaxSetup({
async: true,
cache: true
});
return false;
}
- We start by showing a loader div (with spinning gif )on the page and so making it impossible for the user to do something while everything gets saved.
- Then we make sure that all consequent ajax calls are asynchronous as we would otherwise run ito trouble.
- Next we loop through all the grids submit all changes for each grid.
- Then we loop through all the forms (excluding the forms with class "t-edit-form", these are are for inline editing in the grids) and we submit the forms through ajax.
- Finally, we hide the loader div again.
I have omitted checking for validation errors and what to do when everything is saved. Maybe we need to move to another page?
Well, it almost works, can I go home now?
We discovered a weird behavior in our grids. Because we wanted to unclutter our pages we also removed the delete buttons in the grids for removing records and added a column at the beginning of each row with a checkbox so the user can select multiple rows. Then using a delete button in the toolbar you can delete the rows in one go.
Great!
And then we discovered that the value of the first data column was not saved using our button. Other columns worked perfectly. The data was simply not in the request to the server.
After a lot of reaearch, Googling, trial and error
$('.t-grid').each(function (index) {
var grid = $(this).data('tGrid');
// save any editing rows first
$(this).find(".t-grid-edit-row").each(function () {
grid.updateRow(this);
});
grid.submitChanges();
});
Close and still no cigar.
The problem was that the edited column was somehow not persisted in the client side data. So that was what needed to be made sure. Persist all data in client side memory before submitting the grid changes.
But how?
More Googling
Trying evermore results from Google brought me to Paul Reynold's blog. In his post "enhanced batch editing using telerik extensions for asp .net mvc grid control" he explains a lot. Not all of it was relevant for my solutions, but I found a bit of code that helped to fix my problem. Thank you, Paul. $('.t-grid').each(function (index) {
var grid = $(this).data('tGrid');
// save any editing cells first
$(grid.element).find(".t-grid-edit-cell").each(function () {
grid.saveCell(this);
});
grid.submitChanges();
});
I tried to update an entire row (which resulted in a server call) and the solution is to save each cell and these get persisted client side. Just what I wanted.
Lessons learned
- Third party controls are great for making life easier, because all normal functionality is there and tested.
- Third party controls make things really difficult at times when you want to do something slightly out of the box.
- Solutions can be found by hunting through multiple search results and reading all the way through these.
- Never give up.









