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

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

Related

Can not remove an item from an array using Sequelize and MYSQL

I am using MYSQL through Sequelize to build a node.js application with typescript. I created a table and the table has a field which I made a JSON dataType and made it an array by default. I have been able to push items into the array. I would like to remove items from the array, how can I achieve this?
I tried using await category.update({array_Of_food:Sequelize.fn('array_remove',Sequelize.col('array_of_food'),JSON.stringify(category.dataValues.array_Of_food && category.dataValues.array_Of_food[index]))})
I got an error that array_remove does not exist.
I solved my problem this way since I couldn't find an inbuilt way of doing it. I know this is not the best method but at least it works. At the end, I wrote a longer code.
1.Get the string value of the item you want to remove.
const indexString = food.dataValues.food_Name as string;
2.Get the index number of that item inside the array you wish to delete it from:
const index = category.dataValues.array_Of_food?.indexOf(indexString) as number;
3.Create a variable for the the array out of the model colum that you are targeting.
const arrayValue = category.dataValues.array_Of_food
4.Remove that item from the array variable that you crceated:
arrayValue?.splice(index, 1);
5.Use the update method and pass the array variable you deleted the item from as the update: This will replace the initial array values with the new array values. Remember that the new array values contain all the values in the array column excluding the value you deleted.
await category.update({array_Of_food: arrayValue})
This worked!

binding a angular reactive form FormControl to a dynamic value

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.

passing constant values to ng-model

i am trying to pass a constant value to a label and trying to access it somewhere else.This is what i have tried
<label ng-model="alertID"> 1 </label>
and i am trying to access it in a controller by passing alertID to a function in controller.but i am getting "undefined" as alertID's value.Following is the function in controller
$scope.saveEventInfo = function(alertID){
var id = alertID;
}
How do i get its value?I dont want to use ng-init
ng-model on a label doesn't make sense because it's not an input and therefore doesn't need two-way binding.
I'm guessing you just want to initialise the value like this:
<label ng-init="alertID = 1">{{ alertID }}</label>
$scope.alertID will now be 1 on your controller.

How to get 'No User found' message from the below variable in Node.js

How to get 'No User found' message from the below variable in Node.js
{"error":["No User found."]}
You can do it like this:
var data = {"error":["No User found."]};
alert(data.error[0]);
The outer object has a property "error". That property, then contains an array from which you want the first element in the array (index 0).
So, you get the error property with data.error. And, then you reach into the array and get the first element with data.error[0].
You need to iterate through this object with a for loop and grab the value based on the name of the property.
for(item in obj){
console.log(obj[item]);
}
You can use an if statement and the match method if you need to make sure that the property name is 'error'.
Here is my fiddle:
http://jsfiddle.net/nhmaggiej/eajqzxfr/

How to store value in html without showing on page

I'm using Jinja2 and Google App Engine.
I identify my models with integer num:
class TestModel(db.Model):
value = db.StringProperty()
num = db.IntegerProperty()
and have a class for editing the value:
class Edit(Handler):
def get(self):
#show input box with value, let user edit value, render page with num
def post(self):
#get new value, filter database for model instance BY NUM and replace with new value
I need to store num on the page so I can retrieve it for the post method but I don't want the page to display it.
I tried:
<span name = "num" value = "{{num}}"></span>
and in the post method I have
num = self.request.get('num')
logging.error("NUM: %s" % num)
But when I run this, num doesn't show in the logs (I have "NUM: ") and I get
ValueError: invalid literal for int() with base 10: ''
When I render the page, the source code shows that num is there. Why isn't this code properly retrieving num?
Have you tried putting the field into a hidden input element. i.e.
<input type="hidden" name="..." value="..."/>
Also make sure that the value you are trying to retrieve in your post is within the form tags.