data-bind=""
attribute.autoNumeric()
method allows you to activate auto-masking on the specified element using a selector, for example, like this: $('.autonum').autoNumeric();
autoNumericGet();
method autoNumericGet();
- allows you to get a value that can be used in arithmetic operations (ie, the value freed from the delimiters that decorate the input data). For example: $(selector).autoNumericGet();
autoNumericSet(value);
- processes the value
, decorating it with the necessary delimiters, for the beauty of the output of numbers. For example, $(selector).autoNumericSet(value);
autoNumeric()
method as a parameter: $('.autonum').autoNumeric({ aSep: ',', aDec: '.', mDec: 0 });
ko.bindingHandlers.numberMaskedValue = { init: function(element, valueAccessor, allBindingsAccessor) { // , , // var options = allBindingsAccessor().autoNumericOptions || { aSep: ',', aDec: '.', mDec: 0 }; // html $(element).autoNumeric(options); // 'focusout' , // observable ko.utils.registerEventHandler(element, 'focusout', function() { var observable = valueAccessor(); // value = $(element).autoNumericGet(); observable(isNaN(value) ? 0 : value); }); }, update: function(element, valueAccessor) { var value = ko.utils.unwrapObservable(valueAccessor()); // , , $(element).autoNumericSet(value); } };
<input type="text" data-bind="numberMaskedValue: countOfFriends"/>
<input type="text" data-bind="numberMaskedValue: countOfFriends, autoNumericOptions:{aSep: ',', aDec: '.', mDec: 3}"/>
<div data-bind="foreach: humans"> <div class="block-of-data"> <div> <label class="label label-info">id</label> <span data-bind="text: id"></span> </div> <div> <label class="label label-info">Name</label> <span data-bind="text: name"></span> </div> <div> <label class="label label-info">Count of friends</label> <input type="text" data-bind="numberMaskedValue: countOfFriends"/> </div> <div> <label class="label label-interest">Real value "Count of friends"</label> <span data-bind="text: countOfFriends"></span> </div> </div> <div>
$(function() { ko.bindingHandlers.numberMaskedValue = { init: function(element, valueAccessor, allBindingsAccessor) { var options = allBindingsAccessor().autoNumericOptions || { aSep: ',', aDec: '.', mDec: 0 }; $(element).autoNumeric(options); ko.utils.registerEventHandler(element, 'focusout', function() { var observable = valueAccessor(); value = $(element).autoNumericGet(); observable(isNaN(value) ? 0 : value); }); }, update: function(element, valueAccessor) { var value = ko.utils.unwrapObservable(valueAccessor()); $(element).autoNumericSet(value); } }; function Human(idv, namev, countOfFriendsv) { var id = ko.observable(idv), name = ko.observable(namev), countOfFriends = ko.observable(countOfFriendsv); return { id: id, name: name, countOfFriends: countOfFriends } } function HumansModel() { humans = ko.observableArray([new Human(1,'Alex', 1234), new Human(2,'Bob',12457)]); } ko.applyBindings(new HumansModel()) });
.block-of-data{ border: solid 1px black; margin:10px; padding: 5px; background-color:#ffffaa; -webkit-border-radius: 10px; -moz-border-radius: 10px; border-radius: 10px; } .label { padding: 1px 4px 2px; -webkit-border-radius: 3px; -moz-border-radius: 3px; border-radius: 3px; } .label { font-size: 10.998px; font-weight: bold; line-height: 14px; color: white; text-shadow: 0 -1px 0 rgba(0, 0, 0, 0.25); white-space: nowrap; vertical-align: baseline; background-color: #999; } .label-info { background-color: #3A87AD; } .label-interest { background-color: #ff7722; }
Source: https://habr.com/ru/post/149010/
All Articles