0

We have recently upgraded the jquery to jquery/3.6.1.

I have a partial page which is called through main page via ajax. In the javascript of Partial page, I have the code like this:

$(".weighting-total").NumericBox("setValue", $(".weighting").NumericBoxSum());

and this is my function:

$.fn.NumericBoxSum = function (blankIfNoNumerics) {
var NumericCount = 0;
var sum = 0.0;

blankIfNoNumerics = blankIfNoNumerics === undefined ? false : blankIfNoNumerics;

this.each(function () {
    
    if ($(this).NumericBox("isNumeric")) // code fails here
    {
        sum += parseFloat($(this).NumericBox("getValue"));
        NumericCount++;
    }
});
if (NumericCount == 0 && blankIfNoNumerics) {
    sum = "";
}

return sum;
};

This is my widget:

$.widget("Data.NumericBox", {

options: {
    allowReservedValues: undefined,
    readOnly: undefined,
    decimalPlaces: undefined,
    showZero: undefined,
    allowNegative: undefined,
    thousandSeperator: undefined
},

_create: function () {
    this._super();
    var that = this;

    // Option: Allow Reserved Values
    if (this.options.allowReservedValues == undefined) {
        if (this.element.data("allow-reserved-values") != undefined) {
            this.options.allowReservedValues = JSON.parse(this.element.data("allow-reserved-values").toLowerCase());
        }
        else {
            this.options.allowReservedValues = false;
        }
    }

    // Option: Read Only
    if (this.options.readOnly != undefined) {
        this.element.prop("readonly", this.options.readOnly);
    }

    // Option: Decimal Places
    if (this.options.decimalPlaces == undefined) {
        if (this.element.data("decimal-places") != undefined) {
            this.options.decimalPlaces = parseInt(this.element.data("decimal-places"));
        }
        else {
            this.options.decimalPlaces = 0;
        }
    }

    // Option: Show Zero
    if (this.options.showZero == undefined) {
        if (this.element.data("show-zero") != undefined) {
            this.options.showZero = JSON.parse(this.element.data("show-zero").toLowerCase());
        }
        else {
            this.options.showZero = true;
        }
    }

    // Option: Allow Negative
    if (this.options.allowNegative == undefined) {
        if (this.element.data("allow-negative") != undefined) {
            this.options.allowNegative = JSON.parse(this.element.data("allow-negative").toLowerCase());
        }
        else {
            this.options.allowNegative = false;
        }
    }

    // Option: Thousand Seperator
    if (this.options.thousandSeperator == undefined) {
        if (this.element.data("thousand-seperator") != undefined) {
            this.options.thousandSeperator = this.element.data("thousand-seperator");
        }
        else {
            this.options.thousandSeperator = ",";
        }
    }

    // Set client-side validation to ignore N/Avail and N/Appl values
    if ($.validator.defaults.ignore.indexOf(".fd-not-available") < 0) {
        $.validator.setDefaults({
            ignore: $.validator.defaults.ignore + ",.fd-not-available,.fd-not-applicable"
        });
    }

    // Define validation for hyphens
    if (this.options.allowNegative) {
        if ($.validator.methods["val-hyphen"] === undefined) {
            $.validator.addMethod("val-hyphen", function (value, element) {
                var message = $(element).data("val-number");
                if (value == "-") {
                    return false;
                }

                return true;
            }, $.validator.format("{0}"));
        }
        this.element.attr("val-hyphen", this.element.data("val-number"));
    }

    // Handle paste
    this.element.on("paste", function (event) {
        if ($(this).hasClass("fd-not-available") || $(this).hasClass("fd-not-applicable")) {
            $(this).val("").removeClass("fd-not-available fd-not-applicable");
        }
    });

    // Handle clearing of textbox
    this.element.on("input", function (event) {
        if ($(this).val() == "") {
            $(this).removeClass("fd-not-available fd-not-applicable");
        }
    });

    this.element.keydown(function (event) {
        if (that.isReserved()) {
            if (!$(this).prop("readonly") && (event.which == 8 || event.which == 46)) {
                $(this).val("").removeClass("fd-not-available fd-not-applicable").select();
                event.preventDefault();
            }
        }
    });

    this.element.keypress(function (event) {
        if (that.element.prop("readonly")) {
            event.preventDefault();
            return;
        }

        if (event.which == 78 || event.which == 110) { // N
            if (that.options.allowReservedValues) {
                that.setReserved(ReservedValue.NotAvailable);
                $(this).change();
            }
            event.preventDefault();
            return;
        }

        if (event.which == 80 || event.which == 112) { // P
            if (that.options.allowReservedValues) {
                that.setReserved(ReservedValue.NotApplicable);
                $(this).change();
            }
            event.preventDefault();
            return;
        }

        if (event.which == 45) { // -
            if (!that.options.allowNegative) {
                event.preventDefault();
                return;
            }
            if (that.isReserved()) {
                that.setValue("");
            }
            that.refresh();
            return;
        }

        if (event.which == 46) { // .
            if (that.options.decimalPlaces == 0) {
                event.preventDefault();
                return;
            }
            if (that.isReserved()) {
                that.setValue("");
            }
            that.refresh();
            return;
        }

        if (event.which >= 48 && event.which <= 57) { // 0 - 9
            if (that.isReserved()) {
                $(this).val("");
            }
            if (that.isReserved()) {
                that.setValue("");
            }
            that.refresh();
            return;
        }
        event.preventDefault();

    });

    this.element.focusin(function (event) {
        var value = that._internalFormat($(this).val());
        $(this).val(value);
    });

    this.element.focusout(function (event) {
        var value = that._externalFormat($(this).val());
        $(this).val(value);
    });

    this.element.val(this._externalFormat(this.element.val()));
    this.refresh();
},

_externalFormat: function (value) {
    if ($.isNumeric(value)) {
        if (parseFloat(value) == 0 && !this.options.showZero) {
            return "";
        }
        else if (!ReservedValue.isReserved(value)) {
            var signPart = value > -1 && value < 0 ? "-" : ""; // Required: In the following statement parseInt removes the sign for small negative numbers
            var integerPart = parseInt(numeral(value).format("0" + (0).toFixed(this.options.decimalPlaces).substr(1)));
            var fractionalPart = Big(value).minus(Big(integerPart)).abs();

            var display = signPart + numeral(integerPart).format("0" + this.options.thousandSeperator + "0") +
                fractionalPart.toFixed(this.options.decimalPlaces).substr(1);

            return display;
        }
    }

    return value;
},

_internalFormat: function (value) {
    try {
        value = value.replace(/,/g, "");
        if ($.isNumeric(value)) {
            if (this.options.decimalPlaces == 0) {
                value = parseInt(value);
            }
            else {
                value = Big(value).format("0" + (0).toFixed(this.options.decimalPlaces).substr(1)).toString();
            }
        }
        return value;
    }
    catch (e) {
        return value;
    }
},

refresh: function () {
    var value = this._internalFormat(this.element.val());
    if (this.options.allowReservedValues) {
        if (value == ReservedValue.NotAvailable) {
            this.element.val(ReservedValue.text(value)).removeClass("fd-not-applicable").addClass("fd-not-available");
        }
        else if (value == ReservedValue.NotApplicable) {
            this.element.val(ReservedValue.text(value)).removeClass("fd-not-available").addClass("fd-not-applicable");
        }
        else {
            if (this.element.hasClass("fd-not-available") || this.element.hasClass("fd-not-applicable")) {
                this.element.removeClass("fd-not-available fd-not-applicable");
            }
        }
    }
    return this;
},

setValue: function (value) {
    this.element.val(this._externalFormat(value));
    this.refresh();
},

getValue: function () {
    return this._internalFormat(this.element.val());
},

isNumeric: function (includeReservedValues) {
    includeReservedValues = includeReservedValues === undefined ? false : includeReservedValues;
    if (!includeReservedValues && this.isReserved()) {
        return false;
    }
    else {
        return $.isNumeric(this.getValue());
    }
},

isReserved: function (resval) {
    var value = ReservedValue.parse(this._internalFormat(this.element.val()));
    if (resval === undefined) {
        return ReservedValue.isReserved(value);
    }
    else {
        return value == resval;
    }
},

setReserved: function (resval) {
    this.element.val(resval).valid();
    this.refresh();
},

getReserved: function () {
    return ReservedValue.parse(this._internalFormat(this.element.val()));
}

}); The error is "Uncaught Error: cannot call methods on NumericBox prior to initialization; attempted to call method 'isNumeric'". I tried the following links:

jquery ui Dialog: cannot call methods on dialog prior to initialization

and other links but these didnt help. Kindly suggest. The jquery version is jquery/3.6.1.

Gauravsa
  • 6,330
  • 2
  • 21
  • 30

1 Answers1

0

Managed to get it working by initializing. Thanks @badpiggie

$(".weighting-total").NumericBox().("setValue", $(".weighting").NumericBoxSum());
Gauravsa
  • 6,330
  • 2
  • 21
  • 30