Last active
December 2, 2016 14:26
-
-
Save travist/26c59c61986b3e23942ff0654a49ed21 to your computer and use it in GitHub Desktop.
Auto-incrementing value in Form.io
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| angular.module('myApp') | |
| .controller('FormController', ['$scope', 'Formio', function($scope, Formio) { | |
| $scope.form = null; | |
| $scope.submission = {data: {}}; | |
| var formio = (new Formio('https://myproject.form.io/myform')); | |
| var getAutoIncrement = function(fieldName, cb) { | |
| formio.loadSubmissions({params: {sort: '-data.' + fieldName, limit: 1}}).then(function(subs) { | |
| var value = 0; | |
| if (subs.length) { | |
| value = parseInt(subs[0].data[fieldName], 10); | |
| value++; | |
| } | |
| cb(null, value); | |
| }, cb); | |
| }; | |
| formio.loadForm().then(function(form) { | |
| $scope.form = form; | |
| }); | |
| $scope.$on('formSubmission', function(event, submission) { | |
| getAutoIncrement('incrementingField', function(err, value) { | |
| if (err) { return } | |
| $scope.submission.data.incrementingField = value; | |
| formio.saveSubmission($scope.submission); | |
| }); | |
| }); | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <formio form="form" submission="submission"></formio> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment