Knockout Bindings issue - data-binding

I keep getting the error when I click on the button to load the data into the table. The data loads but chrome tools throw the error below.
You cannot apply bindings multiple times to the same element
The code I am running is as follows what do I have to change.
<!-- Nav tabs -->
<ul class="nav nav-tabs">
<li class="active">AM</li>
<li>CM</li>
<li>IM</li>
<li>KT</li>
</ul>
<!-- Tab panes -->
<div class="tab-content">
<div class="tab-pane active" id="1">
<table class="table table-hover">
<tr>
<td>A</td>
<td>B</td>
<td>C</td>
<td>D</td>
<td>E</td>
</tr>
</table>
</div>
<div class="tab-pane" id="2">
<table class="table table-hover">
<thead>
<tr>
<th>Title</th>
<th>Author</th>
<th>Link</th>
<th>Link1</th>
</tr>
</thead>
<tbody data-bind="foreach: Questions">
<tr>
<td data-bind="text: title"></td>
<td data-bind="text: author"></td>
<td>
<button type="button" class="btn btn-link">Link</button>
</td>
<td>
<button type="button" class="btn btn-link" data-bind="text: questionLink"> </button>
<!--<a class="btn" type="button" href="http://example.com">Default (Gray) Button</a>-->
<!--<a class="btn" data-bind="href: questionLink">Default (Gray) Button</a>-->
</td>
</tr>
</tbody>
</table>
</div>
<div class="tab-pane" id="3"></div>
<div class="tab-pane" id="4"></div>
<p><button id="clickMe" value="clickme" type="button" onclick="AddQuestions();">Load Questions</button></p>
<script type="text/javascript">
function AddQuestions() {
var self = this;
self.Questions = ko.observableArray();
self.Questions.push(new Question('1', '1', '1', '1', '1'));
self.Questions.push(new Question('2', '2', '2', '2', '2'));
ko.applyBindings();
function Question(label, id, title, author, questionLink) {
var self = this;
self.Label = ko.observable(label);
self.ID = ko.observable(id);
self.title = ko.observable(title);
self.author = ko.observable(author);
self.questionLink = ko.observable(questionLink);
}
}
</script>

The problem is you are calling ko.applyBindings more than once. You should only call this once per page.
function Questions() {
var self = this;
self.Questions = ko.observableArray();
self.addQuestions = function() {
self.Questions.push(new Question('1', '1', '1', '1', '1'));
self.Questions.push(new Question('2', '2', '2', '2', '2'));
};
function Question(label, id, title, author, questionLink) {
var self = this;
self.Label = ko.observable(label);
self.ID = ko.observable(id);
self.title = ko.observable(title);
self.author = ko.observable(author);
self.questionLink = ko.observable(questionLink);
}
}
ko.applyBindings(new Questions());
<button id="clickMe" value="clickme" data-bind="click: $root.addQuestions()">Load Questions</button></p>

You can call ko.applyBindings only once per DOM element.
Either call it once for the whole page with ko.applyBindings(viewmodel) or call it for the elements you want with ko.applyBindings(viewmodel, domElement).
Note that you can't call it on a jQuery object directly:
ko.applyBindings(viewmodel, $("#myObj"));
Won't work.
Use this instead if needed:
ko.applyBindings(viewmodel, $("#myObj").get(0));

Related

Navigation via button click in Angular 10

I have a list of names that I want to show only 5 of them on every page. Users can see the next 5 names by pressing “Next” button.
How can I set the click attribute of a button to do this for me in Angular.
Here is my .html file:
<div class="container">
<table class="table" >
<tbody *ngFor="let name of names.slice(index, index+ 5)">
<tr> <td>{{ name.value}} </td> </tr>
</tbody>
</table>
<button type="button" class="btn btn-primary" style ="float: left" (click)="previousFunction()"> < Previous </button>
<button type="button" class="btn btn-primary" style ="float: right" (click)="nextFunction()"> Next > </button>
</div>
Thanks in advance.
Well it was quiet easy to solve ...
Here is the html code:
<div class="container">
<table class="table" >
<tbody *ngFor="let name of names.slice(index, index+ 5)">
<tr> <td>{{ name.value}} </td> </tr>
</tbody>
</table>
<button type="button" class="btn btn-primary" style ="float: left" (click)="previousFunction($event, index)"> < Previous </button>
<button type="button" class="btn btn-primary" style ="float: right" (click)="nextFunction($event, index)"> Next > </button>
</div>
and here is the .ts code:
export class AppComponent {
constructor() { }
index: number = 0;
pageSize: number = 5;
names = [
{ name: 'a'},
{ name: 'b'},
{ name: 'c'},
{ name: 'd'},
{ name: 'e'},
{ name: 'f'},
{ name: 'g'},
{ name: 'h'},
{ name: 'i'},
{ name: 'j' },
];
previousFunction($event: any, count: any){
this.index = count - this.pageSize
}
nextFunction($event: any, count: any){
this.index = count + this.pageSize
}

Why am I getting 'TypeError: Cannot read property 'slice' of undefined'?

I am working on a small project. It is an ASP.NET MVC/Angular5 project. I am simply trying to populate a tableView (PrimeNG) with data that is selected from a Modal or PrimeNG Dialog.
Here is a snipit of the relevent ts file:
displayIndexDialog: boolean;
index: Index = {};
selectedIndex: Index;
newIndex: boolean;
indexes: Index[];
//METHODS
getIndexes() {
this._indexesService.getIndexes().subscribe(indexes => this.indexList = indexes);
}
showIndexAdd() {
this.newIndex = true;
this.selectedIndex = {};
this.displayIndexDialog = true;
this.getIndexes();
}
closeIndexDialog() {
this.displayIndexDialog = false;
}
saveIndex() {
let index = [...this.indexes];
if (this.newIndex) {
index.push(this.index);
}else
index[this.indexes.indexOf(this.selectedIndex)] = this.index;
this.indexes = index;
this.indexes = null;
this.displayIndexDialog = false;
};
//INTERFACE
interface Index {
defaultIndex?;
pK_Index ?;
description ?;
}
Here is the relevent HTML:
<!--INDEX TABLE-->
<p-table [columns]="cols3" [value]="indexes">
<ng-template pTemplate="header" let-columns>
<tr>
<th *ngFor="let col of columns">
{{col.header}}
</th>
</tr>
</ng-template>
<ng-template pTemplate="body" let-rowData let-columns="columns">
<tr [pSelectableRow]="rowData">
<td *ngFor="let col of columns">
{{rowData[col.field]}}
</td>
</tr>
</ng-template>
<ng-template pTemplate="summary" let-rowData>
<div style="text-align:left">
<button class="btn-cblt" type="button" pButton icon="fa-plus" (click)="showIndexAdd()" label="Add"></button>
</div>
</ng-template>
</p-table><br />
<h4>Differentials</h4>
<p-table [columns]="cols2" [value]="diffs1">
<ng-template pTemplate="header" let-columns>
<tr>
<th *ngFor="let col of columns">
{{col.header}}
</th>
</tr>
</ng-template>
<ng-template pTemplate="body" let-rowData let-columns="columns">
<tr [pSelectableRow]="rowData">
<td *ngFor="let col of columns">
{{rowData[col.field]}}
</td>
</tr>
</ng-template>
<ng-template pTemplate="summary" let-rowData>
<div style="text-align:left">
<button class="btn-cblt" type="button" pButton icon="fa-plus" (click)="showDifferentialAdd()" label="Add"></button>
</div>
</ng-template>
</p-table>
<!--ADD INDEX DIALOG-->
<p-dialog class="sha" header="Add Index" [(visible)]="displayIndexDialog" [responsive]="true" showEffect="fade" [modal]="true" [width]="600" [height]="300">
<div class="ui-g ui-fluid" *ngIf="selectedIndex">
<div class="ui-g-12">
<div class="ui-g-4">
<label for="Index">Index</label>
</div>
<div class="ui-g-8">
<p-dropdown id="index" placeholder="Select an Index" [options]="indexList" [(ngModel)]="selectedIndex.description" [ngModelOptions]="{standalone: true}" optionLabel="description" [showClear]="true"> </p-dropdown>
</div>
</div>
<div class="ui-g-12">
<div class="ui-g-4">
<label for="isDefault">Default Index?</label>
</div>
<div class="ui-g-8">
<p-checkbox name="defaultIndex" id="isDefault" [(ngModel)]="selectedIndex.defaultIndex"></p-checkbox>
</div>
</div>
<div id="indexName">{{index.pK_Index ? index.pK_Index: ' ' }}</div>
</div>
<div class="ui-dialog-buttonpane ui-helper-clearfix">
<button class="btn-cblt" type="button" pButton icon="fa-check" (click)="saveIndex()" label="Save"></button>
<button class="btn-cblt-danger" type="button" pButton icon="fa-close" (click)="closeIndexDialog()" label="Cancel"></button>
</div>
</p-dialog>
Every time I click the 'save' button on the dialog, in the console I get:

I have been staring at this for 3 days now and Have no idea why this isn't working. Please help!
The Resolution to this was #ConnorsFan 's suggestion. The indexes were not initialized. Setting indexes: Index[] = [] fixed the issue. Thank you #ConnorsFan.

Why Ajax helpers doesnt work?

Im trying to achieve a filter search box, actually it works but it just completely reload all of my Index view instead of replacing the div content:
My controller Actions (The index action and the partial action):
public IActionResult Index()
{
var grupoModels = _grupos.GetAll("");
var listaGrupos = grupoModels
.Select(resultado => new grupoCRUDModel
{
Codigo = resultado.id,
Nombre = resultado.nombre,
NIS = resultado.nis,
FeAcceso = resultado.feacceso,
Linea = resultado.Linea
});
var model = new grupoIndexModel()
{
grupos = listaGrupos
};
return View(model);
}
public IActionResult tablePartial(string filter = "")
{
var grupoModels = _grupos.GetAll(filter);
var listaGrupos = grupoModels
.Select(resultado => new grupoCRUDModel
{
Codigo = resultado.id,
Nombre = resultado.nombre,
NIS = resultado.nis,
FeAcceso = resultado.feacceso,
Linea = resultado.Linea
});
var model = new grupoIndexModel()
{
grupos = listaGrupos
};
return PartialView("tablePartial",model);
}
My Index view with the searchbox and the target div to render my partial:
<div>
<div class="mdl-grid headeritems">
<div class="mdl-cell mdl-cell--8-col ">
<h2>LISTADO DE GRUPOS</h2>
</div>
<div class="mdl-cell mdl-cell--2-col" style="text-align:right;">
<button class="mdl-button mdl-js-button mdl-button--fab" id="btnAdd">
<a class="material-icons" asp-controller="Grupo" asp-action="Crear" data-ajax="true" data-toggle="modal" data-target="#modalGrupo">add</a>
</button>
</div>
<div class="mdl-cell mdl-cell--2-col">
<form asp-controller="Grupo" asp-action="tablePartial" data-ajax-method="GET" data-ajax-update="#pdiv" data-ajax="true" method="get">
<div class="mdl-textfield mdl-js-textfield mdl-textfield--expandable">
<label class="mdl-button mdl-js-button mdl-button--icon" for="sbox">
<i class="material-icons">search</i>
</label>
<div class="mdl-textfield__expandable-holder">
<input class="mdl-textfield__input" type="text" id="sbox" name="filter">
<label class="mdl-textfield__label" for="sample-expandable">Expandable Input</label>
</div>
</div>
</form>
</div>
</div>
<br />
<div class="mdl-grid">
<div class="mdl-cell mdl-cell--1-col"></div>
<div id="pdiv" class="mdl-cell mdl-cell--10-col">
#Html.Partial("tablePartial")
</div>
<div class="mdl-cell mdl-cell--1-col"></div>
</div>
My partialTable code view:
#model libraryDemo.Models.grupoIndexModel
<table class="mdl-data-table mdl-js-data-table mdl-shadow--2dp" id="tablaGrupos" style="width:100%;">
<thead>
<tr>
<th class="mdl-data-table__cell--non-numeric">Nombre</th>
<th class="text-center">Linea</th>
<th class="text-center">Codigo</th>
<th colspan="2" class="text-center">Acciones</th>
</tr>
</thead>
<tbody>
#foreach (var grupo in Model.grupos)
{
<tr class="mtoRow">
<td class="mdl-data-table__cell--non-numeric">#grupo.Nombre</td>
<td class="text-center">#grupo.Linea.nombre</td>
<td class="text-center">#grupo.Codigo</td>
<td class="text-center" data-toggle="tooltip" data-placement="top" title="Editar"><a class="btn btn-warning" asp-controller="Grupo" asp-action="Editar" asp-route-id="#grupo.Codigo" data-ajax="true" data-toggle="modal" data-target="#modalGrupo">Editar</a></td>
<td class="text-center" data-toggle="tooltip" data-placement="top" title="Eliminar"><a class="btn btn-danger" asp-controller="Grupo" asp-action="Eliminar" asp-route-id="#grupo.Codigo" data-ajax="true" data-toggle="modal" data-target="#modalGrupo">Eliminar</a></td>
</tr>
}
</tbody>
</table>
Finally, my ajax function for keyup event:
$("#sbox").keyup(function () {
$("form").submit();
});
try data-ajax-url in place of asp-action
<form data-ajax-url="#Url.Action("tablePartial","Grupo")" data-ajax-method="GET" data-ajax-update="#pdiv" data-ajax="true" method="get">

VueJS data bind related

I use vueJS to display a table data,when I click the delete button, the table always delete the last row,rather not a specific row.I use my custom UIComponent 'lovTable',it changes a input element to another style,how to avoid it and make the data and page change the same way.Here's the code.Tks.
<script>
//服务类型下拉列表数据转化
var mServiceTypeData = $.map(serviceTypeData, function (obj) {
obj.id = obj.value;
obj.text = obj.meaning;
return obj;
});
$(function(){
var vm = new Vue({
el: '#div-rms-memo',
data: {
sub:[]
},
methods:{
//添加子项目
addSubProject:function () {
vm.sub.push({
subPrjServeType:'',
subPrjBelongToDept:'',
});
},
//移除当前行的子项目
removeCurrentRow:function(index){
var $del=vm.sub.splice(index,1);
},
}
});
function renderLines(el, lineIndex){
//渲染select2
$("#subPro"+lineIndex).find(".subPrjServeType").select2Ext({
placeholder: '请选择服务类型',
data: mServiceTypeData,
width:150
}).on('change', function () {
vm.sub[lineIndex].subPrjServeType = $(this).val();
});
//归属部门LOV
$("#subPro"+lineIndex).find(".subPrjBelongToDept").lovTable({
title:'归属部门',
placeholder:'请选择',
valueField:'unitId',
textField:'unitName',
query:[
{
text:'部门ID',
name:'unitId'
},
{
text:'部门名称',
name:'unitName'
}
],
rowSelected:function(row){
vm.sub[lineIndex].subPrjBelongToDept=row.unitId;
},
textClear:function(){
vm.sub[lineIndex].subPrjBelongToDept = "";
}
},{
url : '${base.contextPath}/project/prjBranchView/queryConditionPage?'+_header+'='+_token,
columns: [{
title:'部门ID',
field:'unitId'
},{
title:'部门名称',
field:'unitName'
}]
});
}
Vue.directive('line-bind', {
bind: function (el, binding) {
var lineIndex = binding.value;
console.info('binding index='+lineIndex)
vm.$nextTick(function () {
renderLines(el,lineIndex);
})
},
unbind:function(el,binding){
vm.$nextTick(function () {
for(var i=0;i<vm.sub.length;i++){
renderLines(vm.sub[i],i);
}
})
}
});
})
</script>
<div class="content-wrapper">
<section class="content">
<div id="div-rms-memo" >
<div class="box box-blue">
<div class="form-group">
<a href="javascript:void(0)" class="btn btn-crm" v-on:click="addSubProject">
<span class="glyphicon glyphicon-plus">添加</span>
</a>
</div>
<table align="center" class="table table-bordered" v-line-bind="index" v-for="(el,index) in sub" :id="'subPro'+index" :cindex="index">
<tr class="crm-title">
<td align="center" valign="middle" rowspan="2" style="width:5%;">
<a href="javascript:void(0)" class="btn btn-default btn-xs removeSubPrj" id="removeSubPrjBtn" v-on:click="removeCurrentRow(index)">
<span class="glyphicon glyphicon-minus"></span>
</a>
</td>
<td align="center" style="width:15%;"class="required">服务类型</td>
<td align="center" style="width:15%;"class="required">归属部门</td>
<td align="center" style="width:15%;" class="required">人天</td>
</tr>
<tr>
<td align="center">
<input type="text" id="subPrjServeType" v-model="el.subPrjServeType" class="form-control input-sm subPrjServeType"/>
</td>
<td align="center">
<input type="text" class="form-control input-sm subPrjBelongToDept" v-model="el.subPrjBelongToDept">
</td>
<td align="center">
<input type="number" class="form-control input-sm subPrjPeoplePerDay" v-model="el.subPrjPeoplePerDay">
</td>
</tr>
</table>
</div>
</div>
</section>
</div>

Why can I not return a list of users on my page?

This code is supposed to output the users that I have created using Identity. It will output the native attributes to identity such as email, but for some reason it will not output the date that the password was last changed. I physically see the attribute in my database and was able to input it using the form no problem, but I cannot get it out using IEnumerable<Microsoft.AspNet.Identity.EntityFramework.IdentityUser>. How do I get around this?
#model IEnumerable<Microsoft.AspNet.Identity.EntityFramework.IdentityUser>
#using PagedList.Mvc;
<link href="~/Content/PagedList.css" rel="stylesheet" type="text/css" />
#{
ViewBag.Title = "Manage Users";
}
#using (Html.BeginForm("ManageUsers", "AdminTools", FormMethod.Get))
{
<div class="col-lg-12 col-md-12 section-padding section-top-padding white-background">
<table id="datatable-responsive" class="table table-striped table-bordered dt-responsive nowrap" cellspacing="0" style="width:100%">
<thead>
<tr>
<th>Email</th>
<th>Password Change Date</th>
</tr>
</thead>
<tbody>
#foreach (var item in Model)
{
<tr>
<td>#Html.DisplayFor(modelItem => item.Email)</td>
<td>#Html.DisplayFor(modelItem => item.passwordLastChanged)</td>
</tr>
}
</tbody>
</table>
<button type="button" class="buttonIcon dark" data-toggle="modal" data-target="#CreateUserModal"><span class="glyphicon glyphicon-plus-sign fa-lg"> </span>New User</button>
</div>
}
<div class="modal fade" id="CreateUserModal" role="dialog">
<div class="modal-dialog">
#Html.Action("Register", "Account")
</div>
</div>

Resources