Using separate model for each column in table ng-repeat - angularjs

I like to create a table. for each column in this table I like to use a different model. So if I drop a element in one of my two columns it should store the element in the model with respect to the column. Here is link to a fiddle page where I tried something. Is that the easiest way to do so?
<div ng-app="myApp" ng-controller="MyCtrl">
<ul>
<li ng-repeat="person in data">
<div data-drag="true" data-jqyoui-options="{revert: 'invalid', helper: 'clone'}" jqyoui-draggable="{animate: true, placeholder: 'keep'}" ng-model="person">
{{ person.name }}
</div>
</li>
</ul>
<table >
<tr style="background-color:orange;">
<td ng-model="src" data-drop="true" jqyoui-droppable="{multiple:true, onDrop: 'myDrop'}">Add your items here</td>
<td ng-model="tar" data-drop="true" jqyoui-droppable="{multiple:true, onDrop: 'myDrop'}">Add your items here</td>
</tr>
<tr ng-repeat="person in src track by $index">
<td >{{ person.name }}</td>
<td >{{ tar[$index].name }}</td>
</tr>
</table>
</div>
<script>
var myApp = angular.module('myApp', ['ngDragDrop']);
myApp.controller('MyCtrl', function($scope, $timeout) {
var src = [{
name: 'bob'
}, {
name: 'mary'
}];
var tar = [{
name: 'bob'
}, {
name: 'mary'
}];
$scope.people = [];
$scope.people.src = [];
$scope.people.tar = [];
$scope.data = src;
$scope.hideMe = function() {
return $scope.people.src > 0;
}
$scope.myDrop = function(event, ui) {
console.log($scope.people);
}
});
</script>

Related

Highlight text in grid using AngularJS filter + ng-repeat in table data

I want to highlight search text under ng-repeat which is creating table.
I found one filter as my solution but it's not working on tables.
Can anyone points me where I am wrong to make it work.
<body ng-app="Demo">
<h1>Hello Plunker!</h1>
<div class="container" ng-controller="Demo">
<input type="text" placeholder="Search" ng-model="search">
<table>
<thead>
<th>Some</th>
<th>MyName</th>
</thead>
<tbody>
<tr ng-repeat="item in data | filter:search"
>
<td>
<td>{{item.title}}</td>
<td>{{item.name}}</td>
</td>
</tr>
</tbody>
<!-- with filter -->
</table>
</div>
<script>
angular.module('Demo', [])
.controller('Demo', function($scope) {
$scope.data = [
{ title: "Bad",name:'kaushik' },
{ title: "Good",name:'1kaushik' },
{ title: "Great",name:'2kaushik' },
{ title: "Cool" ,name:'3kaushik'},
{ title: "Excellent",name:'4kaushik' },
{ title: "Awesome",name:'5kaushik' },
{ title: "Horrible",name:'6kaushik' },
]
})
.filter('highlight', function($sce) {
return function(text, phrase) {
if (phrase) text = text.replace(new RegExp('('+phrase+')', 'gi'),
'<span class="highlighted">$1</span>')
return $sce.trustAsHtml(text)
}
})
</script>
</body>
This is working copy of the & demo the same thing I wants is with table.
You're not applying the highlight filter and you also have nested <td> elements. Try this:
<tr ng-repeat="item in data | filter:search">
<td ng-bind-html="item.title | highlight:search></td>
<td ng-bind-html="item.name | highlight:search"></td>
</tr>
angular.module('Demo', [])
.controller('Demo', function($scope) {
$scope.data = [{
title: "Bad",
name: 'kaushik'
},
{
title: "Good",
name: '1kaushik'
},
{
title: "Great",
name: '2kaushik'
},
{
title: "Cool",
name: '3kaushik'
},
{
title: "Excellent",
name: '4kaushik'
},
{
title: "Awesome",
name: '5kaushik'
},
{
title: "Horrible",
name: '6kaushik'
},
]
})
.filter('highlight', function($sce) {
return function(text, phrase) {
if (phrase) {
text = text.replace(new RegExp('(' + phrase + ')', 'gi'),
'<span class="highlighted">$1</span>');
}
return $sce.trustAsHtml(text)
}
});
.highlighted {
background-color: yellow;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.2/angular.min.js"></script>
<body ng-app="Demo">
<h1>Hello Plunker!</h1>
<div class="container" ng-controller="Demo">
<input type="text" placeholder="Search" ng-model="search">
<table>
<thead>
<th>Some</th>
<th>MyName</th>
</thead>
<tbody>
<tr ng-repeat="item in data | filter:search">
<td>
<td ng-bind-html="item.title | highlight:search"></td>
<td ng-bind-html="item.name | highlight:search"></td>
</td>
</tr>
</tbody>
<!-- with filter -->
</table>
</div>
<script>
</script>
</body>

AngularJS Table Custom Search Filter Per Column

I am looking for help in creating a custom filter that will work on any table that has a search property.
So far, I can get the table to filter based on what is input into each column search bar, but I can't figure out how to implement a 'startsWith' for each column as well so that it will only find 'Florida' if you type 'Flor' or 'Fl', etc. instead of finding it when typing 'lor' or 'ida'.
I have been able to get just one column working with a custom filter, but lost on how to implement this for multiple columns.
Here is the plunkr example: http://plnkr.co/edit/CE3uhZmksiepmVL2bLNF?p=preview
Script:
var app = angular.module("stateManagement", []);
app.controller("myCtrl", ["$scope", myCtrl]);
function myCtrl($scope) {
$scope.names = [{
Name: "Florida",
Country: "USA"
}, {
Name: "Texas",
Country: "USA"
}]
$scope.state = '';
$scope.elements = [{
state: "Florida"
}, {
state: "Texas"
}];
}
app.filter('myfilter', function() {
function strStartsWith(str, prefix) {
return (str.toLowerCase() + "").indexOf(prefix.toLowerCase()) === 0;
}
return function(items, state) {
var filtered = [];
angular.forEach(items, function(item) {
if (strStartsWith(item.state, state)) {
filtered.push(item);
}
});
return filtered;
};
});
Html:
<body ng-app='stateManagement' ng-controller='myCtrl'>
<div class="col-md-12">
<table class="table table-responsive">
<thead>
<tr>
<th>Name</th>
<th>Country</th>
</tr>
<tr>
<th><input class="form-control"
type="text"
placeholder="Search..."
ng-model="search.Name"/>
</th>
<th><input class="form-control"
type="text"
placeholder="Search..."
ng-model="search.Country"/>
</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="x in names | filter:search">
<td>{{ x.Name }}</td>
<td>{{ x.Country }}</td>
</tr>
</tbody>
</table>
</div>
<div class="col-md-12">
<input ng-model="state">
<table id="hotels">
<tr data-ng-repeat="element in elements | myfilter:state">
<td>{{element.state}}</td>
</tr>
</table>
<br/>
</div>
</body>
Thank you in advance for helping!

Angularjs rename items in an array

Adding items from one array to a new array using:
$scope.listItems = [];
$scope.addToList = function(item) {
$scope.listItems.push(item);
console.log($scope.listItems);
};
<tr ng-repeat="x in data">
<td><{{ x.id }}</td>
<td><button type="button" ng-click="addToList(x.id)">Add to</button></td>
</tr>
Then printing the new array:
<tr ng-repeat="item in listItems">
<td>{{item.id}}</td>
</tr>
Would it be possible to change the attribute names of the new listItems array using user input?
Sure its possible. But not the way your code is written. You need to pass the object to the function, not the id.
<tr ng-repeat="x in data">
<td>{{ x.id }}</td>
<td><input type="text" ng-model="newVal"/></td> <!--this property can be changed by user-->
<td><button type="button" ng-click="addToList(x, newVal)">Add to</button></td>
</tr>
and in the controller function:
$scope.addToList = function(item, newVal) {
var newItem = item;
newItem.id = newVal;
$scope.listItems.push(item);
console.log($scope.listItems);
};
You could definitely do that, But for that you need to pass in the object in itself not x.id.
Here is a sample working solution.
var app = angular.module("sampleApp", []);
app.controller("sampleController", ["$scope",
function($scope) {
$scope.data = [{
id: "City-1"
}, {
id: "City-2"
}, {
id: "City-3"
}, {
id: "City-4"
}, {
id: "City-5"
}];
$scope.listItems = [];
$scope.addToList = function(item) {
$scope.listItems.push(item);
};
}
]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="sampleApp">
<div ng-controller="sampleController">
<table>
<tr ng-repeat="x in data">
<td>
{{ x.id }}</td>
<td>
<button type="button" ng-click="addToList(x)">Add to</button>
</td>
</tr>
</table>
New List:
<table>
<tr ng-repeat="item in listItems track by $index">
<td>
{{ item.id }}</td>
<td>
<button type="button" ng-click="addToList(x)">Add to</button>
</td>
</tr>
</table>
</div>
</div>

How to Sort the Auto Increment Serial number Column in angularJS Table?

How to Sort the Auto Increment Serial number Column in angularjs Table ?
I'm having Serial Number Column with Auto Increament, I need to Sort the Column.
My Source Code is
<!DOCTYPE html>
<html>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>
<div ng-app="myApp" ng-controller="customersCtrl">
<table>
<thead>
<tr>
<th>S.No.</th>
<th>Name</th>
<th>D.O.B.</th>
</tr>
</thead>
<tr ng-repeat="x in names | orderBy:orderByField:reverseSort">
<td>{{ $index + 1 | number }}</td>
<td>{{ x.Name }}</td>
<td><span ng-if="x.DOB">{{ formatDate(x.DOB) | date:'dd-MM-yyyy' }}</span></td>
</tr>
</table>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $http) {
$scope.names = [
{ Name: 'Jani', DOB: '' },
{ Name: 'Hege', DOB: '1995-05-07' },
{ Name: 'Kai', DOB: '1995-08-24' }
];
$scope.formatDate = function (date) {
return new Date(date);
};
});
</script>
</body>
</html>
The Auto Increment Part is {{ $index + 1 }} within <td> tag.
In Angular 2+ you can use like this:
<div *ngFor="let item of items; let ndx = index">
<div>{{ndx+1}}</div>
</div>
angular.module('myApp', []).controller('namesCtrl', function($scope) {
$scope.names = [
{name:'Jani',country:'Norway'},
{name:'Carl',country:'Sweden'},
{name:'Margareth',country:'England'},
{name:'Hege',country:'Norway'},
{name:'Joe',country:'Denmark'},
{name:'Gustav',country:'Sweden'},
{name:'Birgit',country:'Denmark'},
{name:'Mary',country:'England'},
{name:'Kai',country:'Norway'}
];
$scope.orderByMe = function(x) {
$scope.myOrderBy = x;
}
});
table tr th{
cursor:pointer;
}
table tr th:hover{
background:#ddd;
}
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<body>
<p>Click the table headers to change the sorting order:</p>
<div ng-app="myApp" ng-controller="namesCtrl">
<table border="1" width="100%">
<tr>
<th ng-click="orderByMe('num')">Sl.No</th>
<th ng-click="orderByMe('name')">Name</th>
<th ng-click="orderByMe('country')">Country</th>
</tr>
<tr ng-repeat="x in names | orderBy:myOrderBy">
<td>{{$index + 1}}</td>
<td>{{x.name}}</td>
<td>{{x.country}}</td>
</tr>
</table>
</div>
</body>
</html>
You can use like this
<tr *ngFor="let user of userList ;let i = index">
<td>{{i+1}}</td>
<td>{{user.firstName}}</td>
<td>{{user.lastName}}</td>
<td>{{user.userName}}</td>
<td>{{user.email}}</td>
</tr>

Angular js : not able to iterate through ng-init dynamic data

I am new to the ANGULAR WORLD, please help
I have the following table row
<tr data-ng-repeat="obj in myObjs | filter:search">
<td><strong data-ng-hide="obj.editMode">{{ obj.ObjID }}</strong></td>
<td>
<p data-ng-hide="obj.editMode">{{ obj.Key }}</p>
<p data-ng-show="obj.editMode">
<input type="text" data-ng-model="obj.Name" />
</p>
</td>
<td>
<p data-ng-hide="obj.editMode">{{ obj.Value }}</p>
<div ng-init="objKVP = {{obj.Value}}">
<ul class="example-animate-container">
<li class="animate-repeat" ng-repeat="val in objKVP">
{{val.Key}} : {{val.Value}}
</li>
</ul>
</div>
</td>
At ng-init = objKVP, i'll get the value like
[{'Key':'test','Value':'https://google.com'},
{'Key':'test1','Value':'testval1'},
{'Key':'test2','Value':'t#testval2'},
{'Key':'test3','Value':'testval3'},
{'Key':'test4','Value':'testval3'}]
But when i try to get the Key and Value , its not populating inside the LI tag.
And if I post the above Key Value Pair directly inside the objKVP then it displays the Key and value inside the LI tag,
so the problem is with dynamic values, (though it shows up while i inspect the element)
help
You can try to use the example below:
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
<script>
var myApp = angular.module('myApp', []);
myApp.controller('myController', [
'$scope', function($scope) {
$scope.init = function() {
$scope.a = 'Apples';
$scope.b = 'Bees';
$scope.c = 'Zebras';
$scope.objKVP = [
{ 'Key': 'test', 'Value': 'https://google.com' },
{ 'Key': 'test1', 'Value': 'testval1' },
{ 'Key': 'test2', 'Value': 't#testval2' },
{ 'Key': 'test3', 'Value': 'testval3' },
{ 'Key': 'test4', 'Value': 'testval3' }
];
};
// runs once per controller instantiation
$scope.init();
}
]);
</script>
<body ng-controller="myController">
<h2>JavaScript Projects</h2>
<br>
<h2 ng-bind="a+ ' ' + b +' ' +c "></h2>
<table>
<tr ng-repeat="obj in objKVP | filter:search">
<td><strong ng-hide="obj.editMode">{{ obj.ObjID }}</strong></td>
<td width="100px;">
<p ng-hide="obj.editMode">{{ obj.Key }}</p>
</td>
<td width="200px;">
<p ng-hide="obj.editMode">{{ obj.Value }}</p>
<p ng-show="obj.editMode">
<input type="text" ng-model="obj.Name" />
</p>
</td>
</tr>
</table>
</body>

Resources