hjg
2024-03-18 8d48c59d5d3bcc4148ef97dc6f98e3a8cf9ca436
提交 | 用户 | 时间
58d006 1 var formatNumber = function (element, valueAccessor, allBindingsAccessor, format) {
A 2     // Provide a custom text value
3     var value = valueAccessor(), allBindings = allBindingsAccessor();
4     var numeralFormat = allBindingsAccessor.numeralFormat || format;
5     var strNumber = ko.utils.unwrapObservable(value);
6     if (strNumber) {
7         return numeral(strNumber).format(numeralFormat);
8     }
9     return '';
10 };
11
12 ko.bindingHandlers.numeraltext = {
13     init: function (element, valueAccessor, allBindingsAccessor) {
14         $(element).text(formatNumber(element, valueAccessor, allBindingsAccessor, "(0,0.00)"));  
15     },
16     update: function (element, valueAccessor, allBindingsAccessor) {
17         $(element).text(formatNumber(element, valueAccessor, allBindingsAccessor, "(0,0.00)"));
18     }
19 };
20
21 ko.bindingHandlers.numeralvalue = {
22     init: function (element, valueAccessor, allBindingsAccessor) {
23         $(element).val(formatNumber(element, valueAccessor, allBindingsAccessor, "(0,0.00)"));
24
25         //handle the field changing
26         ko.utils.registerEventHandler(element, "change", function () {
27             var observable = valueAccessor();
28             observable($(element).val());
29         });        
30     },
31     update: function (element, valueAccessor, allBindingsAccessor) {
32         $(element).val(formatNumber(element, valueAccessor, allBindingsAccessor, "(0,0.00)"));
33     }
34 };
35
36 ko.bindingHandlers.percenttext = {
37     init: function (element, valueAccessor, allBindingsAccessor) {
38         $(element).text(formatNumber(element, valueAccessor, allBindingsAccessor, "(0.000 %)"));
39     },
40     update: function (element, valueAccessor, allBindingsAccessor) {
41         $(element).text(formatNumber(element, valueAccessor, allBindingsAccessor, "(0.000 %)"));
42     }
43 };
44
45 ko.bindingHandlers.percentvalue = {
46     init: function (element, valueAccessor, allBindingsAccessor) {
47         $(element).val(formatNumber(element, valueAccessor, allBindingsAccessor, "(0.000 %)"));
48
49         //handle the field changing
50         ko.utils.registerEventHandler(element, "change", function () {
51             var observable = valueAccessor();
52             observable($(element).val());
53         });
54     },
55     update: function (element, valueAccessor, allBindingsAccessor) {
56         $(element).val(formatNumber(element, valueAccessor, allBindingsAccessor, "(0.000 %)"));
57     }
58 };