Framework7 makes it easy to store and parse form data, especially in Ajax loaded pages. And, when you load the same page again, Framework7 can automatically fill in the last content for you!
Enable form autostorage
To enable automatic form storage, simply:
- Add a "store-data" class to the form
- Add an id attribute to the form. If there is no id, it will not work properly.
- Make sure your inputs have a "name" attribute, inputs without a name attribute will be ignored
Automatic form storage without any JS code
- <!-- Form has additional "store-data" class to enable form storage on this form -->
- <formid="my-form"class="list-block store-data">
- <ul>
- <li>
- <divclass="item-content">
- <divclass="item-inner">
- <divclass="item-title label">Name</div>
- <divclass="item-input">
- <!-- Make sure that input have "name attrobute" -->
- <inputtype="text"name="name"placeholder="Your name">
- </div>
- </div>
- </div>
- </li>
- <li>
- <divclass="item-content">
- <divclass="item-inner">
- <divclass="item-title label">E-mail</div>
- <divclass="item-input">
- <!-- Make sure that input have "name attrobute" -->
- <inputtype="email"name="email"placeholder="E-mail">
- </div>
- </div>
- </div>
- </li>
- ... other form fields ...
- </ul>
- </form>
JS API
How is this achieved? It's actually very simple, Framework7 will call formToJSON when the form has any changes, and then call formFromJSON when the "pageInit" event fires.
All form data are stored in localStorage, each form has its own key, the key rules are: localStorage.f7form-[formID]
, where [formId] is the id attribute of the form. Each such key contains all the data for a form.
Below are some of the available methods for managing form data
myApp.formGetData(formId) - 获取id对应的表单的数据
- formId - 字符串 - form 的id
- 返回一个JSON数据
myApp.formDeleteData(formId) - 删除id对应的表单数据
- formId - 字符串 - form 对应的id
myApp.formStoreData(formId, formJSON) - store formJSON data for form with formId "id" attribute
myApp.formStoreData(formId, formJSON) - 把一个JSON数据存储到一个表单对应的localStorage中
- formId - 字符串 - 表单的id属性
- formJSON - 对象 - 要存储的JSON对象
- <form id="my-form2" class="list-block store-data">
- ... form fields ...
- </form>
- <div class="content-block">
- <p><a href="#" class="button get-storage-data">Get Data</a></p>
- <p><a href="#" class="button delete-storage-data">Delete Data</a></p>
- <p><a href="#" class="button save-storage-data">Save Data</a></p>
- </div>
- var myApp = new Framework7();
- var $$ = Dom7;
- $$('.get-storage-data').on('click', function() {
- var storedData = myApp.formGetData('my-form2');
- if(storedData) {
- alert(JSON.stringify(storedData));
- }
- else {
- alert('There is no stored data for this form yet. Try to change any field')
- }
- });
- $$('.delete-storage-data').on('click', function() {
- var storedData = myApp.formDeleteData('my-form2');
- alert('Form data deleted')
- });
- $$('.save-storage-data').on('click', function() {
- var storedData = myApp.formStoreData('my-form2', {
- 'name': 'John',
- 'email': '[email protected]',
- 'gender': 'female',
- 'switch': ['yes'],
- 'slider': 10
- });
- alert('Form data replaced, refresh browser to see changes')
- });
Form storage JavaScript Events
Event Target DescriptionformFromJSON | Form element | Event will be triggered after formFromJSON method called on form to fill form fields |
formToJSON | Form element | Event will be triggered after formToJSON method called on form to convert form fields to JSON |