开发者

Spring MVC: Auto-save functionality (jQuery, Ajax...)

I'd like to implement a "auto-save" functionality on my page. I don't really know how to start though. I got one object (with a list of tasks). I'd like to submit the form every 20 seconds, s开发者_StackOverflowo the users won't lose their data. It doesn't have to be exactly like that. After every submit the submit-button should be disabled as long as there are no changes.

I'm using Spring MVC. I did some research but I'm not an expert in jQuery, Spring... So it's all pretty complicated for me. A tip, or a working example would help me a lot.

It's a pretty complex form (a timesheet). There are +/- 50 textboxes on one page (minimum, depends on the number of tasks available)

Thanks.


I don't know what spring mvc is, but in ASP.NET MVC I would do the following:

I assume all your data is in a form, you give the form an ID, then post it:

$(function () {
   var timer = 0;
   $(this).mousemove(function(e){
     timer = 0;
   });
   $(this).keypress(function() {
     timer = 0;
   });
   window.setInterval(function () {
      timer++;
    if (timer == 20) {
      $('#form').submit(function() {

      });
     }
    }, 1000);

});

Checks for mousemove, keypress, if this isnt done in 20 seconds then it saves the form.

Edit: What you could also do maybe is, after every textbox they fill in, post the data: as followed:

http://api.jquery.com/change/

$('.textbox').change(function() {
            $.ajax({
                url: '/Save/Textbox',
                data: 'TextBoxId=' + $(this).id + '&TextValue=' + $(this).value
            });
});

In this example, you make a controller called Save, action called Textbox, you give the textbox the ID of data that it has to save, and on change (after un focussing the textbox), it posts the textbox id, and the value of the box.

then in the controller you retrieve it:

  public void SaveText(string TextBoxId, string TextValue) {
   // SAVE
    }


Below Js script will help you to make ajax call when ever form field changes.

<script>
  $(document).ready($('.form-control').change(function() {
   $.ajax({
    type : "post",
    url : "http://localhost:8521/SpringExamples/autosave/save.htm",
    cache : false,
    data : $('#employeeForm').serialize(),
    success : function(response) {
     var obj = JSON.parse(response);
     $("#alert").text(JSON.stringify(obj));
     $("#alert").addClass("alert-success");
    },
    error : function() {
     alert('Error while request..');
    }
   });
  }));
 </script>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜