im working on a project, I used mobiscroll input panel. It works great with default presets.
Can I create my own presets?
For example: I want to have a thousands separator for typed numbers, but this should only be visual, and the set value should be without these separators.
I researched user presets and found this
one .
So, I tried this, with custom setting of numpad. For my first attempt, I wanted the behavior of the presets, as in the demo Mobiscroll: Demo
They use a "variable fraction", but I want to define a preset, so I don’t have to write "parseValue: ....." - Code every time I use this type of numpad.
This is the code I used (didn't work): I think I did something wrong.
JQuery / JavaScript
$(function(){
$('#numpad').mobiscroll().numpad({
theme: 'mobiscroll',
display: 'modal',
template: 'dddddddddddddd',
placeholder: '',
fill: 'ltr',
allowLeadingZero: true,
preset: 'mypreset',
buttons: ['set','cancel','clear'],
leftButton: {
text: '.',
value: '.'
}
});
$.mobiscroll.presets.numpad.mypreset = function(inst) {
return {
parseValue: function (value) {
if (value) {
return value.toString().split('');
}
return [];
},
formatValue: function (value) {
return value.join('');
},
validate: function (values) {
var disabledButtons = [],
invalid = false;
if (!values.length || values.length >= 9 || values.indexOf('.') !== -1) {
disabledButtons.push('.');
}
if (values.length == 1 && values[0] === 0) {
for (var i = 1; i <= 9; i++) {
disabledButtons.push(i);
}
}
if (!values.length || values[values.length - 1] == '.') {
invalid = true;
}
return {
invalid: invalid,
disabled: disabledButtons
};
},
headerText: false
}
}
});
HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0">
<title>Mobiscroll</title>
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<link href="css/mobiscroll.custom-2.15.1.min.css" rel="stylesheet" type="text/css" />
<script src="js/mobiscroll.custom-2.15.1.min.js" type="text/javascript"></script>
<style type="text/css">
body { padding: 0; margin: 0; font-size: 16px; font-family: arial, verdana, sans-serif; }
input, select { width: 100%; padding: .625em; margin: 0 0 .625em 0; border: 1px solid #aaa; box-sizing: border-box; -moz-box-sizing: border-box; -webkit-box-sizing: border-box; }
.header { padding: .625em; background: #5185a8;}
.header h1 { margin: 0; padding: 0; color: #fff; text-align: center; font-size: 1.25em; font-weight: bold; text-shadow: 1px 1px 1px #000; text-overflow: ellipsis; overflow: hidden; white-space: nowrap; }
.content { padding: 1em; }
</style>
<script src="/js/test.js"></script>
</head>
<body>
<div data-role="fieldcontain" class="demo-cont" id="demo_cont_numpad1">
<label for="demo_numpad1">Try Decimal Numpad</label>
<input type="text" id="numpad" />
</div>
</div>
</body>
</html>
My last question is, am I right, or are there other (better) ways to do this?
Addition: Sorry for my poor English.