Running a function when an Angular PrimeNg Checkbox is checked

Recently, I’ve been working on implementing a functionality that triggers when a checkbox within a datatable is clicked. The scenario involves a default set of values displayed in the table (ranging from 1 to 10). Additionally, I provide an array of selected items to the component based on my own object (such as 2 and 5). As the grid is constructed, all default elements appear in the DataTable, but only those from our selection are marked with checked checkboxes.

The objective is to have an addOrRemove function execute accordingly; depending on the checkbox status, it should either include or exclude a default data object from the array (e.g., [2,5]). Upon unchecking 2 or 5, they should be removed from the array, while checking others like 1, 3, 4, 6, 7, 8, 9, 10 would add them.

Though capable of writing the necessary code, I face challenges in ensuring my function is invoked upon checkbox press. Below is how the checkbox is defined in the HTML file:

<p-column header="Included" [style]="{'width':'80px', 'text-align':'center'}">
    <template let-object="rowData" pTemplate="body">
        <p-checkbox binary="true" [(ngModel)]="object.included" (change)="addOrRemoveRow($event,object.data)"></p-checkbox>
    </template>
</p-column>

Despite configuring the checkbox this way, the functions do not trigger as expected. The PrimeNG documentation mentions the event onChange for p-checkbox, but using it results in an error:

EXCEPTION: Error in http://localhost:3000/app/shared/grid/checkable-grid/checkable-grid.component.html:9:12 caused by:
 self.parentView.context.addOrRemoveRow is not a function

It appears that changing (change) to (onChange) is problematic due to the checkbox being located inside a template, limiting direct access to the component and its functions. Any assistance on this matter would be greatly appreciated.

Answer №1

Here is the solution to your query:

To avoid errors, it is crucial to schedule regular breaks and review your code meticulously. I encountered a discrepancy between the function addOrRemove() in my component and addOrRemoveRow() in my HTML.

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

The functionality of Bootstrap's JavaScript is dependent on the presence of jQuery - Reactjs framework

When attempting to combine bootstrap with reactjs, I encountered an issue with importing Bootstrap.js It seems that Bootstrap's JavaScript requires jQuery Below are some of the import statements I have tried: import $ from 'jquery'; imp ...

Executing a command in the terminal does not yield the identical outcome as executing it from the debug feature

Below is the code that I am currently working with: var Tesseract = require('tesseract.js') var filename = '2.jpg' Tesseract.recognize(filename) .progress(function (p) { console.log('progress', p) }) .c ...

Combining selected boxes through merging

Looking to create a simple webpage with the following requirements: There should be 10 rows and 3 boxes in each row. If I select 2 or more boxes or drag a box, they should merge together. For example, if my initial screen looks like this: and then I se ...

"Using the fetch Prototype.json() method can lead to the alteration or corruption of the

For the past week, I've been struggling with a seemingly impossible issue in my Javascript code: The problem arises when trying to retrieve data from the server using fetch: fetch(this.props.url, { method: 'POST', b ...

Is it possible to retrieve JavaScript object properties using HTML elements?

I have fetched an array of objects using jQuery.getJSON(). Each object should be represented by an HTML div element, allowing users to click on the element and access all properties of the corresponding object. What is the most efficient approach for achie ...

Sort through an array of objects by their properties

https://i.sstatic.net/Vgc3E.png Is there a way to efficiently filter an array of objects based on a specific key-value pair within each object? ...

Output repeated JSON Array in console using Node.js

Looking to identify duplicates within a JSON array based on the URL. var temp = [ { "name":"Allen", "site":"www.google.com/allen" }, { "name":"Chris", "site":&q ...

What is the process for editing a JSON file and preserving the modifications?

I have a JSON file with a key (food_image) and I am looking to dynamically assign the index value in a loop to it, such as food_image0, food_image1 ... food_image{loop index}. After that, I aim to save this modified JSON file under a new name. All values ...

Progress bar status displayed while uploading multiple files

I'm currently working on a Django project where I have set up a page to input data information about a file and then upload the file itself. https://i.sstatic.net/jB5cr.png Whenever the user clicks on the 'More datasets' button, it dyna ...

Scrolling seamlessly on websites with a single page

I am developing a single-page website that uses different section IDs instead of multiple pages. It's functioning properly, but I want to implement smooth scrolling to the sections rather than just statically jumping to them on the page. Jsfiddle: ht ...

Creating a unique array of non-repeating numbers in ES6:

Looking to create an array of unique random numbers in ES6 without any repeats. Currently, my function is generating an array of random numbers that are repeating: winArray = [...Array(6)].map(() => Math.floor(Math.random() * 53)); Here is a non-ES6 ...

informing the server (possibly through XMLHttpRequest) during the onunload event?

Is there a way to send a single HTTP request with some data to a server to notify it when a client leaves the page? The response is not important in this scenario. Currently, I am using a python-tornado comet application with JavaScript maintaining a requ ...

Utilizing FullCalendar for showcasing comprehensive details

I'm a big fan of the fullcalendar plugin and all its amazing features. I want to enhance it by displaying more customized information for each event when in agendaWeek or agendaMonth view. Switching between views is easy, but I need help filtering out ...

What is the best way to define objects containing arrays of objects in JavaScript?

I am seeking to create a blueprint for the following data structure, with all values provided by the user: { "name":"ABC", "city":"X", "child": [{"name":"ccc","age":"22"}, {"name":"ccc","age":"22"} ] } This data structure consists of a parent obje ...

A guide on retrieving styled text from a database and displaying it in TinyMCE

I am encountering an issue where I need to retrieve formatted text from a database and display it in my TinyMCE editor. The content stored in the database looks like this: <p style="text-align: justify;"><strong>Zdrav&iacute;m</strong ...

Exploring paths deep within by employing wildcards as a query

I have data structured according to Firebase's flat structure advice, storing quotes in nodes like this: quotes -> clientName -> quoteObject Each 'quoteObject' includes a 'dateCreated' value that I aim to retrieve as follow ...

Reorganizing form input array following the dynamic addition and deletion of fields

Currently, I am in the process of developing a custom WordPress widget that allows users to add and remove blocks of input fields in the widget back-end. While I have managed to make the add and remove functionality work smoothly, I am facing an issue when ...

Custom validator failing to trigger ClientValidationFunction

Google may be filled with similar questions, but my issue seems to be a bit unique. What sets it apart is that I am using masked input and have included jquery.maskedinput-1.2.2.js and jquery-1.7.2.min.js in order to utilize masked inputs. The code for mas ...

What is the method for specifying the default option in a SELECT Tag and how can the Index of the chosen option be obtained?

Looking for HTML and JavaScript code that can manipulate SELECT and OPTION Elements. <select> <option> </option> <option> </option> </select> How do I retrieve the index of the selected option and obtai ...

How can we capture and execute a function on the server in Next.js in response to a user's request for the index page (/)?

Is it possible to use a middleware function or another method in Next.js to check if a user is trying to access the home page? My goal is to intervene when a user tries to reach the home page. Intercepting a URL request is quite straightforward with Next. ...