binding a angular reactive form FormControl to a dynamic value - html

I have a reactive form instantiated using FormBuilder. I would like to use this form for updating or adding a new employee. I am trying to add an expression to the formBuilder constructor that checks to see whether there is a currently selected employee and if there is use one of that employee's interface fields as the value on the form and if there's no current selected employee have a empty string as the value of the specific FormControl.
what i've tried:
'EMP_NM': [this.selectedEmp ? this.selectedEmp.EMP_NM : '', Validators.required],
and then in my editEmployee function:
editCashier(employee: IEmployee) {
this.selectedEmp = employee;
this.empForm.reset();
console.log(this.selectedEmp.EMP_NM);
}
When I do this the value shown on the EMP_NM input field in the HTML doesn't get updated. i've tried setting a setTimeout() in my editEmployee function but that didn't work either. Any idea what might be going wrong here?
I initialize the empForm in my conponent's constructor using formBuilder's group function. Would it help if I moved that to a different function and then call that function everytime the editEmployee function is called to re-initialize the form?

You can update it with patchValue or setValue:
this.empForm['controls']['employee'].patchValue(employee);
Have no clue if it's a nested control but you can go more layers down.

Related

Pass parameter to SSRS report, while initializing a default value

I am trying to pass a new parameter to a SSRS report, therefore I have created 2 methods in the Contract class:
[DataMemberAttribute('LanguageId')]
public LanguageId parmLanguageId(languageId _languageId = languageId)
{
languageId = _languageId;
return languageId;
}
And another to retrieve the value:
public LanguageId getLanguageId()
{
return languageId;
}
In my DP class I am also retrieving the value:
languageId = _contract.getLanguageId();
So far, so good, the dialog when running my report is passing through the value with the selected Language ID in my dialog.
The problem I'm having, is when initializing a default value in the dialog, it doesn't pass my selected value, but the default value initialized when opening the dialog.
I was thinking to edit my parmLanguageId like:
[DataMemberAttribute('LanguageId')]
public LanguageId parmLanguageId(languageId _languageId = 'DefaultLanguageValue')
{
languageId = _languageId;
return languageId;
}
But, then comes the part where I have to change the variable to the selected value, I probably miss a simple solution, but how do I accomplish this?
Try overriding method prePromptModifyContract in your report controller class and adding following lines in it:
YourContract contract = this.parmReportContract().parmRdpContract() as YourContract;
contract.parmLanguageId('DefaultLanguageValue');

How to use 2d array on ngmodel in angular 2?

Would like to know how to set 2d array in ngmodel for dynamic checkbox?
I have a role and permission setup form for a super admin with multiple checkbox.
How to differentiate model name with role id and permission id. I need to pass role id and permission id with the model name in an array.
eg : [(ngModel)]="permission[role.id][per.id]"
Is there a way to assign 2d value for ngmodel in Form?
Looking forward for an earliest response.
Thanks in advance.
As ngModel is directive to obtain 2-way binding system.
You can easily do the syntax:
[(ngModel)]="permission[role.id][per.id]"
where permission is an empty array defined initially as:
permission = []
or
permission = [[]]
where your permission variable will be a multidimensional array.
After this, if you try to ngModel
[(ngModel)]="permission[role.id][per.id]"
it will cause a undefined issue as ngModel not only gets the value from input it has to show also that is how are 2-way binding works.
To avoid issue while rendering due to undefined, we have to assign dummy data to the permission Array initially only.
this.permission = new Array(this.numberOfRoles).fill(0);
for (let j = 0; j < this.numberOfRoles; j++) {
this.permission[j] = new Array(this.numberOfPermission).fill(0);
}
This solves the undefined issue such as below
enter image description here

Razor Html.Editor set default value

I have searched but no good solution for now.
I try using is like:
#Html.Editor("userName", new { htmlAttributes = new { value = "ABC" } })
Though, the value doesn't want to appear in renderred HTML.
I need to use Editor element as well as this is kind of 'search' field.
Do you know how is it possible to pass value to Html.Editor?
You should not attempt to override the value attribute in the HtmlHelpers. Instead set the default value in the GET method before you pass the model to the view.
You have indicated userName is not a property of your model (in which case you should be using a view model), but you can use
ViewBag.userName = "yourDefaultValue";

insert query symfony 2 without form

I'm working with Symfony 2 and I need to insert some data in a MySQL table. I know how to do it using a form:
$m=new table();
$form=$this->container->get('form.factory')->create(new tableType(),$m);
$request=$this->getRequest();
if($request->getMethod()=='POST')
{
$form->bind($request);
if ($form->isValid())
{
$rm=$this->container->get('doctrine')->getEntityManager();
$rm->persist($m);
$rm->flush();
}
that works but I dont want to use a pre-defined form because I need complex control on my input. I need to generate the value with jQuery.
So how can I proceed to insert the values of my input into my table?
Generally you can pass the whole request to the action as following
use Symfony\Component\HttpFoundation\Request;
// <...>
public function fooAction(Request $request)
{
$foo = $request->query->get('foo', 'default_value_for_foo'); // get the foo param from request query string (GET params)
$bar = $request->query->get('bar', 'default_value_for_bar'); // get the bar param from request POST params
}
Also you might be interested in collection form types which can allow you to generate multiple rows or entities for form (not fixed)
That's actually easier than using forms :D
<?php
// ...
// fetch your params
$m = new table();
$m->setWhatever($request->get('whatever'));
// persist
$em = $this->get('doctrine.orm.entity_manager');
$em->persist($m);
Note:
use camel-case PHP-class names: e.g. Table, NiceTable
$form->bind is deprecated I think, use $form->handleRequest
anyway if you need to validate your input, I recommend using validators and forms anyway. Setting up a model and validate it is quite smart. You don't need to create the view createView() of it of course, but the validation component in Symfony is very mighty :).

cant set Index ObjectChoiceField (load slow json)

I have a select that I get Json post with http, but I try to sets initially selected index but there is nothing in the list do not select anything. because the json is great.
public AppMainScreen() {
loadLists();
MySelect = new ObjectChoiceField( "Select: ", new Object[0], 3 );
VerticalFieldManager vfm = new VerticalFieldManager(Manager.VERTICAL_SCROLL);
vfm.add(MySelect);
add(vfm);
}
This statement appears wrong to me:
new ObjectChoiceField( "Select: ", new Object[0],3);
The second parameter to this constructor is supposed to be an array of objects whose .toString() method will be used to populate the choices. In this case, you have given it a 0 length array, i.e. no Objects. So there is nothing to choose. And then you have asked it to automatically select the 3rd item, and of course there is no 3rd item.
You should correct the code to actually supply an object array.
One option to make it easy is have your JSON load actually create a String array with one entry per selectable item. Then you use the index selected to identify the chosen item.