Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
5368
WebDataGrid running balance column
posted

I have a WebDataGrid that is being used for something like a checking account register.  It needs to maintain a running balance in the last column... which means all balances must be recalculated on every add/edit.  I would prefer to do this without a postback.

is there a code sample that demonstrates how to loop through all the rows using the CSOM and read/modify cell values?  I intend to use the same code to modify individual cell formatting (coloring negative amounts and balances red).

 

Parents
No Data
Reply
  • 5368
    Verified Answer
    Offline posted

    In case anyone else runs into a similar need... the following code does two things:

    • Maintains a running balance in each row
    • Formats number cells based on their value

    Just need to call "refreshBalances()" after each client-side grid update.  There may be a better way to wire it to a grid event.

       <style type="text/css">

            .amountpositive

            {

                color: Green;

            }

            .amountnegative

            {

                color: Red;

            }

            .amount0

            {

                color: Black;

            }

        </style>

     

     

    <script type="text/javascript">

        function dgRegister_Initialize(sender, eventargs) {

                refreshBalances();

            }

            function refreshBalances() {

                var rows = $find("WebSplitter1_tmpl0_dgRegister").get_rows();

                var rowlength = rows.get_length();

                var rowindex = 0;

                var currentAmount = 0;

                var runningBalance = 0;

                var currentrow;

                for (rowindex = 0; rowindex <= rowlength - 1; rowindex++) {

                    currentrow = rows.get_row(rowindex);

                    currentAmount = currentrow.get_cellByColumnKey("Amount").get_value();

                    runningBalance += currentAmount;

     

                    currentrow.get_cellByColumnKey("Balance").set_value(runningBalance);

                    format_cellAmount(currentrow.get_cellByColumnKey("Balance"));

                    format_cellAmount(currentrow.get_cellByColumnKey("Amount"));

                }

            }

            function format_cellAmount(dgcell) {

                var currentvalue = dgcell.get_value();

                if (currentvalue == 0) {

                    dgcell.set_text("-");

                    dgcell.get_element().className = "amount0";

                }

                if (currentvalue > 0) {

                    dgcell.get_element().className = "amountpositive";

                }

                if (currentvalue < 0) {

                    dgcell.get_element().className = "amountnegative";

                }

            }

     

     

    </script>

Children