1
0
-1

Hi experts, i want to ask on how to calculate percentage of shares. since the total shares is outside the spreadsheet.

percentage = (shares (in spreadsheet)/ total share )*100



ive tried to use this : Auto calculate values on the List Grid

but my code wont works. :

<script>
$(function() {
    // Function to calculate the percentage of shares
    function calculateSharePercentage(row, totalShares) {
        var sharesValue = parseFloat($(row).find("td:nth-child(2) span span").text()); // shares value is in the 2nd column
        var percentage = (sharesValue / totalShares) * 100;
        return percentage.toFixed(2); // returning percentage with 2 decimal places
    }

    // Calculate and update values when any form element in the grid changes
    $(".grid.form-element").on("change", function() {
        var cell = $(this).find(".hover");
        var row = $(cell).closest("tr");

        if ($(row).find("td")) {
            // Calculate and update share percentage
            var totalShares = parseFloat($("#total_sharess").text()); // assuming the total shares is in an element with ID 'total_sharess'
            if (!isNaN(totalShares) && totalShares > 0) {
                var sharePercentage = calculateSharePercentage(row, totalShares);
                $(row).find("td:nth-child(3) > span").text(sharePercentage + "%"); // percentage should be displayed in the 3rd column

                // Update hidden JSON definition if needed
                var json = $(row).find("textarea").val();
                var obj = JSON.parse(json || '{}');
                var fieldId = $(row).find("td:nth-child(3) > span").attr("column_key"); // assuming column_key is set

                if (fieldId) {
                    obj[fieldId] = sharePercentage;
                    var newJson = JSON.stringify(obj);
                    $(row).find("textarea").val(newJson);
                }
            }
        }
    });
});
</script>

    CommentAdd your comment...

    1 answer

    1.  
      1
      0
      -1

      Hi, perhaps you could try the solution in spreadsheet auto-calculation.

        CommentAdd your comment...