I've got a jqgrid (version 3.5.3) on my site which gets its results from an ajax call to a web service. Often the query is complicated and it takes a few seconds to load the result. While it is loading the user sees a box [Loading...].
In case the users realise they're searching for the wrong thing, the client has asked to add a cancel button to the grid, which would:
- make the grid forget about the data it's just requested
- retain the results already loaded from the previous search
There doesn't seem to be anything built in for this, so I'm probably looking for a bit of a hack to achieve this.
Any ideas?
Here's our solution, which is very similar to Oleg's, the main difference is that we keep track of a list of XHRs to make sure we clean all requests up
...
In general
$.ajax
request returnsXMLHttpRequest
object havingabort
method. So if the corresponding call of the$.ajax
would be have the formand we would have access to the
lastXhr
valuable then we could be able to calllastXhr.abort()
. I think that a new method likeabortAjaxRequest
in jqGrid can be the best solution.Without changing of the current source code of jqGrid the solution could looks like following
In the code I suppose, that we have a "Cancel" button with the id="cancel" outside of jqGrid. I should mention, that I don't yet tested the code above, but I hope it should work.
You should understand, of cause, that the code above aborts only the waiting of the browser on the client side and the process on the server will be continued. If your server will be implement the server side aborting, then the code above will be not needed and you will be able to call this server aborting method directly.