Angular.js - How to pass the $index to a function? - arrays

This is my structure in scope:
$scope.tables = [
{name: 'tweets',
columns: ['message', 'user'],
items: [
{message: 'hello', user: 'mike'},
{message: 'hi', user: 'bob'},
{message: 'hola', user: 'bob'}
]
},
{name: 'users',
columns: ['username'],
items: [
{username: 'mike'},
{username: 'bob'}
]
}
];
This is the "addTweet" function. Since the button that calls this function is generated from a loop, it needs to pass the $index to the function. I've tried the following but it doesn't work. The button doesn't seem to trigger anything at all. If I call the function from outside the loop though, it works.
$scope.addTweet = function(id){
$scope.tables[id].items.push({message: $scope.message, user: $scope.user});
};
<table ng-repeat="table in tables">
<form ng-submit="addTweet($index)">
<tr>
<td ng-repeat="column in table.columns">
<input type="text" ng-model="column"></input>
</td>
<td>
<button type="submit">Add Row</button>
</td>
</tr>
</form>
</table>

Don't use tag 'form' inside tag 'table' - it's incorrect html syntax.
Use this way:
<table ng-repeat="(tableIdx, table) in tables">
<tr>
<td ng-repeat="column in table.columns">
<input type="text" ng-model="column"></input>
</td>
<td>
<button type="submit" ng-click="addTweet(tableIdx)">Add Row</button>
</td>
</tr>
</table>
or this
<form ng-submit="addTweet($index)" ng-repeat="table in tables">
<table>
<tr>
<td ng-repeat="column in table.columns">
<input type="text" ng-model="column"></input>
</td>
<td>
<button type="submit">Add Row</button>
</td>
</tr>
</table>
</form>
I'm sorry. I didn't see addTweet function code. $scope.message and $scope.user properties are not initialized in your $scope, because this binding ng-model="column" refer to the $scope.table[tableId].columns[columnId]
If you want to add new item and work with dynamic model names try this:
<tr ng-init="table.newPost={}">
<td ng-repeat="column in table.columns">
<input type="text" ng-model="table.newPost[column]"></input>
</td>
<td>
<button type="submit">Add Row</button>
</td>
</tr>
And code for addTweet function:
$scope.addTweet = function(id) {
$scope.tables[id].items.push($scope.tables[id].newPost);
$scope.tables[id].newPost = {};
}

Related

angularjs + get multiple checkbox value and attribute values

I am trying to get the multiple checkbox value and attribute values.
Each checkbox have multiple attribute values(data-id, data-ot,data-si).
How to get the checked checkbox with 3 attribute values.
<table>
<thead class="search">
<tr>
<th>
<input type="checkbox" class="selectall " ng-model="selectall" ng-click="select(data)">
</th>
</tr>
</thead>
<tbody>
<tr >
<td>
<input data-id="287" data-ot="31" data-si="541" type="checkbox" - ng-model="rowselect">
</td>
</tr>
<tr >
<td>
<input data-id="295" data-ot="331" data-si="31" type="checkbox" ng-model="rowselect">
</td>
</tr>
<tr >
<td>
<input data-id="297" data-ot="321" data-si="31" type="checkbox" ng-model="rowselect">
</td>
</tr>
<tr >
<td>
<input data-id="296" data-ot="451" data-si="671" type="checkbox" ng-model="rowselect">
</td>
</tr>
<tr >
<td>
<input data-id="293" data-ot="91" data-si="651" type="checkbox" ng-model="rowselect">
</td>
</tr>
<tr >
<td>
<input data-id="294" data-ot="13" data-si="14" type="checkbox" ng-model="rowselect">
</td>
</tr>
</tbody>
</table>
<input type="button" name="Submit" value="Submit" ng-click="ShowSelected()" />
Expected output:
For example 2 checkbox selected selected means:
[{"id":"xx","ot":"xx","si"},{"id":"xx","ot":"xx","si"},]
Code:
app.controller('MyCtrl', function($scope) {
$scope.ShowSelected = function() {
///
};
});
I don't see the point in keeping your data inside attributes like that unless that is something you specifically need for some particular reason.
The easiest way to do this is to make an object array on your scope like this:
$scope.data = [
{id: 123, ot: 234, si: 567, checked: false},
{id: 321, ot: 243, si: 789, checked: false},
{id: 345, ot: 678, si: 567, checked: false}
];
Then you can use ng-repeat to create the table and bind the checkboxes to the .checked property:
<table>
<thead>
<!-- your table header -->
</thead>
<tbody>
<tr ng-repeat="element in data">
<td>
<input type="checkbox" ng-model="element.checked">
</td>
</tr>
</tbody>
</table>
And then write the showSelected() function:
$scope.showSelected = function(){
var tempArray = [];
for(var i=0; i<data.length; i++){
if(data[i].checked) tempArray.push(data[i]);
}
return tempArray; //or maybe console.log(tempArray) or encode to JSON etc.
}
Add attribute 'checked' to your checkbox model
$scope.checkboxList = {[
{
"id": "xx",
"ot": "xx",
"checked": false
},
{
"id": "xx",
"ot": "xx",
"checked": false
}
]}
Use ng-model in your html to bind to your checked attribute
<tr ng-repeat="checkbox in checkboxList ">
<td><input type="checkbox" ng-model="checkbox.selected"></td>
<td>{{checkbox.ot}}</td>
</tr>
And in your controller you can use $filter to filter your checkboxes
app.controller('MyCtrl', function($scope, $filter) {
$scope.ShowSelected = $filter('filter')($scope.checkboxList , { $: true });
});

AngularJS Sort table columns

I have a table filter whichs show the content according to the user parameters, this is working perfectly but now I want to let the user filter according to each column.
This is the issue:
When you type "User" on the text input (Filter by columns" I want to only display the "User Column", if you type "Start Date" then only displays "Start Date column" I'm wondering if this is possible using filter option or how can I approach to this requirement.
Here's my code:
<div class="col-xs-10 grid-container">
<div class="row form-entry">
<div class="col-xs-3 input-container">
<input type="text" placeholder="Search in Grid" ng-model="searchFills"/>
</div>
<div class="col-xs-3 input-container">
<input type="text" placeholder="Filter by columns" />
</div>
</div>
<div class="col-xs-10 grid-container">
<div class="generic-grid">
<table class="table-bordered grid-table table-hover">
<thead>
<tr>
<th>id</th>
<th>User</th>
<th>Alt User</th>
<th>Creation Date</th>
<th>Start Date</th>
<th>Recieved Date</th>
<th>1</th>
<th>2</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="fill in fills_list | filter:searchFills">
<td>
{{fill.id}}
</td>
<td>
{{fill.user}}
</td>
<td>
{{fill.useralt}}
</td>
<td>
{{fill.creationdate}}
</td>
<td>
{{fill.startdate}}
</td>
<td>
{{fill.recieveddate}}
</td>
<td>
</td>
<td>
</td>
</tr>
</tbody>
</table>
</div>
</div>
You can add ng-model="columnFilter" to your input:
<input type="text" placeholder="Filter by columns" `ng-model="columnFilter"` />
Then addng-hide="columnFilter == 'Start Date'" to <td>{{fill.user}}</td>
Add ng-hide="columnFilter == 'User'" to <td>{{fill.startdate}}</td>
And add ng-hide="columnFilter == 'User' || columnFilter == 'Start Date'" to the rest of the <td>'s.
If you want to filter by column, then you need ng-repeat by columns. After that you can use the same filter that you are using for filtering rows
controller.js
controllers.controller("MainController", ["$scope", function($scope) {
return ["$scope", function ($scope) {
$scope.columns = [{
title: "id",
alias: "id"
}, {
title: "User",
alias: "user"
}, {
title: "Alt User",
alias: "useralt"
}, {
title: "Creation Date",
alias: "creationdate"
}, {
title: "Start Date",
alias: "startdate"
}, {
title: "Recieved Date",
alias: "recieveddate"
}];
}]);
template.html
<div ng-controller="MainController">
<div class="col-xs-10 grid-container">
<div class="row form-entry">
<div class="col-xs-3 input-container">
<input type="text" placeholder="Search in Grid" ng-model="searchFills" />
</div>
<div class="col-xs-3 input-container">
<input type="text" placeholder="Filter by columns" ng-model="searchColumn" />
</div>
</div>
<div class="col-xs-10 grid-container">
<div class="generic-grid">
<table class="table-bordered grid-table table-hover">
<thead>
<tr>
<th ng-repeat="column in columns | filter:searchColumn">{{column.title}}</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="fill in fills_list | filter:searchFills">
<td ng-repeat="column in columns | filter:searchColumn">{{fill[column.alias]}}</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
That will filter any column in a table.

Hiding the table columns and rows using AngularJs

I have checkboxes with table headers, i want to hide the table columns and rows based on the checkbox click,
<ul>
<li ng-repeat="opt in fieldvalues"><input type="checkbox" ng-model="checked" value="{{opt}}" />{{opt}}</li>
</ul>
<table>
<tr>
<th ng-show="checked=='true'">Activity ID</a></th>
<th>Activity Description</th>
</tr>
<tr ng-repeat="nm in makerQueueData">
<td ng-show="checked=='true'">{{nm.formattedIdentifier}}</td>
<td>{{nm.description}}</td>
</tr>
</table>
I tried but no luck.
<ul>
<li ng-repeat="opt in fieldvalues"><input type="checkbox" ng-model="checked" value="{{opt}}" />{{opt}}</li>
</ul>
<table>
<tr>
<th ng-show="checked"><a>Activity ID</a></th>
//here checked gets bool value itself based on selection. you don't need to compare it to string 'true'.
//keep in mind i assume {{checked}} returns only bool value
<th>Activity Description</th>
</tr>
<tr ng-repeat="nm in makerQueueData">
<td ng-show="checked">{{nm.formattedIdentifier}}</td>
<td>{{nm.description}}</td>
</tr>
</table>
Working fiddle for your : http://jsfiddle.net/b69pkeLd/1/
Let me know if any issue occurs.
I hope this is what you are looking for:
var myApp = angular.module('myApp',[]);
function MyCtrl($scope) {
$scope.fieldvalues = [
{text: 'Test1', checked: false},
{text: 'Test2', checked: false}
];
$scope.makerQueueData = [
'Whatever your', 'data is'
];
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="MyCtrl">
<div ng-repeat="opt in fieldvalues">
<input type="checkbox" id="checkbox-{{$index}}" ng-model="opt.checked" value="{{opt.text}}" />
<label for="checkbox-{{$index}}">{{opt.text}}</label>
<table ng-show="opt.checked">
<tr>
<th>Activity ID</a></th>
<th>Activity Description</th>
</tr>
<tr ng-repeat="nm in makerQueueData">
<td ng-show="opt.checked'">{{$index}}</td>
<td>{{nm}}</td>
</tr>
</table>
</div>
</div>
Moreover, use <label> for type="checkbox" description. This makes the label clickable.

How to use collapse in a directive?

I have this part of code where second <tr> appears only on Edit click and a form appears which I'm displaying using directive.
<table class="table">
<tbody ng-repeat="Emp in Employees">
<tr>
...
<td>
<input type="button" class="btn btn-default" value="Edit" ng-click="isCollapsed = !isCollapsed" />
</td>
</tr>
<tr collapse="isCollapsed">
<td>
<div>
<employee-form></employee-form>
</div>
</td>
</tr>
</tbody>
</table>
Now I have a button in my employeeForm directive as well which when clicked should make this form disappear (isCollapsed=true). But somehow that is not working.
Markup for employee-form:
<form>
...
<button type="button" class="btn btn-default" value="Cancel" ng-click="isCollapsed = true" />
</form>
JS part for this is:
$scope.isCollapsed = true;
So what I want is on click of the Cancel button in the employeeForm the <tr> should disappear.
View in Plunker
I hope it may help you:..
HTML:
<div ng-app="myapp" ng-controller="myctrl" id="id01">
<table>
<tbody ng-repeat="emp in Employees">
<tr>
<td>
<p>{{emp}}<input type="button" value="edit" ng-click="setshowindex($index)"/></p>
</td>
</tr>
<tr ng-class="{'show':$index==showing,'hide':$index!==showing}">
<td>
<employee-form></employee-form>
</td>
</tr>
</tbody>
</table>
Script:
angular.module('myapp',[])
.controller('myctrl',function($scope){
//$scope.collapsed = true;
$scope.Employees = ['abc','xyz','klm'];
$scope.showing = -1;
$scope.setshowindex = function (i) {
$scope.showing = -1;
$scope.showing = i;
};
})
.directive('employeeForm',function(){
return {
restrict: 'E',
replace:true,
template: '<form><input type="text" ng-value="emp" /><button ng-click="setshowindex(-1)">Cancel</button></form>'
};
});
CSS:
tr.show{
display: table-row;
}
tr.hide{
display: none;
}

Angularjs Binding data to other table by check radio button, and update after click button

<table>
<tr>
<td><input type="radio" name="groupName" value="song" ng-model="$parent.selectedSong"/></td>
<td>{{song.name}}</td>
<td>{{song.artist}}</td>
<td>{{song.genre}}</td>
<td>{{song.price}}</td>
<td>
<input type="button" value="Delete" ng-click="deleteItem(song.id)" />
</td>
<td>
<input type="button" value="View" ng-click="go('OneSong', song)" />
</td>
</tr>
</table>
<table border="1">
<tr><td id="Td1">Correct Table</td></tr>
<tr>
<td>Name: </td>
<td><input type="text" ng-model="editItem.name"/></td>
</tr>
<tr>
<td>Artist: </td>
<td><input type="text" ng-model="editItem.artist"/></td>
</tr>
<tr>
<td>Genre: </td>
<td><input type="text" ng-model="editItem.genre"/></td>
</tr>
<tr>
<td>Price: </td>
<td><input type="text" ng-model="editItem.price"/></td>
</tr>
<tr>
<td colspan="2">
<!--<input type="button" value="Insert "ng-click="addItem()"/>-->
Add New Song
<input type="button" value="Update" ng-click="updateItem()"/>
<input type="button" value="Cancel" ng-click="cancel()"/>
</td>
</tr>
</table>
From the little information you provided, I am taking a stab at this:
http://jsfiddle.net/V44fQ/
var app = angular.module('songApp',[]);
app.controller('SongCtrl',function($scope) {
$scope.edit_song = {};
$scope.song = [
{name:'Heartbreaker',artist:'Led Zeppelin',genre:'Rock',price:'.99'},
{name:'War Pigs',artist:'Black Sabbath',genre:'Rock',price:'.99'}
];
$scope.applySong = function(song) {
$scope.edit_song = angular.copy(song);
$scope.edit_song.song_index = $scope.song.indexOf(song);
};
$scope.saveSong = function() {
var idx = $scope.edit_song.song_index;
$scope.song[idx] = $scope.edit_song;
$scope.cancelSong();
};
$scope.cancelSong = function() {
$scope.edit_song = {};
};
});

Resources