Dynamically add property to object in angularjs ng-repeat - html

I have a list of object and iterating that list using ng-repeat to generate table row. each td contains source name and checkbox. I want to bind the checkbox with a property which is not available into list. how that is possible? The list is like that:-
scope.reasons = [
{sourceName:'Lack of rainfall'},
{ sourceName: 'Change in land use' },
{sourceName:'Change in Land Cover'}
];
and the HTML code is like that:-
<table>
<tr ng-repeat="source in reasons">
<td>{{source.sourceName}}</td>
<td><input type="checkbox" ng-checked="source.postAdequate"></td>
</tr>
</table>

You can do this using the ng-model attribute, ng-change is just for the checking purposes that is change detection.
<table>
<tr ng-repeat="source in reasons">
<td>{{source.sourceName}}</td>
<td>
<input type="checkbox" ng-model="source.postAdequate" ng-change="changeDet()">
</td>
</tr>
</table>
Demo Fiddle
Hope this helps

Try this:
<input type="checkbox" ng-model="source.postAdequate">
See here jsfiddle link:
https://jsfiddle.net/avnesh2/5cpm48tc/2/
But it will add source.postAdequate: true/false only if you click, remains objects will remains same.
So if you want to add source.postAdequate: true/false in all add in $scope.reasons before only.
Hope this will help you.

Using ng-model instead of ng-checked will add property internally when checkbox is changed
<input type="checkbox" ng-model="source.postAdequate"></td>
If you must have that property on all objects iterate the array and add it
scope.reasons.forEach(function(item){
item.postAdequate = false
})

Can you loop the scope object once to initially set postAdequate:false for all items. Then bind that object in checkbox ng-model.
angular.forEach($scope.reasons, function(e,i){
e.postAdequate = false;
});
html code
<table>
<tr ng-repeat="source in reasons">
<td>{{source.sourceName}}</td>
<td>
<input type="checkbox" ng-model="source.postAdequate" ng-click="clickfunction(source.postAdequate)">
</td></tr>
</table>

Related

RadioButton inside Table using Blazor

First of all, I am using Blazor WebAssembly 3.2.0 Preview 4.
I have a list of objects named ObjectList and each object has an bool property named principal. This list populates a table.
The logic is that only one element in the list can have the value of principal property set to true so this is why radio buttons are ideal.
In this example, I am using MatBlazor as a library for controls.
Is there a way to put radiobutton for this property inside a table as in the code below?
<table class="table table-condensed">
<thead>
<tr>
<th>Object</th>
<th>Principal</th>
</tr>
</thead>
<tbody>
#foreach (var object in ObjectList)
{
<tr>
<td>#object .name</td>
<td>
<MatRadioButton TValue="bool" Value="#object.principal"></MatRadioButton>
</td>
</tr>
}
</tbody>
</table>
The MatRadioButton button goes inside a MatRadioGroup:
<MatRadioGroup #bind-Value="#Val1" TValue="string">
<MatRadioButton Value="#string.Empty" TValue="string">Default</MatRadioButton>
<MatRadioButton Value="#("f")" Label="Female" TValue="string"></MatRadioButton>
<MatRadioButton Value="#("m")" TValue="string">Male</MatRadioButton>
<MatRadioButton Value="#("d")" Disabled="true" TValue="string">Disabled</MatRadioButton>
</MatRadioGroup>
#code
{
protected string Val1;
}
MatRadioButton Documenation
So in your case, it might be something like:
<MatRadioGroup #bind-Value="#object.principal" TValue="bool">
<MatRadioButton Value="true" TValue="bool">True</MatRadioButton>
<MatRadioButton Value="false" TValue="bool">False</MatRadioButton>
</MatRadioGroup>
I share this here also because I needed similiar, and there weren't many great answers around to be found.

How to filter the object values when an input is given?

HTML
<input type="search" placeholder="Filter" ng-model="searchstr" ng-change="details()">
<table style="width:831px;overflow:auto">
<tr ng-repeat="d in details" ng-if="$index%3==0">
<td style="height:232px;width:164px;" ng-if="details[$index]">{{details[$index].CourseName}}<br>{{details[$index].Professor}}<br>{{details[$index].CourseDuration}}</td>
<td style="height:232px;width:164px;" ng-if="details[$index+1]">{{details[$index+1].CourseName}}<br>{{details[$index+1].Professor}}<br>{{details[$index+1].CourseDuration}}</td>
<td style="height:232px;width:164px;" ng-if="details[$index+2]">{{details[$index+2].CourseName}}<br>{{details[$index+2].Professor}}<br>{{details[$index+2].CourseDuration}}</td>
</tr>
</table>
js file
$scope.data=function()
{
$scope.details=$filter("filter")($scope.details,$scope.searchstr);
}
I have tried like above but only for the first time its displaying
I got it.I just changed the slight logic in js file.I am posting it as it may be helpful to somebody else.
First i just copied the $scope.details to d and filtered with the temp variable d.
$scope.searchstr="";
var d=$scope.details;
$scope.data=function()
{
$scope.details=$filter("filter")(d,$scope.searchstr);
}
You don't need the $scope.data function. You use the builtin angular filter like
<tr ng-repeat="d in details | filter:searchstr" ng-if="$index%3==0">

setting up input type within ng-repeat

I have a list of list of input fields in options. Now, I am adding these input fields to my page using ng-repeat.
<input type="text">
I have their input types in the same options object which is repeating. These types are like this: String, Varchar, etc.
Now, I want to set type of input field according to the parameter type i have.
Here is what i want to do
<tr ng-repeat = "option in options">
<td><input type="text" ></td>
</tr>
I have a parameter named option.Paramtype which contains type of each input field. I want to set the type of input field as number if it is Int and so on.
Please help.
Given you have a array of options such as:
options = [
{Paramtype:'String', foo:25, bar:'Unicorn'},
{Paramtype:'int', foo:30, bar:'Kitten'},
{Paramtype:'Email', foo:28, bar:'Dragon'},
{Paramtype:'Date', foo:15, bar:'Kanye'}
]
Within your ng-repeat block you could use ng-if to check the Paramtype property and set the input type accordingly. Like so:
<tr ng-repeat = "option in options">
<td>
<input ng-if="option.Paramtype == 'String'" type="text">
<input ng-if="option.Paramtype == 'int'" type="number">
<input ng-if="option.Paramtype == 'Email'" type="email">
</td>
</tr>
Hopefully that helps
Otherwise if you could put an additional parameter into each element in your options array you could fill the input type using angular binding.
options = [
{Paramtype:'String', inputType:'text', bar:'Unicorn'},
{Paramtype:'int', inputType:'number', bar:'Kitten'},
{Paramtype:'Email', inputType:'email', bar:'Dragon'},
{Paramtype:'Date', inputType:'datetime', bar:'Kanye'}
]
<table>
<tr ng-repeat = "option in options">
<td><input type="{{option.inputType}}"></td>
</tr>
</table>
I'd simply suggest you to use ng-attr directive to set the value of type attribute dynamically, In order to make your implementation more cleaner, you should maintain one object that will have mapping of What Paramtype would be what input Type. Then we could use mappingOfTypes variable with its index to populate the type attribute value of input field.
Markup
<tr ng-repeat = "option in options">
<td><input ng-attr-type="{{mappingOfTypes[option.Paramtype]}}" ></td>
</tr>
Controller
$scope.mappingOfTypes = ["String": "text", "Int": 'number'];

checking a single checkbox per html row using angular js

i have a table that loads multiple checkboxes and selectboxes.when i click on one checkbox or select box it automatically selects every other box on the table .i want to have the option to choose either one checkbox or select box per row on it own.
<tr id="TableBody" ng-repeat="code in Register.RegisterDetails">
<td>{{$index+1}}</td>
<td ng-bind="code .CodeID"><input type="text" ng-model="Register.CodeID" /></td>
<td ng-bind="code .name"><input type="text" ng-model="Register.Firstname" /></td>
<td ng-bind="code .Lastname"><input type="text" ng-model="Register.Lastname" /></td>
<td><input type="checkbox" ng-model="Register.Presentstatus" id="PresentCheckbox" name="PresentCheckbox" /></td>
<td><select id="reasons" name="reasons" ng-model="Register.Category" ng-disabled="Register.Presentstatus" ng-clicked="Register.Presentstatus && O" ></td>
</tr>
my module that gets my data
function Register(){ self.RegisterDetails = function () {
var params = { pass params here };
return $http.get
{
url: GetRegisterDetails,
params: params,
success: function (data) {
self.RegisterDetails = data.data;
}
});
}
my controller
ngAppModule.controller('RegisterController',['$scope','$http',function($scope,$http)
{
var self = this;
$scope.Register = new Register($http);
}]);
all the above code works fine. i just dont know how to check a single box per row.sorry im new to this site
The main problem you're running into is you are binding to the wrong this inside your ng-repeat.
Your HTML currently repeats a checkbox for every code, but binds that to the same object property Register.Presentstatus.
<tr id="TableBody" ng-repeat="code in Register.RegisterDetails">
<td>{{$index+1}}</td>
....
<td><input type="checkbox" ng-model="Register.Presentstatus" .../></td>
</tr>
You'll need to bind this to a row-specific (code-specific) property if you want each row to have independent check boxes. Perhaps you are looking for something that binds the checkbox to an element in an array:
<tr id="TableBody" ng-repeat="code in Register.RegisterDetails">
<td>{{$index+1}}</td>
....
<td><input type="checkbox" ng-model="Register.Presentstatus[$index]" .../></td>
</tr>
or actually binds to a property of the code object
<tr id="TableBody" ng-repeat="code in Register.RegisterDetails">
<td>{{$index+1}}</td>
....
<td><input type="checkbox" ng-model="code.Presentstatus" .../></td>
</tr>
Since youur ng-model="Register.Presentstatus" is repeating and is same for all so you have same binding for all rows. you can alter them to have different binding

When using the input type "image", I'm trying to make it so that when I mouse over it, it changes to another picture

<form action='./index.php' method='post' class='form'>
<table>
<tr>
<td>$error</td>
</tr>
<tr>
<td>Username:</td>
<td><input type='text' name='user' /></td>
</tr>
<tr>
<td>Password:</td>
<td><input type='password' name='password' /></td>
</tr>
<tr>
<td></td>
<td><input type='image' src='./images/loginbtn.jpg' name='loginbtn' value='Login' alt='Submit Form'/></td>
</tr>
</table>
</form>
The above code is code from my (work in progress) website. I'm using the "image" input type as it allows me to use an image as a button. The thing I'm having trouble with here, is putting in some code that allows me to put another image in for when I mouse over it. This code is enclosed in a PHP tag and set to the variable "$form". This is so I can echo it out and run some other PHP code with it easily.
Is there any CSS that I could use to put in another picture for when I mouse over the button? Am I maybe making this more complicated than it seems?
For native javascript you can use onmouseover as per this example:
http://jsfiddle.net/42E99/
apologies for the long image urls, hope it doesn't confuse.
EDIT
CSS: http://jsfiddle.net/dZ2qL/ using hover to change background image.
2nd EDIT
http://jsfiddle.net/dZ2qL/3/
Use a <div> instead of <input> and also added some lines to the css.
background-color:transparent;
border: 0px solid;
Take a look at this >>fiddle<<
Here is a short jquery code demo:
First you need to include jquery
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
then you include this piece of code:
<script>
$(document).ready(function() {
$("#test").hover(
function () {
$(this).attr('src','http://www.seobook.com/images/smallfish.jpg');
},
function () {
$(this).attr('src','http://www.google.com/images/srpr/logo4w.png');
}
);
});
</script>
and in your html, you set id or class to your element (it must be the same with the one in the sample script, value is test in my example.
<input id="test" type='image' src='http://www.google.com/images/srpr/logo4w.png' name='loginbtn' value='Login' alt='Submit Form'/>