PrimeNg Checkbox execute function - javascript

I've been trying to get a function to happen whenever I press a checkbox in a datatable. Basically what I have is a datatable with default values in it (1 to 10 for example). Then I give an array of items to my component which are present in an object of mine (2 & 5 for example). now when the grid is constructed It'll all the default elements in the dataTable but Only the data that is in our object will have a checked checkbox next to them. Now what I want to do is to have my addOrRemove function executed wich depending on the state of the checkbox will add or remove a default data object from the array ([2,5] in this case). So 2 or 5 can be unchecked and thus removed from the array and 1,3,4,6,7,8,9,10 can be checked and added to the array.
I don't realy have a problem writing the code for this but I'm struggling to have my function being executed whenever I press the checkbox.
this is how I have my checkbox defined in my 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>
using this I'm not getting any functions triggered.
In the PrimeNg documentation there is stated that p-checkbox has an event onChange. but whenever I use that I'm getting the 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
That is whenever I change the (change) to (onChange).
I've read that it has something to do with my checkbox being inside of a template and therefore there is no direct access to my component and its functions. Any help on this would be much appreciated.

The answer to this question is:
Make sure you take enough breaks and read your code carefully. In my component I have a function called addOrRemove(), in my html I had addOrRemoveRow().

Related

Angular agGrid - Is it possible to pass rowSelection event into cellRenderer?

I've got a custom radio button cellRenderer which I'm trying to display as selected when a row is clicked.
It works when clicking on the button directly, but I want it to also select on row selection, if the user clicks elsewhere.
The problem is, the cellRenderer doesn't know when it's row is clicked. The API documentation for agGrid only has a cellClicked method.
Is there a way to do this?
There are a few ways of doing this. Here is one approach:
Firstly, you can listen to whether a row has been clicked or selected via the following events: rowClicked, rowSelected.
Read more about these events in the documentation:
https://www.ag-grid.com/javascript-grid-events/#reference-selection
You can either add these events globally on the gridOptions or, on the cell renderer itself by utilising the Grid API method: addEventListener (which you can read more about here: https://www.ag-grid.com/javascript-grid-api/#reference-events)
Once you have captured the row selection/clicked event, you can get the instance of the cell renderer by using the API method getCellRendererInstances, you can read more on this implementation here: https://www.ag-grid.com/javascript-grid-cell-rendering-components/#accessing-cell-renderer-instances
Finally, you have the instance of the renderer, you can do whatever you want to set the radio button to be checked. e.g. adding a method on the renderer to change the state.

select data on click of checkbox in table

I have a table with checkboxes and I want to capture the data in the table rows on click of each of the checkboxes. I have a functional call (click)="checkConditions($event, data)" which is called on click of each checkbox.
Here data gives me all the data of the row which I want to get an array variable. The issue is that on click of the first checkbox it works fine. I get the data and I push it in an empty array but if I click the second checkbox, I lose the original data and only have the latest data. I have tried this.checkedInstruments.push(...data);
it gives me an error "Found non-callable ##iterator" in angular 8.
Can anyone help?
Yogesh, I can see the array checkedInstruments is getting initialized again with empty array and hence you have only the latest data and not the previous one. Try declaring the checkedInstruments array at class level and check again.

React/JSX - how to code an editable input with initial content provided via props that only updates its parent when submitted?

All the examples I have found that keep local state can't provide an initial value for the input. In my case a parent component retrieves field from a server and passes these to my input form where the fields shall be editable. But I don't want to pass each change back up the hierarchy if I can avoid it, rather only when the form is submitted (either via button or by pressing enter)
Update: the usage is as follows. Imagine a todo list. The top level holds the list and the detail component. When I click at the list the detail should update to show the selected todo text. That text should be editable.
So as far the the detail component goes the initial state of the input is the text from the list that gets passed down in props. It needs to change when a different todo in the list is selected. On the other hand I should be able to edit it and when submit triggered that todo text should be passed back up via a callback prop.
So I have to keep local state to collect the input, but I want that state to be initialized with the existing todo text from the list. If I use the Facebook example of an uncontrolled form, I find the edited text remains displayed when I switch to a different todo in the list. Maybe I'm doing it wrong or is it a conceptual problem? Using controlled input initializing the state in the constructor doesn't work either because the constructor only gets called once (not on each re-render)!
I solved the problem by using the lifecycle method componentWillReceiveProps(nextprops) where I can set the state to the new props. This method gets called each time a re-render becomes necessary - in my case because the parent changes the childs props.
See https://facebook.github.io/react/docs/react-component.html#componentwillreceiveprops
You can set the initial state based on the property passed in from the parent, then edit that state in the component before submitting with the form submission

How to programmatically add components or views inside other components/views in ember js

I have a component say {{component-1}} which gets called many times and creates a custom-texbox container and label as many times as it gets called.
Now whenever a user writes something in it a suggestion box should appear below it. Since the suggestion box can be reused everywhere i dont want to have a separate suggestion box for each {{component-1}}, rather i want to have another component called {{suggestion-box}} that gets inserted inside component-1 i.e. the textbox container.
I dont want {{suggestion-box}} to be inside dom at all since it is needed only when somebody types in it. I want to add/insert it into {{component-1}} when someone types. Instead of a component i even tried to use a view
Here are the different things i have tried and failed
Note:
suggestionBox is the component
textbox-container is an element inside {{component-1}}
Inside {{component-1}}this.$().find(".textbox-container").append(this.suggestionBox );where this.suggestionBox = suggestionBoxComponent.create(); I have event tried suggestionBoxView.create();It gives me the error that i one view cant be inserted into another and i need to use containerView
var tmp = Ember.Handlebars.compile('<div class=".suggestionBox"></div>');this.$().find('.textbox-container').append(tmp());I get the error called
Uncaught TypeError: Cannot read property 'push' of undefined
I even tried to use view instead of component i.e. make suggestionBox a view but then again i cannot insert one view inside another
I have tried a lot more things.
Few points:
I dont want alternate solutions of how textbox and suggestion box could be created
How to pass information from a component or a a template to a view? Say i do {{view "suggestion-box"}} inside component-1 template, how do i pass values to it? Say for components we pass in the context like this {{component1 sampleVar1=val1 sampleVar2=val2}}
i Want to know how to programmatically add a component or a view and if it is a view how to pass the data to it?
I dont want to use container-view since it will cause more complexities, however if your solution allows me to pass value from {{component-1}} to container-view and inturn pass it to corresponding childView1 and childView2 then that solution is acceptable
Just an update:
I even tried to use a view container inside the {{component-1}}
I also tried to use view block inside {{component-1}} i.e.
{{#view "view-name"}}
----earlier component elements here-----
{{/view}}
In both the above points "view-name" is a ContainerView which is getting inserted properly but the component element are not getting inserted
This can be achieved with a ContainerView and the viewName attribute. The component or view can access the ContainerView or any other view that is part of a template through its assigned viewName.
Example,
hbs
{{view Ember.ContainerView viewName="my-menu-container"}}
js - then from the component or view
this.get("my-menu-container").pushObject(suggestionBoxViewOrComponent);
http://emberjs.jsbin.com/yoyujeqi/1/edit
p.s. the context of the example is not relevant to the suggestion box, sorry about that, as it has been extracted from another answer related to adding a menu view dynamically.

YUI Custom Event for Dropdown value pre-selection?

I have a dropdown field and its value is pre-selected as soon as its rendered (Say, its a Country field in a signup form). And I have other dropdowns or other components which change the selected value of the first dropdown dynamically. Now I want to fire a method with every "value getting selected" in the dropdown. Is it possible?
To put my question in much more clearer way, I want to create a onDefaultValueSet event and subscribe the first dropdown to it. So in which ever way the dropdown gets any value selected, the corresponding handler (my function) gets called.
I tried to do it with YUI Custom Events, but I am not sure how the browser will be calling(understanding) my handler every time a value is selected in the dropdown.
onSelect (from Default DOM) is not a right answer I guess, as I tried it.
Please help me in tackling this problem.
I am not sure whether this is an answer, but I've found a workaround. Please validate this.
So I was making a item "selected" using javascript-dom manipulation. Meaning, using
domElement.options[5].selected = True;
So with the (YUI)custom event I created, I started calling "fire()" right after this. So the code becomes:
domElement.options[5].selected = True;
onDefaultValueSetEvent.fire(domElement.name);
But I am not sure, what if the particular option is selected in a gui fashion. Meaning, how automatically fire() method is called

Categories