this is the code in html page, need code for both component and service please help for this problem ** multiple deletion by using check box in angular 2 **i posted only html code but i dont know code for both components and service here the code have using single deletions but i need multiple deletions
<div class="card-content table-responsive" *ngIf="this.pagedItems!=null && this.pagedItems.length > 0">
<table class="table">
<thead>
<tr>
<th class="text-danger" style="font-weight: bold">Sl NO</th>
<!-- <th class="text-danger" style="font-weight: bold">Date</th>-->
<th class="text-danger" style="font-weight: bold">Income/Expense</th>
<th class="text-danger" style="font-weight: bold">Payment Mode</th>
<th class="text-danger" style="font-weight: bold">Debtor</th>
<th class="text-danger" style="font-weight: bold">Remarks</th>
<th class="text-danger" style="font-weight: bold">Amount</th>
<th class="text-danger" style="font-weight: bold"></th>
</tr>
</thead>
<tbody>
<tr *ngFor="let item of this.pagedItems | paginate: { itemsPerPage: 500, currentPage: p }; let i = index" >
<td>{{i+1}}</td>
<!-- <td>{{item.date_of_entry}}</td>-->
<td>{{item.income_expense}}</td>
<td>{{item.payment_mode}}</td>
<td>{{item.creditor_fname}}</td>
<td>{{item.remarks}}</td>
<td>{{item.amount}}</td>
<td>{{item.entry_id}}</td>
<td><input type="checkbox" name="deletecheck"></td>
<td class="td-actions text-right">
<button type="button" rel="tooltip" title="Edit" class="btn btn-primary btn-simple btn-xs" (click) = "editSales({entryid:item.entry_id,InExp:item.income_expense,Amt:item.amount,Paymode:item.payment_mode,Debtr:item.creditor_id,Vehic:item.vechicle_no,Remarks:item.remarks})">
<i class="material-icons">edit</i>
</button>
</td>
</tr>
<button (click)="deleteSelected()">Delete</button>
</tbody>
</table>
<br>
<pagination-controls (pageChange)="p = $event"></pagination-controls>
</div>
Add an array of selected items in the component:
selectedItems: Item[];
Add another button to (de)select items:
<button type="button" rel="tooltip" title="Select" class="btn btn-info btn-simple btn-xs" (click) = "selectSales({entryid:item.entry_id})">
<i class="material-icons">close</i>
</button>
In component:
selectSales(item) {
const indexFound = this.selectedItems.indexOf(selected => selected.id === item.id;
if (indexFound > -1) {
this.selectedItems.splice(indexFound, 1);
} else {
this.selectedItems.push(item);
}
}
deleteSelected() {
this.selectedItems.forEach(item => {
// delete each item
});
}
Related
My code is like this: when you check the 'hide' column, the whole row is hidden. I also have a 'Show All' checkbox. When I check it, all rows (including the hidden ones) are shown. But when I uncheck it, all rows disappear. How can I make it so only the hidden rows disappear when I uncheck 'Show All', and the other rows still remain visible?
<body ng-app="myApp" ng-controller="myController">
<h1>Fruits in Vietnam</h1>
<hr>
<form name="myForm">
<div class="input-group input-group-lg">
<input type="text" class="form-control" name="mdfruitname" id="txtfruitname" ng-model="mdfruitname"
required>
<div class="input-group-append">
<button class="btn btn-outline-secondary" type="button" ng-click="addnew()"
ng-disabled="myForm.mdfruitname.$pristine || myForm.mdfruitname.$invalid">Add
</button>
</div>
</div>
</form>
</div>
<table class="table table-striped">
<thead>
<tr>
<th>Description</th>
<th>Hide</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="fruit in fruitnameDetail" ng-show="showall" ng-hide="hidef">
<td>{{fruit.fruitname}}</td>
<td><input type="checkbox" ng-model="hidef"></td>
<td>
<a class="btn btn-xs delete-record" ng-click="removefruitname($index)">
<i class="glyphicon glyphicon-trash"></i>
</a>
</td>
</tr>
</tbody>
</table>
<div><input type="checkbox" ng-model="showall"><span>SHOW ALL</span></div>
</body>
<script>
var app = angular.module("myApp", []);
app.controller('myController', function ($scope, $window) {
$scope.fruitnameDetail = [
{
"fruitname": "Banana"
},
{
"fruitname": "Mango"
},
{
"fruitname": "Orange"
}
];
$scope.addnew = function () {
var fruitnameDetail = {
fruitname: $scope.mdfruitname
};
$scope.fruitnameDetail.push(fruitnameDetail);
$scope.mdfruitname = '';
};
$scope.removefruitname = function (index) {
var name = $scope.fruitnameDetail[index].fruitname;
if ($window.confirm("Do you want to delete " + name + " ?")) {
$scope.fruitnameDetail.splice(index, 1);
}
};
});
</script>
Do not use ng-show and ng-hide at the same time. Just change a little bit your condition for ng-repeat. Here the example that should work:
<body ng-app="myApp" ng-controller="myController">
<h1>Fruits in Vietnam</h1>
<hr>
<form name="myForm">
<div class="input-group input-group-lg">
<input type="text" class="form-control" name="mdfruitname" id="txtfruitname" ng-model="mdfruitname"
required>
<div class="input-group-append">
<button class="btn btn-outline-secondary" type="button" ng-click="addnew()"
ng-disabled="myForm.mdfruitname.$pristine || myForm.mdfruitname.$invalid">Add
</button>
</div>
</div>
</form>
</div>
<table class="table table-striped">
<thead>
<tr>
<th>Description</th>
<th>Hide</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="fruit in fruitnameDetail" ng-show="showall || !hidef">
<td>{{fruit.fruitname}}</td>
<td><input type="checkbox" ng-model="hidef"></td>
<td>
<a class="btn btn-xs delete-record" ng-click="removefruitname($index)">
<i class="glyphicon glyphicon-trash"></i>
</a>
</td>
</tr>
</tbody>
</table>
<div><input type="checkbox" ng-model="showall"><span>SHOW ALL</span></div>
</body>
In my angularjs app there a rows (using ng-repeat), created through .
The code creating rows is below:
<tbody>
<tr ng-repeat="data in downloads">
<td data-title="ID">{{$index + 1}}</td>
<td data-title="User">{{data.user_name}}</td>
<td data-title="Issue">{{data.download_name}}</td>
<td data-title="Progress">{{data.size}}</td>
<td data-title="Completed time" am-time-ago="data.completed_time|amFromUnix"</td>
<td class="information-parent" data-title="More">
<md-icon ng-click="switchIcon()" class="bassa-red-color">{{icon}}</md-icon
</td>
</tr>
</tbody>
I'm trying to implement something that when you click the arrow, the row expands with information appearing like this:
I however can not get the information to appear along the bottom like that. What should I be trying to implement to get text along the bottom - flex box, inline-box ?
Take a look at Bootstrap collapse.js. I think this is what you are looking for, of course, you have to change it in order to meet your needs.
Try below code for your scenario.
(function(ng, app){
app = angular.module('app', [])
app.controller("mainController", ['$scope',
function($scope) {
$scope.downloads = [{
"user_name": "John",
"download_name": "Doe jhonejhonejhone",
"size": "0 byte"
},
{
"user_name": "Anna",
"download_name": "Doe DoeDoeDoeDoe",
"size": "0 byte"
},
{
"user_name": "Maron",
"download_name": "Anna DoeDoeDoeDoeDoe",
"size": "5 byte"
}
];
$scope.editFiles = function() {
$scope.editBar = true;
};
$scope.remove = function(index) {
$scope.downloads.splice(index, 1);
};
}
]);
}(angular));
.btn-primary{
margin-right: 10px;
}
<html ng-app="app">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.0/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container" ng-controller="mainController">
<div class="row">
<div class="col-md-12">
<input type="submit" class="btn btn-primary addnew pull-left" value="Edit Files" ng-click="editFiles()">
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="panel panel-default">
<div class="panel-body">
<form>
<table border=1 class="table table-striped table-bordered">
<thead>
<tr>
<th>
<b> # </b>
</th>
<th>User</th>
<th>Download Name</th>
<th>Complete Name</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="data in downloads">
<td>
<b> {{$index + 1}} </b>
</td>
<td>
{{data.user_name}}
</td>
<td>
{{data.download_name}}
<div class="btn-group pull-right" ng-show="editBar">
<button class="btn btn-primary btn-xs"><span class="glyphicon glyphicon-pencil"></span></button>
<button class="btn btn-danger btn-xs"><span class="glyphicon glyphicon-trash" ng-click="remove($index)"></span></button>
</div>
</td>
<td>
{{data.size}}
</td>
</tr>
</tbody>
</table>
</form>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
You can try the following:
move your ng-repeat to your tbody
add ng-init="data.showSubRow = false" to your tbody
add another row to the tbody that should only be visible when showSubRow is true
so your ng-repeat would look like
<tbody ng-repeat="data in downloads" ng-init="data.showSubRow = false">
<tr>
visible row
</tr>
<tr ng-show="data.showSubRow">
visible when clicked
</tr>
</tbody>
now you can add individual buttons in each row to show / hide content on individual rows or add an edit all function that would make all subrows visible
show / hide individual row
<tr>
<td data-title="ID">
{{$index + 1}
<span>
<button ng-show="!data.showSubRow" ng-click="data.showSubRow = true">+</button>
<button ng-show="data.showSubRow" ng-click="data.showSubRow = false">-</button>
</span>
</td>
<td data-title="User">{{data.user_name}}</td>
<td data-title="Issue">{{data.download_name}}</td>
<td data-title="Progress">{{data.size}}</td>
<td data-title="Completed time" am-time-ago="data.completed_time|amFromUnix"</td>
<td class="information-parent" data-title="More">
<md-icon ng-click="switchIcon()" class="bassa-red-color">{{icon}}</md-icon
</td>
</tr>
display all
$scope.editAll = function(){
angular.forEach($scope.downloads, function(value, key) {
value.showSubRow = !value.showSubRow;
});
}
Working sample ----> Demo
View of the page
I attached the image of the view. I want to iterate the two ngfor loops inside the tr tag. I used the div tag inside the tr but it's looping the button next to the tag. I want that in file table header (please see the image file for understanding)...
Table section:
this is the table section of my HTML page.
<table class="table">
<thead>
<tr>
<th scope="col">Category</th>
<th scope="col">Created By</th>
<th scope="col">Date Created</th>
<th scope="col">File Name</th>
<th scope="col">File</th>
</tr>
</thead>
<tbody>
<ng-template [ngTemplateOutlet]="tmplt"></ng-template>
</tbody>
</table>
<input class="btn btn-primary" type="button" value="Save" />
<input class="btn btn-danger" type="button" value="Cancel" />
</form>
</div>
</div>
</div>
<ng-template #tmplt>
<tr *ngFor="let cattype of categorytype">
<td>{{cattype.Name}}</td>
<div *ngFor="let manage of managecontent">
<td>{{manage.CreatedBy}}</td>
<td>{{manage.CreatedDate}}</td>
<td>{{manage.DocumentDetails.DocName}}</td>
<td><app-file-upload [documentModel]="manage.DocumentDetails" [isMultipleFile]="true" [model]="manage" (emitterFile)="fileSelect($event)"></app-file-upload></td>
<td><input class="btn btn-danger" type="button" value="Remove" /></td>
</div>
</tr>
</ng-template>
Full Html Template:
<div class="text-center">
<div class="container">
<div class="jumbotron">
<form novalidate>
<h3>Manage Content</h3>
<br>
<h4>Select Gender:</h4>
<label class="radio-inline"><input type="radio" #gender name="gender" value="1" checked>Male</label>
<label class="radio-inline"><input type="radio" #gender name="gender" value="2">Female</label>
<br>
<br>
<h4>Select Routing Type:</h4>
<div class="row">
<div class="col-sm-3"></div>
<div class="col-sm-6">
<select class="form-control">
<option>Choose Route Type</option>
<option *ngFor="let manage of dropdown" value="{{manage.Id}}">{{manage.Name}}</option>
</select>
</div>
</div>
<br>
<br>
<table class="table">
<thead>
<tr>
<th scope="col">Category</th>
<th scope="col">Created By</th>
<th scope="col">Date Created</th>
<th scope="col">File Name</th>
<th scope="col">File</th>
</tr>
</thead>
<tbody>
<ng-template [ngTemplateOutlet]="tmplt"></ng-template>
</tbody>
</table>
<input class="btn btn-primary" type="button" value="Save" />
<input class="btn btn-danger" type="button" value="Cancel" />
</form>
</div>
</div>
</div>
<ng-template #tmplt>
<tr *ngFor="let cattype of categorytype">
<td>{{cattype.Name}}</td>
<div *ngFor="let manage of managecontent">
<td>{{manage.CreatedBy}}</td>
<td>{{manage.CreatedDate}}</td>
<td>{{manage.DocumentDetails.DocName}}</td>
<td><app-file-upload [documentModel]="manage.DocumentDetails" [isMultipleFile]="true" [model]="manage" (emitterFile)="fileSelect($event)"></app-file-upload></td>
<td><input class="btn btn-danger" type="button" value="Remove" /></td>
</div>
</tr>
</ng-template>
TS File:
import { Component, OnInit, TemplateRef, ViewChild } from "#angular/core";
import { ManageContentService } from "../Service/managecontent.service";
import { ManageContentModel, dropdownmodel, categorytypemodel } from "../Model/managecontent.model";
import { DocumentDetails } from "../Model/document.model";
#Component({
selector: 'manage-content',
templateUrl: './app/QadAdminConfig/Templates/managecontent.component.html',
providers: [ManageContentService]
})
export class ManageContentComponent implements OnInit {
data: any;
categorytype: Array<categorytypemodel> = [];
dropdown: Array<dropdownmodel> = [];
managecontent: Array<ManageContentModel> = [];
#ViewChild("tmplt") tmpltTbl: TemplateRef<any>;
ngOnInit()
{
this.getRouType();
this.getcontenttype();
}
constructor(private _managecontentService: ManageContentService)
{
let model: ManageContentModel = new ManageContentModel();
model.DocumentDetails = new DocumentDetails();
model.DocumentDetails.Id = 0;
this.managecontent.push(model);
}
getRouType() {
this._managecontentService.GetRouteType().subscribe(
data => {
if (data.Success) {
this.dropdown = data.Result as dropdownmodel[];
console.log(this.dropdown);
}
});
}
getcontenttype() {
this._managecontentService.GetContent().subscribe(
data => {
if (data.Success) {
this.categorytype = data.Result as categorytypemodel[];
console.log(this.categorytype);
console.log(this.managecontent);
}
});
}
getdata() {
}
}
Model:
import { BaseModel } from "./base.model";
import { DocumentDetails } from "./document.model";
export class ManageContentModel extends BaseModel {
RouteTypeId: number;
CategoryTypeId: number;
Gender: number;
DocId: number;
DocumentDetails: DocumentDetails;
}
export class dropdownmodel {
Id: number;
Name: string;
Name_AR: string;
}
export class categorytypemodel {
Id: number;
Name: string;
Name_AR: string;
}
You can use ng-template syntax
For more information : https://toddmotto.com/angular-ngfor-template-element#ngfor-and-ng-template
<tr *ngFor="let cattype of categorytype">
<td>{{cattype.Name}}</td>
<ng-template ngFor let-manage [ngForOf]="managecontent | async">
<td>{{manage.CreatedBy}}</td>
<td>{{manage.CreatedDate}}</td>
<td>{{manage.DocumentDetails.DocName}}</td>
<td><app-file-upload [documentModel]="manage.DocumentDetails" [isMultipleFile]="true" [model]="manage" (emitterFile)="fileSelect($event)"></app-file-upload></td>
<td><input class="btn btn-danger" type="button" value="Remove" /></td>
</ng-template>
</tr>
I have a table which contains some data and some buttons which changes according to response and user.
Here's the table.
<div class="form-group">
<div class="col-lg-12 col-md-9">
<table id="basic-datatables" class="table table-bordered" cellspacing="0" width="100">
<thead style="text-align:center">
<tr>
<th rowspan="1" colspan="1" style="width:100px; text-align:center">Employee ID</th>
<th rowspan="1" colspan="1" style="width:100px; text-align:center">Employe Name</th>
<th rowspan="1" colspan="1" style="width:100px; text-align:center">Email</th>
<th rowspan="1" colspan="1" style="width:100px; text-align:center">Department</th>
<th rowspan="1" colspan="1" style="width:100px; text-align:center">Date Created / Joined</th>
<th rowspan="1" colspan="1" style="width:110px; text-align:center">Actions</th>
</tr>
</thead>
<tbody style="text-align:match-parent">
<tr ng-repeat="data in details = (FilterDetails | limitTo:itemsPerPage:(currentPage-1)*itemsPerPage)">
<td style="text-align:center">{{data.Employee.Empid}}</td>
<td style="text-align:center">{{data.Employee.Fname}} {{data.Employee.Lname}}</td>
<td style="text-align:center">{{data.Employee.Email}}</td>
<td style="text-align:center">{{data.Department.DeptName}}</td>
<td style="text-align:center">{{data.Employee.Date_of_join | dateFormat}}</td>
<td style="text-align:center; width:100px">
<div>
<button type="submit" class="btn btn-success pad btn-sm" ng-click="Generate(data)">Generate</button>
<!--<button type="submit" class="btn btn-success pad btn-sm" ng-click="state = true" ng-hide="state">Generate</button>-->
<button type="submit" class="btn btn-warning btn-sm" ng-show="data.UserLogin.Status=='pending'">Pending</button>
<button type="submit" class="btn btn-default btn-sm" ng-show="data.UserLogin.Statuss=='pending'">Retry</button>
</div>
</td>
</tr>
</tbody>
</table>
<p ng-show="details.length == 0">No Users found.</p>
<div class="col-md-8 col-xs-12">
<uib-pagination total-items="totalEmployees" ng-model="currentPage" ng-change="pageChanged()" max-size="maxSize" boundary-links="true" class="pagination" items-per-page="itemsPerPage"></uib-pagination>
</div>
</div>
</div>
What I need to get done is that when the response comes from the JSON data the buttons have to change. Now according to this;
Generate button should show whendata.UserLogin.Status == ''(empty)
Pending button should show when data.UserLogin.Status == 'pending'
Retry button should show when data.UserLogin.Status == 'pending'
How do I achieve this. Help woud be appreciated.
You can use ng-show ng-hide or ng-if like below,
<button class="btn btn-default" ng-show="data.UserLogin.Status == ''">Generate </button>
or
<button class="btn btn-default" ng-hide="data.UserLogin.Status != ''">Generate </button>
or
<button class="btn btn-default" ng-if="data.UserLogin.Status == ''">Generate </button>
You should go for ng-if instead of ng-show or ng-hide. Because in case of ng-show or ng-hide based on the flag the content is loaded in the DOM. so if u try to change the value of flag by using web developer of browser u can be able to see the actual content. But if u go for ng-if, the content is loaded at runtime only when the condition is true. so whenever u want a quicker performance you can go for ng-show or ng-hide but if we want runtime behavior u should use ng-if.
You can write code as below..
<button class="btn btn-default" ng-show="data.UserLogin.Status == ''">Generate </button>
<button class="btn btn-default" ng-show="data.UserLogin.Status == 'pending'">Pending </button>
<button class="btn btn-default" ng-show="data.UserLogin.Status == 'pending'">Retry</button>
We can use ng-if,ng-show,ng-hide like
<button type="submit" class="btn btn-success pad btn-sm" ng-click="Generate(data)" ng-if="!data.UserLogin.Status">Generate</button> <button type="submit" class="btn btn-success pad btn-sm" ng-click="Pending (data)" ng-if="!data.UserLogin.Status">Pending </button>
use ng-if ,it is efficient
Use below code in the success function of the API. This code will run after you get the response and the DOM is created.
$scope.$evalAsync(function(){
$timeout(function() {
// Here set the scope variable and it will work in ng-show
}, 0); // wait...
});
Note: Do not forget to add $timeout in the controller.
i have a page that have two buttons Next and Previos. This buttons load a table in a PartialView.
My problem is when i press the button second time, it doesnt work.
Here is the code
The View :
<div id="Centros">
<div class="divPantalla2">
<table cellspacing="0" class="table table-hover table-responsive table-bordered">
<tr>
<th class="thTablas">
Nombre
</th>
<th class="thTablas">
Nro- Establecimiento
</th>
<th class="thTablas">
Departamento
</th>
<th class="thTablas">
Localidad
</th>
<th class="thTablas">
</th>
</tr>
#if (ViewBag.OnePageOfCEProyecto != null)
{
foreach (ANEP.Models.CentroEducativoModel item in ViewBag.OnePageOfCEProyecto)
{
<tr>
<td>
#item.Nombre
</td>
<td>
#item.NroEstablecimiento
</td>
<td>
#if (item.dDepartamento != null)
{
#item.dDepartamento.Descripcion;
}
</td>
<td>
#if (item.dLocalidad != null)
{
#item.dLocalidad.NomLocalidad;
}
</td>
<td>
<button type="submit" value="Eliminar+#item.CEID" name="Command" class="btn_eliminar">
</button>
</td>
</tr>
}
}
</table>
</div>
<div id="botones">
#if (ViewBag.OnePageOfCEProyecto != null)
{
<div align="center">
<div align="center">
#if (Session["PROYECTO_PAGINA_ACTUAL"] != null && int.Parse(Session["PROYECTO_PAGINA_ACTUAL"].ToString()) > 1)
{
<button type="button" id="Primera" value="Primera1" name="Command" class="btn btn-primary">
<font color="#0C58A8"><<</font></button>
<button type="button" id="Anterior" value="Anterior1" name="Command" class="btn btn-primary">
<font color="#0C58A8"><</font></button>
}
<font color="#0C58A8">#int.Parse(Session["PROYECTO_PAGINA_ACTUAL"].ToString())</font>
#if (ViewBag.OnePageOfCEProyecto.Count == 8)
{
<button type="button" id="Siguiente" value="Siguiente1" name="Command" class="btn btn-primary">
<font color="#0C58A8">></font></button>
<button type="button" id="Ultima" value="Ultima1" name="Command" class="btn btn-primary">
<font color="#0C58A8">>></font></button>
}
</div>
</div>
}
</div>
</div>
The PartialView :
<div class="divPantalla2">
<table cellspacing="0" class="table table-hover table-responsive table-bordered">
<tr>
<th class="thTablas">
Nombre
</th>
<th class="thTablas">
Nro- Establecimiento
</th>
<th class="thTablas">
Departamento
</th>
<th class="thTablas">
Localidad
</th>
<th class="thTablas">
</th>
</tr>
#if (ViewBag.OnePageOfCEProyecto != null)
{
foreach (ANEP.Models.CentroEducativoModel item in ViewBag.OnePageOfCEProyecto)
{
<tr>
<td>
#item.Nombre
</td>
<td>
#item.NroEstablecimiento
</td>
<td>
#if (item.dDepartamento != null)
{
#item.dDepartamento.Descripcion;
}
</td>
<td>
#if (item.dLocalidad != null)
{
#item.dLocalidad.NomLocalidad;
}
</td>
<td>
<button type="submit" value="Eliminar+#item.CEID" name="Command" class="btn_eliminar">
</button>
</td>
</tr>
}
}
</table>
</div>
<div id="botones">
#if (ViewBag.OnePageOfCEProyecto != null)
{
<div align="center">
<div align="center">
#if (Session["PROYECTO_PAGINA_ACTUAL"] != null && int.Parse(Session["PROYECTO_PAGINA_ACTUAL"].ToString()) > 1)
{
<button type="button" id="Primera" value="Primera1" name="Command" class="btn btn-primary">
<font color="#0C58A8"><<</font></button>
<button type="button" id="Anterior" value="Anterior1" name="Command" class="btn btn-primary">
<font color="#0C58A8"><</font></button>
}
<font color="#0C58A8">#int.Parse(Session["PROYECTO_PAGINA_ACTUAL"].ToString())</font>
#if (ViewBag.OnePageOfCEProyecto.Count == 8)
{
<button type="button" id="Siguiente" value="Siguiente1" name="Command" class="btn btn-primary">
<font color="#0C58A8">></font></button>
<button type="button" id="Ultima" value="Ultima1" name="Command" class="btn btn-primary">
<font color="#0C58A8">>></font></button>
}
</div>
</div>
}
</div>
The AJAX function (only for the button next):
$(document).ready(function () {
$("#Siguiente").on("click", function () {
var accion = $('#Siguiente').val();
$.ajax({
url: "/Proyecto/TablaCentros",
type: "GET",
cache: false,
data: { command: accion }
})
.done(function (partialViewResult) {
$("#Centros").html(partialViewResult);
});
});
});
And the controller :
public ActionResult TablaCentros(string command, Proyecto p)
{
if (command != null)
{
if (command.Contains("Siguiente1"))
{
if (Session["REGISTRO_PROYECTO"] != null)
{
Proyecto model = p;
int page = (int)Session["PROYECTO_PAGINA_ACTUAL"];
page++;
Session["PROYECTO_PAGINA_ACTUAL"] = page;
List<CentroEducativoModel> lista = (List<CentroEducativoModel>)Session["PROYECTO_CENTRO_EDUCATIVOS"];
var onePageOfCEProyecto = lista.ToPagedList(page, 8); // will only contain 15 products max because of the pageSize
ViewBag.OnePageOfCEProyecto = onePageOfCEProyecto;
cargarEstadoProyecto();
cargarCentroEducativos();
cargarClaseProyecto();
cargarTipoProyecto();
return PartialView("_TablaCentros", model);
}
}
}
return PartialView("_TablaCentros");
}
Sorry for my English,
Thanks!!!
It looks like your buttons are part of your partial view. When you initially load your view (without ajax) your button is bound to your click handler and all is well. When you click that button, your handler fires, makes the request and then replaces the #Centros content with the response -- however your bound button is part of the content that is replaced and your "new" buttons aren't bound.
You will need to rebind your handlers to your buttons after the ajax request has completed and you've loaded the result. You have a couple of options -- you can move your script to your partial view so it would fire after it's loaded, you can call your binding code in your done() handler or you can change your selector for your existing code to use a delegate.
I would use the delegate which would look similar to this:
$("#Centros").on("click", "#Siguiente", function() { ... })
This essentially means that anytime a click event happens in #Centros to an element with an ID of #Siguiente then handle the event.
I hope what ny answer will help you. What I have noticed in the signature of your Action in the controller it is supposed to receive two arguments command and Proyecto but on your ajax function you just pass command:accion So your Action does not receive the object Proyecto.