cant remove selected only from table - angular - angularjs

http://plnkr.co/edit/WFONNlAEIeN8K9DEJCqV?p=catalogue
i traying to delete/remove only the selected row
but no success.
remove function
$scope.removeTrainee = function(trainee){
var removeTrainee = $scope.trainees.indexOf(trainee);
$scope.trainees.splice(removeTrainee, 1);
};
the select function
$scope.selectedRow = null; // initialize our variable to null
$scope.setClickedRow = function(index){ //function that sets the value of selectedRow to current index
$scope.selectedRow = index;
};
tried to implement the selectedrow in ng-if or ng-click with && sign
no nothing
the remove function works, but not with selected one.
help please

To take the row selected you $scope.selectedRow so your funcion should be something like this:
$scope.removeTrainee = function(trainee){
var removeTrainee = $scope.trainees.indexOf(trainee);
$scope.trainees.splice($scope.selectedRow, 1);
};
Anyway I recommend you inplement the delete function in another way, more intuitive, for example adding a class to the selected row after clicking in it or adding a new row with a checkbox, to know which row is selected.
And you could also inprove your addTrainee function:
$scope.addTrainee = function(){
$scope.trainees.push(
$scope.newTreinee
);
$scope.newTreinee = {};
};
Hope this solution helps you.

Related

angularjs on form edit select option does not load value On Second Time

My form has got 2 select boxes. The second select options box is loaded when an option is selected in first box. The form is used for both adding and editing. I show the list below the form with edit button against each row. When I click on edit button it loads the values in form to edit. The thing is when I click on a row for first time it loads the values in form perfectly. If I click on another row button to edit, the SECOND select option alone does not show the selected value. But the box is loaded with values.
Code in controller:
$scope.editShift = function(row) {
for (var i = 0; i < $scope.availableShift.length; i++) {
if ($scope.availableShift[i].shiftId === row.entity.shiftId) {
$scope.shift = angular.copy($scope.availableShift[i]);
break;
};
};
$scope.selectLineNames();
};
$scope.selectLineNames = function(){
$scope.availableListOfLineNames = [];
$scope.availableListOfLineNamesTemp = LineDetails.ld_get_ln_resource.query
({plant: $scope.shift.plant}, function(response) {
angular.forEach(response, function(item) {
if (item) {
$scope.availableListOfLineNames.push(item);
}
});
});
};
Second Select Option in Form
<select name="lineName" ng-model="shift.lineName"
ng-options="x for x in availableListOfLineNames" id="selectoption2" required>
<option value="">Select One</option>
</select>
EDIT:
Calling $scope.selectLineNames(); inside $scope.editShift somehow makes the lineName value undefined after copy, on second time. Inside selectLineNames() too the value is available until it reaches the forEach() .Only after the loop starts It becomes undefined.
So used a duplicate method like $scope.selectLineNamesTemp(); and passed values from $scope.editShift and assigned them in the duplicate method like below.
$scope.selectLineNamesTemp = function(){
$scope.availableListOfLineNames = [];
$scope.availableListOfLineNamesTemp = LineDetails.ld_get_ln_resource.query(
{plant: plantTemp}, function(response) {
angular.forEach(response, function(item) {
if (item) {
$scope.availableListOfLineNames.push(item);
$scope.shift.lineName = lnTemp; // assigned from editShift
}
});
}) ;
};
Can someone point out why the value become undefined? Also is there a way to use the same old method without using the duplicate method. Hope someone help.

working on select but not on blur - EXTJS

I am newbie to ExtJS I have the following lines of code that is working fine on select event and now I am planning to add on blur event too.
autoResolve.on("select" || "blur", function (component, record, index) {
var fieldSet = utils.getComponentFromMngr(component.id.split("~")[0]);
if(autoResolveData.CURRSEL){ //Set previous selection property
var xmlElem = fieldSet.DomainXML.documentElement.childNodes[1];
xmlElem.setAttribute("PR_DOMAINTYPE",autoResolveData.FILL_SUBTYP);
xmlElem.setAttribute("PR_DOMAINID", record.get("ITEMID"));
xmlElem.setAttribute("PR_DOMAINVALUE", record.data.TITLE);
fieldSet.DomainObj.push({PRDomainType:autoResolveData.FILL_SUBTYP,PRDomainID:record.get("ITEMID"),PRDomainValue:record.data.TITLE});
}
it is still working fine on select event but not on blur event where am I going wrong please suggest
"select" || "blur" will return select, as you can find out if you type the following in browser console:
console.log("select" || "blur");
Furthermore, "blur" event does not have record as the second parameter. You would have to look how to get record and call the function with a valid record parameter.
What you want to achieve is roughly the following:
var myFunction = function (component, record, index) {
var fieldSet = utils.getComponentFromMngr(component.id.split("~")[0]);
if(autoResolveData.CURRSEL){ //Set previous selection property
var xmlElem = fieldSet.DomainXML.documentElement.childNodes[1];
xmlElem.setAttribute("PR_DOMAINTYPE",autoResolveData.FILL_SUBTYP);
xmlElem.setAttribute("PR_DOMAINID", record.get("ITEMID"));
xmlElem.setAttribute("PR_DOMAINVALUE", record.data.TITLE);
fieldSet.DomainObj.push({PRDomainType:autoResolveData.FILL_SUBTYP,PRDomainID:record.get("ITEMID"),PRDomainValue:record.data.TITLE});
}
};
autoResolve.on({
select:myFunction,
blur:function(component) {
var record = ... // your special magic here
return myFunction(component,record);
}
});

Set focus to newly added row in ui-grid

I am using ui-grid. The use-case is such that on clicking a button, a new row is added. I want to set focus on the first cell of the newly added row Any suggestions on how to do it ? Here's a plunkr for the same.
http://plnkr.co/edit/6zyZ5q
$scope.addNew = function() {
var newobj = {"id": "","name":"" };
$scope.gridOptions.data.unshift(newobj);
$scope.gridApi.core.notifyDataChange(uiGridConstants.dataChange.ROW);
//set focus to the new row
$scope.gridApi.cellNav.scrollToFocus( $scope.gridOptions.data[0], $scope.gridOptions.columnDefs[0]);
};
$timeout(function() {
$scope.gridApi.cellNav.scrollToFocus(
$scope.termGridOptions.data[0],
$scope.termGridOptions.columnDefs[0]
);
});
I hope it helps.

custom pagination to table using Angular JS

I'm trying to add pagination for my code. I'm able to add number of rows after clicking
add button. But, getting confused how to apply pagination custom.
Any advice on how to implement?
This is my fiddle http://jsfiddle.net/H2ps5/185/
var myApp = angular.module('myApp',[]);
myApp.controller('ctrl',function($scope){
$scope.firstname = 'Nizam'; // Set First Name on Page Load.
$scope.lastname = 'Ali'; // Set Last Name on Page Load.
$scope.users = []; // Define "users" Array.
/**
* Adding a new user.
*/
$scope.newuser = function(){
if($scope.firstname != "" && $scope.lastname != ""){ // If Values are not Empty.
this.users.push( // Push the values of First Name & Last Name in "users" Array.
{
firstname:$scope.firstname, // Key value pair.
lastname:$scope.lastname
}
);
$scope.firstname = ''; // Clear First Name
$scope.lastname = ''; // Clear Last Name
} else {
alert("Please fill values");
}
}
$scope.remove = function(user){ // Removing the user.
$scope.users.splice($scope.users.indexOf(user),1);
}
});
Kindly give me suggestions in which way I need to go
Diretive should be a way.
Make your own directive to handle how to manipulate the DOM when number of users in the scope gets to 10 or 20 or whatever you want.
Maybe another one for nav links between pages.

Remove item from list after filtering

I have the following issue:
I've create a list that allow the user to delete an item from list, as following:
When user click on trash icon, the item is removed normally.
The problem is when the user uses the filter on top.
In that case, if I delete the number 6565 (index 4 in original list, 1 on filtered list), the item deleted is on index 1 on original list, resulting on delete the register with number #564456
This is my delete call on click:
$scope.deleteOwn = function (uuid) {
console.log(uuid);
var coupon = $scope.ownsCoupons[uuid];
Coupon.delete({'id' : coupon.uuid}, function () {
$scope.ownsCoupons.splice(uuid, 1);
});
}
And this is my html template:
<td></i></td>
I also try to use the code: $scope.ownsCoupons.splice(coupon, 1);without success.
Does anyone know how to fix that?
I've coded using the following reference: AngularJS How to remove an Item from scope
[EDIT]
I've created a Plunker to this: http://plnkr.co/edit/Fhxp6uZyTJCY05CAQ7yA?p=preview
As mentioned by #pkozlowski.opensource, you can't depend on $index to identify an item in an array in this way. I would make the following changes:
HTML:
<td><a ng-click="deleteWish(coupon)"><i class="icon-trash"></i></a></td>
JS:
$scope.deleteWish = function (coupon) {
var index = $scope.coupons.indexOf(coupon);
if (index != -1) {
$scope.coupons.splice(index, 1);
}
}
Here is a working Plunker: http://plnkr.co/edit/b0b2cYGsM5wtw8hIrQB5?p=preview

Resources