In the project I’m working on, we use jqGrid to display hierarchical records in an Excel-like manner. Changes are queued up on the client and submitted to the server whenever the user clicks the “Save” button. That means a user could make a bunch of changes, then navigate away from the page accidentally, and lose everything they just did. Preventing that is easy enough though, just use this JavaScript snippet:
1: window.onbeforeunload=checkForChanges;
2: function checkForChanges(){
3: var rows = $('#treeTable').getChangedCells('all');
4: if (rows != null && rows.length > 0) {
5: return 'You have unsaved changes that will be discarded.';
6: }
7: };
The checkForChanges function is bound to the onbeforeunload event, which is fired whenever the browser window is about to change the page. This includes page reload/refreshes or anything like that. The checkForChanges function uses the jqGrid getChangedCells method to see if there are any unsaved changes. If there are, it returns a message that will be shown automagically in a JavaScript confirm dialog. If there aren’t, it doesn’t return anything, and the user is allowed to navigate without being nagged.
I’m going to be a jerk here, and say that the first problem is that you’re trying to give excel-like behavior to a web app.
I expect I’d catch flak for that comment, but I’ve yet to see a solution to do this that wasn’t a huge hack. And it always ended up with some miserable pile of garbage that’s hard as hell to maintain, and that users still find frustrating.
When it comes up currently, I put on my "Angry Rob" hat and start ranting. If people want excel-like behavior, then fucking GIVE THEM EXCEL! If it just HAS to be that style of interface, you’re only causing yourself pain by trying to imitate.
I recommend allowing an excel import. If you need to have it formatted or certain columns, blah blah blah, then it’s simple enough to generate a blank excel file, with the columns and rows needed, and let them fill in the blanks. I’ve always though this is a much better solution. No javascript, no inconsistencies, no users bitching because it doesn’t have whatever excel feature. All you do is generate the template (simple), import the filled out one (simple), and validate what they gave you. If it’s invalid, then tell them to fix and try again. Hell, you can even alter the file they uploaded to point out incorrect cells or some such, and let them redownload and fix.
(Btw, I’m not yelling at you, I’m yelling at the situation)
Yeah, this product is shaping up very hackishly, honestly, but being web-based and multi-user is one of the big distinguishing features for this system. I have thought about using Excel + .NET add-ins + web services, but I don’t think the "customer" will accept that.
I just had an idea – a web control that you can initialize with an Excel document, make certain fields editable, etc, and then add multi-user access permission to.
@James, you mean an ActiveX control that displays an Excel spreadsheet?
I don’t know what you two are talking about exactly, but I hate you both.
No, not only display an Excel spreadsheet, and not in ActiveX. Straight up javascript with the ability to edit the spreadsheet.