Calculate the sum of the column values and disable the option to insert new rows in the UI grid

I am currently working on a ui-grid that has a column C which displays percentage values. There is a button labeled "add rows" that allows users to add new rows to the grid. However, the catch is that users can only add new rows until the total percentage in column C reaches 100%. After that point, no more rows can be added.

How should we define the grid options to accommodate this limitation?


$ctrl.structuringGrid = {
columnDefs: [
{
name: 'structurer',
displayName: 'Structurer',
headerCellClass: 'text-align-center',                  
headerTooltip: true,

editableCellTemplate: "<input name=\"structurer\" ng-model=\"MODEL_COL_FIELD\" ng-class=\"'colt' + col.uid\" class=\"form-control\" type=\"text\" uib-typeahead=\"user as user.name for user in grid.appScope.$ctrl.fetchUsers($viewValue)\" typeahead-select-on-blur='true' typeahead-min-length=\"4\" typeahead-editable=\"false\" ui-grid-editor>",
cellTemplate: "<input style=\"height:26px;\" name=\"structurer\" ng-model=\"MODEL_COL_FIELD\" ng-class=\"'colt' + col.uid\" class=\"form-control\" type=\"text\" uib-typeahead=\"user as user.name for user in grid.appScope.$ctrl.fetchUsers($viewValue)\" typeahead-select-on-blur='true' typeahead-min-length=\"4\" typeahead-editable=\"false\" ui-grid-editor>",
cellEditableCondition: true,
width: 200
},
{
name: 'Business',                    
displayName: 'Structuring Business',
headerCellClass: 'text-align-center',                     
headerTooltip: true,  
width: 200,
cellTemplate: "<select name=\"structBusiness\" model=\"MODEL_COL_FIELD\" list-id=\"'STRUCT_BUSINESS'\" align=\"right\" edit-enabled=\"true\" disable=\"false\" custom-select-value=\"'value'\" ordering=\"'value'\"></select> ",
cellEditableCondition: true
},
{
name: 'SubBusiness',                    
displayName: 'Structuring Sub-Business',
cellTemplate: "<loud-select name=\"'structSubBusiness'\" model=\"MODEL_COL_FIELD\" items=\"$ctrl.subbusinessList\" align=\"right\" edit-enabled=\"true\" disable=\"false\" custom-select-value=\"'value'\" ordering=\"'value'\"></loud-select>",
headerCellClass: 'text-align-center',                     
headerTooltip: true,
width: 200,
cellEditableCondition: true
},
{
name: 'BusinessAllocation',                    
displayName: 'Business Allocation',
cellTemplate: "<input style=\"height:26px; background-color: #091c2b;\" type=\"number\" name=\"structBusinessAllocation\" ng-model=\"MODEL_COL_FIELD\" text-align=\"right\" placeholder=\"0.00\" ng-max=\"99.99\" ng-min=\"0\" > </input>",
headerCellClass: 'text-align-center',
aggregationType: uiGridConstants.aggregationTypes.sum,
headerTooltip: true,
width: 200,
cellEditableCondition: true,
height: 25
},
{
name: 'actions',
displayName: 'Actions',                    
headerTooltip: true,
width: 100,
enableFiltering: false,
cellTemplate: getButtons()
}
],
enableSorting: false,
minRowsToShow: 7,
autoResize: true,
enableHorizontalScrollbar: 0,
enableVerticalScrollbar: 1,
enableRowSelection: true,
enableFullRowSelection: true,
enableRowHeaderSelection: false,
multiSelect: false,
enableFiltering: false,
enableGridMenu: false,
onRegisterApi: function(gridApi){
$ctrl.aGridApi = gridApi;

}
};

When the button is clicked, the following code is executed:


$ctrl.addNewRow = function() {
console.log(" Total business allocation -------------------------- " + $ctrl.GridApi.grid.columns[3].getAggregationValue());
$ctrl.Grid.data.push({Id:'', structurer: '', business: '', SubBusiness: '', BusinessAllocation: ''});  
};

Answer №1

Implemented ng-change() method in order to achieve the desired outcome.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Trouble triggering hidden radio button in Angular 9 when clicked

I have implemented radio buttons within a div with the following CSS styles (to enable selection by clicking on the div): .plans-list { display: flex; justify-content: center; margin: 2rem 0; .plan { display: flex; margin: 0 0.5rem; p ...

Tips for concealing a dynamic table element in Angular 9

I have dynamically generated columns and rows in a table. Here is my HTML for the table component: <table id="tabella" class="table table-striped table-hover"> <thead class="thead-dark"> <tr> <th *ngFor="let header of _ob ...

I am seeking to transform an elongated image into a two-dimensional circle using WebGL. This process will involve intentional distortion of the image

Trying to figure out how to take a high-resolution image and wrap it into a circle has been a challenge. It's like bending a steel bar until it forms a perfect circle with both ends touching. I've been grappling with threejs for the past 8 hours ...

What is the best way to determine whether a YouTube video permits embedded playback?

When working with user-generated content, it's important to note that YouTube channels may restrict their videos from being played in an embedded player. In order to provide a better user experience, I need to detect the specific reason why a video ca ...

How can I write the code to enable dragging the button for navigating to the next page?

<a draggable="true" class="user" id="leonardo" ondragstart="dragUser(this, event)" aria-selected="undefined"> IPD</a> If I want the button to navigate to the next page when dragged, what code should I write? ...

Implementing dual language codes for a single locale in internationalization (i18n) efforts

I am currently using i18n to display translations in English and Japanese. The default language is set to English with the code en, but I have recently discovered that my website is not utilizing the correct ISO language code for Japanese, which should be ...

JS function to reverse a string

Not to worry, I've managed to figure out how to reverse a string (a one-word string at least). > function reverse(s){ > return s.split("").reverse().join(""); } Is there a way to reverse a string in this manner: "Dady come home" -> "yd ...

Tips for sending attributes to jQuery autocomplete

I'm facing a major issue with implementing a jquery autocomplete feature, and JavaScript isn't my strong suit. Currently, I'm using the jquery.auto-complete plugin available at: https://github.com/Pixabay/jQuery-autoComplete, which is an up ...

Issue with Gulp Watch failing to detect modifications in Browserify files

Currently, I am utilizing the laravel-elixir-vueify npm package for my project. Gulp watch functionality performs as expected when I make changes to files within the "scripts" or "styles" functions. However, there seems to be an issue when it comes to moni ...

Assigning a value to a JavaScript variable using Ajax

Struggling with a problem for hours and still can't find the issue... A registration form is set up for users to create accounts. When the submit button is clicked, a validateForm function is triggered. Within this function, some JavaScript tests ar ...

Angular's $watch function does not receive the parameter 'newVal'

I have been facing an issue displaying messages from a backend response in an Angular AJAX call. I am using a ShareDataService to share data between different controllers. 'use strict'; var angularApp = angular.module('angularApp'); ...

Using JQuery and JavaScript to store and dynamically apply functions

I have a code snippet that looks like this:. var nextSibling = $(this.parentNode).next(); I am interested in dynamically changing the next() function to prev(), based on a keypress event. (The context here is an input element within a table). Can someo ...

Having trouble with installing Recharts through npm

When I try to install recharts using npm, I encounter the following error message in my console: npm ERR! code ERESOLVE npm ERR! ERESOLVE unable to resolve dependency tree npm ERR! ...

Dynamic Population of Django Drop Down List using JavaScript

In my Django + Python website, users can request access to a database through a form that provides them with the following options: Environment: A dropdown list with two values - Development and Production. Permission: Another dropdown list with two val ...

What is the process of saving a model with @tensorflow/tfjs-node version 2?

I've been struggling with setting up the save handler to save my model. I've scoured through various platforms like stack overflow and GitHub, but haven't had any luck. Help! Any guidance would be greatly appreciated!!! :) Below is a snipp ...

Remove any nulls or undefined values from the object

I have developed a custom function that eliminates null and undefined values from an object. However, it seems to be mistakenly removing keys where the value is 0 as well. For instance: { key1: 'value1', key2: 0 } Even though it shouldn&ap ...

Streamlined approach to triggering ng-change on a single textbox

Consider this array within an angular controller: somelist = [ { name: 'John', dirty: false }, { name: 'Max', dirty: false }, { name: 'Betty', dirty: false } ]; To iterat ...

How can we modify the position of a header from fixed to relative when the mobile drop-down menu is opened?

I am experiencing an issue with my responsive design. When viewed on a device that is less than 600px wide, the drop-down multi-level navigation overflows downward and does not scroll because the header has a fixed position. As a result, the tabs in the me ...

Combining objects in JavaScript

I am currently working on converting the object received from the server into a format compatible with the backend system. I have a received object that looks like this { 'User.permissions.user.view.dashboard': true, 'Admin.permissio ...

What is the reason for the jQuery plugin not being applied after replacing the page content with an Ajax response?

At the moment, I am utilizing jQuery ajax to dynamically add content to my website. Additionally, I have incorporated the jquery.selectbox-0.6.1.js plugin to enhance the style of select boxes on the page. The plugin successfully styles the select boxes up ...