I want create the screen like this.
The select dropdowns and input fields should not effect on each other (right now if I select company, that is showing as selected in the next set, it should not like that). How can I implement this?
'use strict';
angular.module('newmonthlyplanning.controllers', [])
.controller('newmonthlyplanningCtrl', ['$scope','$window', '$state', 'serviceFactory', '$compile', '$interval','targetPlanningService',
function ($scope,$window,$state, serviceFactory, $compile,$interval,targetPlanningService) {
$scope.status = '200';
$scope.months = ['Jan','Feb','Mar','Apr','May','June','July','Aug','Sep','Oct','Nov','Dec'];
$scope.getAllSectors = function(){
targetPlanningService.getAllsectors().then(function (response) {
$scope.status = response.status;
console.log(response);
if (response.status === 200) {
$scope.sectors = response.data.sector_list;
}
else {
}
}, function (response) {
console.log(response);
if (response.status === 401) {
$state.go('login');
}
});
};
$scope.getcustomers = function(sector){
console.log("selected sector ",sector);
targetPlanningService.getAllCustomers(sector).then(function (response) {
$scope.status = response.status;
console.log(response);
if (response.status === 200) {
$scope.customers = response.data.customer_list;
}
else {
}
}, function (response) {
console.log(response);
if (response.status === 401) {
$state.go('login');
}
});
};
$scope.targetDetails = [];
$scope.getAllCompanies = function(){
targetPlanningService.getAllCompanies().then(function (response) {
$scope.status = response.status;
console.log("companies---> ",response);
if (response.status === 200) {
$scope.companies = response.data.companies;
$scope.targetDetails.push({'companies':$scope.companies,'sectors': $scope.sectors ,'customers': $scope.customers ,'targetss': $scope.targets});
}
else {
}
}, function (response) {
console.log(response);
if (response.status === 401) {
$state.go('login');
}
});
};
$scope.getAllproductIds = function(){
/* var details = {
'company' : $scope.targetDetails.company,
'sector' : $scope.targetDetails.sector
}
console.log("details--->",details);*/
console.log("$scope.targetDetails.company--->",$scope.targetDetails.company);
targetPlanningService.getAllproductIds($scope.targetDetails.company).then(function (response) {
$scope.status = response.status;
console.log("product_ids--->",response);
if (response.status === 200) {
$scope.productids = response.data.item_list;
}
else
{
}
}, function (response) {
console.log(response);
if (response.status === 401) {
$state.go('login');
}
});
};
$scope.targets = [{'pid': '','week1': '', 'week2': '','week3':'','week4':''}];
var i=1;
$scope.addNewChoice = function(id) {
$scope.targetDetails;
i++;
$scope.targets.push({'pid': '','week1': '', 'week2': '','week3':'','week4':''});
};
$scope.removeChoice = function(val) {
console.log("index------>",val);
$scope.targets.splice(val,1);
};
//$scope.targetDetails = [];
console.log("companies at targetDetails------>",$scope.companies);
var i =0;
$scope.addorder = function(){
var object = {};
object['targetss'] = $scope.targets;
// $scope.targetDetails.push({'companies':$scope.companies,'sectors': $scope.sectors ,'customers': '','targetss': $scope.targets});
$scope.targetDetails.push(object);
};
$scope.init = function () {
console.log("present state...",$state.current.name);
if($state.current.name==="monthly"){
$scope.getAllSectors();
// $scope.getnumberOfweeks();
$scope.getAllCompanies();
// $scope.getTargetDetails();
//$scope.getCommentDetails();
}
};
$scope.init();
}]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12" style="padding: 0px" ng-repeat="details in targetDetails">
<div class="row">
<table class="table table-bordered">
<thead>
<tr>
<th>Company</th>
<th>Sector</th>
<th>Customer</th>
</tr>
</thead>
<tbody>
<tr >
<td>
<div>
<select class="form-control" data-ng-model="targetDetails.company" ng-change="getAllproductIds()" style="border: 1px solid skyblue;">
<option value="">Select Company</option>
<option ng-repeat="company in details.companies track by $index" value="{{company}}" >{{company}}</option>
</select>
</div>
</td>
<td >
<div >
<select class="form-control" data-ng-model="targetDetails.sector" ng-change="getcustomers(targetDetails.sector)" style="border: 1px solid skyblue;">
<option value="">Select Sector</option>
<option ng-repeat=" sector in details.sectors track by $index" value="{{sector}}">{{sector}}</option>
</select>
</div>
</td>
<td>
<div >
<select class="form-control" data-ng-model="targetDetails.customer" style="border: 1px solid skyblue;">
<option value="Customer"> Customer</option>
<option ng-repeat=" customer in details.customers track by $index" value="{{customer}}">{{customer}}</option>
</select>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<div class="row" style="padding: 0px">
<table class="table table-bordered">
<thead>
<tr>
<th>Product Id</th>
<th >WEEK1</th>
<th >WEEK2</th>
<th >WEEK3</th>
<th >WEEK4</th>
<!-- <th>Total</th> -->
<!-- <th>Status</th> -->
<th >Action</th>
</tr>
</thead>
<tbody>
<tr data-ng-repeat="target in details.targetss">
<td align="center" >
<select class="form-control" style="width: 120px;border: 1px solid skyblue;" data-ng-model="target.pid" ng-change="getUnitsofProduct(target.pid)">
<option value="">Select pid</option>
<option data-ng-repeat="pid in productids">{{pid}}</option>
</select>
</td>
<td align="center">
<input class="form-control" style="width: 70px" type="text" name="" ng-model="target.week1">
</td>
<td align="center">
<input class="form-control" style="width: 70px" type="text" name="" ng-model="target.week2">
</td>
<td align="center">
<input class="form-control" style="width: 70px" type="text" name="" ng-model="target.week3">
</td>
<td align="center">
<input class="form-control" style="width: 70px" type="text" name="" ng-model="target.week4">
</td>
<!-- <td align="center" >
{{ (target.week1 * 1) + (target.week2 * 1)+ (target.week3 * 1)+ (target.week4 *1)+ (target.week5 *1) }} {{target.uom}}
</td> -->
<!-- <td ng-if="target.status==''"></td>
<td ng-if="target.status==='Pending'"><span style="color: blue">{{target.status}}</span></td>
<td ng-if="target.status==='Rejected'"><span style="color: red">{{target.status}}</span></td>
<td ng-if="target.status==='Accepted'"><span style="color: green">{{target.status}}</span></td> -->
<td align="center">
<button class="add" style="background-color: #008CBA;" data-ng-show="$last" data-ng-click="addNewChoice($parent.$index)">+</button>
<button class="sub" data-ng-click="removeChoice($index)" style="background-color:#f44336;margin:0px;">-</button>
</td>
<td>{{$parent.$index}}</td>
</tr>
</tbody>
</table>
<br>
</div>
</div>
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12" style="padding: 0px">
<div class="col-xs-1 col-sm-1 col-md-1 col-lg-1" >
<button data-ng-click="addorder()" style=" padding: 5px;margin: 0px">Add Order</button>
</div>
</div>
</div>
You can use $index for the purpose.
For example:
<select class="form-control" data-ng-model="targetDetails.company[$index]" ng-change="getAllproductIds()" style="border: 1px solid skyblue;">
<option value="">Select Company</option>
<option ng-repeat="company in details.companies track by $index" value="{{company}}" >{{company}}</option>
</select>
Use in ng-model:
data-ng-model = "targetDetails.company[$index]"
Hope this helps.
Related
Here is html code:
<div class="filters">
<div class="row">
<div class="col-md-3 filter">
<div class="form-group">
<label>Name</label>
<input type="text" value="" id="filter-by-name" class="form-control" autocomplete="off">
</div>
</div>
<div class="col-md-3 filter">
<div class="form-group">
<label>Supplier</label>
<input type="text" value="" id="filter-by-supplier" class="form-control" data-id="" autocomplete="off">
</div>
</div>
<div class="col-md-2 filter">
<div class="form-group">
<label>From Date</label>
<input type="number" min="0" value="" id="filter-by-fromdate" class="form-control" autocomplete="off">
</div>
</div>
<div class="col-md-2 filter">
<div class="form-group">
<label>To Date</label>
<input type="number" min="0" value="" id="filter-by-todate" class="form-control" autocomplete="off">
</div>
</div>
<div class="col-md-2 filter">
<div class="form-group">
<label>PriceNo</label>
<select id="filter-by-priceno" class="form-control">
<option value=""></option>
</select>
</div>
</div>
</div>
</div>
<table class="iblock table table-striped table-bordered table-hover table-order-items">
<thead>
<tr class="group-process">
<th>Name</th>
<th>Supplier</th>
<th>Fromdate</th>
<th>Todate</th>
<th colspan="3">PriceNo</th>
</tr>
</thead>
<tbody>
<tr class="odd group-process" data-id="2354031" style="background-color: rgb(255, 213, 128);">
<td>SEALING WASHER</td>
<td>
<select name="product-supplier_id[2354031-3-2]" class="form-control">
<option value="3" selected="">AGCO</option>
<option value="8">MAZZ</option>
<option value="9">Tomchuk</option>
<option value="88">ATTL</option>
</select>
</td>
<td><input type="text" name="product-delivery-from[2354031-3-2]" class="form-control text-right" value="0" placeholder="0"></td>
<td><input type="text" name="product-delivery-to[2354031-3-2]" class="form-control text-right" value="13" placeholder="13"></td>
<td>
<select name="product-price_no[2354031-3-2]" class="form-control text-right">
<option value="1">Standart</option>
<option value="2" selected="">Express</option>
<option value="3">Special Price</option>
</select>
</td>
</tr>
</tbody>
</table>
Also here is my Jquery code:
$("[id^=filter-by]").on('keyup change', function () {
var match = true;
var nameValue = $.trim($('#filter-by-name').val()).toLowerCase();
var supplierValue = $.trim($('#filter-by-supplier').val()).toLowerCase();
var fromdateValue = parseInt($('#filter-by-fromdate').val());
var todateValue = parseInt($('#filter-by-todate').val());
var pricenoValue = $('#filter-by-priceno').find(":selected").text();
$groupProcessingTable.find('tbody tr:not(:last-child)').each(function () {
if (nameValue != "") {
if (!($(this.children[0]).find('a').text().toLowerCase().indexOf(nameValue) > -1)) {
match = false;
}
if (supplierValue != "") {
if (!($(this.children[1]).find('option').filter(':selected').text().toLowerCase().indexOf(supplierValue) > -1)) {
match = false;
}
if (!isNaN(fromdateValue)) {
if (!(parseInt($(this.children[2]).find('input[name^=product-delivery-from]').val()) >= fromdateValue)) {
match = false;
}
if (!isNaN(todateValue)) {
if (!(parseInt($(this.children[3]).find('input[name^=product-delivery-to]').val()) <= todateValue)) {
match = false;
}
}
if (pricenoValue != "") {
if (!($(this.children[4]).find('a').text().toLowerCase().indexOf(pricenoValue) > -1)) {
match = false;
}
}
if (match) {
$(this).css("background-color", yellow);
} else {
$(this).css("background-color", '' );
}
});
});
My filter which contains inputs and selects has to work as Logical AND.
When all inputs and selects are true, this row have yellow background; otherwise this row's background color wil be transparent.
Please, help!
Thank You for all answers.
I tried to write Jquery code for solving using events, filter(), each(), flags.
I am expecting to get code for filtering table rows (by changing their background-color) with select and inputs in Jquery.
I've rewritten then code a bit, and for the test reason I have removed :not(:last-child) from $("#groupProcessingTable tbody tr")
The code will now only check each filter if i filter has a value.
$("#groupProcessingTable tbody tr").each(function() {
var match = (nameValue || supplierValue || fromdateValue || todateValue || pricenoValue);
if (nameValue != "") {
match = $(this).find("a").text().toLowerCase().includes(nameValue);
}
if (supplierValue != "") {
match = match && $(this).find("option:selected").text().toLowerCase().includes(supplierValue);
}
if (!isNaN(fromdateValue)) {
match = match && parseInt($(this).find("input[name^=product-delivery-from]").val()) >= fromdateValue;
}
if (!isNaN(todateValue)) {
match = match && parseInt($(this).find("input[name^=product-delivery-to]").val()) <= todateValue;
}
if (pricenoValue != "") {
match = match && $(this).find("select[name^=product-price_no] option:selected").text().toLowerCase() == pricenoValue;
}
if (match) {
$(this).css("background-color", "yellow");
} else {
$(this).css("background-color", "");
}
});
Demo
$("[id^=filter-by]").on('keyup change', function() {
var nameValue = $.trim($('#filter-by-name').val()).toLowerCase();
var supplierValue = $.trim($('#filter-by-supplier').val()).toLowerCase();
var fromdateValue = parseInt($('#filter-by-fromdate').val());
var todateValue = parseInt($('#filter-by-todate').val());
var pricenoValue = $('#filter-by-priceno option:selected').text().toLowerCase();
$("#groupProcessingTable tbody tr").each(function() {
var match = (nameValue || supplierValue || fromdateValue || todateValue || pricenoValue);
if (nameValue != "") {
match = $(this).find("a").text().toLowerCase().includes(nameValue);
}
if (supplierValue != "") {
match = match && $(this).find("option:selected").text().toLowerCase().includes(supplierValue);
}
if (!isNaN(fromdateValue)) {
match = match && parseInt($(this).find("input[name^=product-delivery-from]").val()) >= fromdateValue;
}
if (!isNaN(todateValue)) {
match = match && parseInt($(this).find("input[name^=product-delivery-to]").val()) <= todateValue;
}
if (pricenoValue != "") {
match = match && $(this).find("select[name^=product-price_no] option:selected").text().toLowerCase() == pricenoValue;
}
if (match) {
$(this).css("background-color", "yellow");
} else {
$(this).css("background-color", "");
}
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="filters">
<div class="row">
<div class="col-md-3 filter">
<div class="form-group">
<label>Name</label>
<input type="text" value="" id="filter-by-name" class="form-control" autocomplete="off">
</div>
</div>
<div class="col-md-3 filter">
<div class="form-group">
<label>Supplier</label>
<input type="text" value="" id="filter-by-supplier" class="form-control" data-id="" autocomplete="off">
</div>
</div>
<div class="col-md-2 filter">
<div class="form-group">
<label>From Date</label>
<input type="number" min="0" value="" id="filter-by-fromdate" class="form-control" autocomplete="off">
</div>
</div>
<div class="col-md-2 filter">
<div class="form-group">
<label>To Date</label>
<input type="number" min="0" value="" id="filter-by-todate" class="form-control" autocomplete="off">
</div>
</div>
<div class="col-md-2 filter">
<div class="form-group">
<label>PriceNo</label>
<select id="filter-by-priceno" class="form-control">
<option value=""></option>
<option value="1">Standart</option>
<option value="2">Express</option>
<option value="3">Special Price</option>
</select>
</div>
</div>
</div>
</div>
<table id="groupProcessingTable" class="iblock table table-striped table-bordered table-hover table-order-items">
<thead>
<tr class="group-process">
<th>Name</th>
<th>Supplier</th>
<th>Fromdate</th>
<th>Todate</th>
<th colspan="3">PriceNo</th>
</tr>
</thead>
<tbody>
<tr class="odd group-process" data-id="2354031" style="background-color: rgb(255, 213, 128);">
<td>SEALING WASHER</td>
<td>
<select name="product-supplier_id[2354031-3-2]" class="form-control">
<option value="3" selected="">AGCO</option>
<option value="8">MAZZ</option>
<option value="9">Tomchuk</option>
<option value="88">ATTL</option>
</select>
</td>
<td><input type="text" name="product-delivery-from[2354031-3-2]" class="form-control text-right" value="0" placeholder="0"></td>
<td><input type="text" name="product-delivery-to[2354031-3-2]" class="form-control text-right" value="13" placeholder="13"></td>
<td>
<select name="product-price_no[2354031-3-2]" class="form-control text-right">
<option value="1">Standart</option>
<option value="2" selected="">Express</option>
<option value="3">Special Price</option>
</select>
</td>
</tr>
</tbody>
</table>
I have a search with filter that narrows down data as expected. I would like to implement the functionality of hiding all the data by default, and only show results when they match a search. At the moment if nothing is typed everything is shown. The list will be very long once all data will be added, hence the request. Many thanks.
$(document).ready(function(){
$("#search-box").on("keyup", function() {
var value = $(this).val().toLowerCase();
var filter = $('#search-filter').val().toLowerCase();
if(filter == "listitem") {
$(".listitem").filter(function() {
$(this).toggle($(this).text().toLowerCase().indexOf(value) > -1)
});
} else {
$(".td-"+filter).filter(function() {
$(this).parent().toggle($(this).text().toLowerCase().indexOf(value) > -1)
});
}
});
$('#search-filter').on("change",function(){
var value = $("#search-box").val().toLowerCase();
var filter = $(this).val().toLowerCase();
if(filter == "listitem") {
$(".listitem").filter(function() {
$(this).toggle($(this).text().toLowerCase().indexOf(value) > -1)
});
} else {
$(".td-"+filter).filter(function() {
$(this).parent().toggle($(this).text().toLowerCase().indexOf(value) > -1)
});
}
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container">
<div class="input-group">
<div class="input-group-append">
<select id="search-filter" name="search-filter">
<!--<option value="listitem">All</option>-->
<option value="" disabled selected>Search by</option>
<option value="name">Name</option>
<option value="surname">Surname</option>
</select>
</div>
<input id="search-box" type="text" name="search" placeholder="Type here..." required="required"/>
</div>
<table>
<thead>
<tr>
<th>Name</th>
<th>Surname</th>
</tr>
</thead>
<tbody>
<tr class="listitem">
<td class="td-name">Jane</td>
<td class="td-surname">Doe</td>
</tr>
<tr class="listitem">
<td class="td-name">Dela</td>
<td class="td-surname">Cruz</td>
</tr>
</tbody>
</table>
</div>
Start by hiding everything. Then use an if statement to test if the search value is not empty, and show the matching elements.
I've also pulled the filtering code out into a named function, so we don't have to repeat it in both event listeners.
.filter() shouldn't be used to execute operations on each element, that should be done with .each(). But in this case you can use it to return a new collection that's just the matching elements, and you can then show them all together.
function filter_items(value, filter) {
$(".listitem, thead").hide();
if (value != '') {
if (filter == "listitem") {
$(".listitem").filter(function() {
return $(this).text().toLowerCase().includes(value)
}).show();
} else {
$(".td-" + filter).filter(function() {
return $(this).text().toLowerCase().includes(value)
}).closest(".listitem").show();
}
if ($(".listitem:visible").length > 0) {
$("thead").show();
}
}
}
$(document).ready(function() {
$("#search-box").on("keyup", function() {
var value = $(this).val().toLowerCase();
var filter = $('#search-filter').val().toLowerCase();
filter_items(value, filter);
});
$('#search-filter').on("change", function() {
var value = $("#search-box").val().toLowerCase();
var filter = $(this).val().toLowerCase();
filter_items(value, filter);
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container">
<div class="input-group">
<div class="input-group-append">
<select id="search-filter" name="search-filter">
<!--<option value="listitem">All</option>-->
<option value="" disabled selected>Search by</option>
<option value="name">Name</option>
<option value="surname">Surname</option>
</select>
</div>
<input id="search-box" type="text" name="search" placeholder="Type here..." required="required" />
</div>
<table>
<thead>
<tr>
<th>Name</th>
<th>Surname</th>
</tr>
</thead>
<tbody>
<tr class="listitem">
<td class="td-name">Jane</td>
<td class="td-surname">Doe</td>
</tr>
<tr class="listitem">
<td class="td-name">Dela</td>
<td class="td-surname">Cruz</td>
</tr>
</tbody>
</table>
</div>
I want to get the output as single row like group name and visible or not. but in my scenario i am getting different output .
enter image description here
Here is my html code.
#model List<F3CentricMVCApp.Areas.KnowledgeBox.Models.GroupResponse>
<script src="~/lib/jquery-validation/dist/jquery.validate.js"></script>
<script src="~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.js"></script>
<div class="col-12">
<table id="tblSectionGroups" class="table responsive table-striped" bPaginate="true">
<thead>
<tr>
<th width="95%">Groups</th>
<th width="5%" class="no-sort"></th>
</tr>
</thead>
<tbody>
#if (#ViewBag.UnAssidnedGroups is not null)
{
#foreach (var item in Model)
{
<tr>
<td>#item.Name</td>
#foreach (var unassinedGroup in ViewBag.UnAssidnedGroups)
{
#if (#unassinedGroup.GroupId == #item.Id)
{
<td>
<input type="checkbox" class="chkSectionVisibility" />
</td>
}
else
{
<td>
<input type="checkbox" class="chkSectionVisibility" checked="checked" />
</td>
}
}
</tr>
}
}
</tbody>
</table>
</div>
<script>
$('#tblSectionGroups').DataTable({
"bLengthChange": false,
"pageLength": 5,
"bPaginate": true,
"stripeClasses": [],
"info": false,
language: {
searchPlaceholder: "Type to filter list",
search: ""
},
"order": [],
"columnDefs": [{
"targets": 'no-sort',
"orderable": false,
}]
});
</script>
<div class="col-md-12 text-right">
<button type="button" class="btn btn-custom" tabindex="3" id="btnSave">Save</button>
</div>
Single checkbox for each group in a row is my requirement. any body can guide me how to deal with two collections in a code which is going through 2 foreach statements. but the logic should not be disturbed after all.
Update your foreach code as below i.e.
#if (#ViewBag.UnAssidnedGroups is not null)
{
#foreach (var item in Model)
{
int isUnassigned = 0;
<tr>
<td>#item.Name</td>
#foreach (var unassinedGroup in ViewBag.UnAssidnedGroups)
{
#if (#unassinedGroup.GroupId == #item.Id)
{
<td>
<input type="checkbox" class="chkSectionVisibility" />
</td>
// Setting.
isUnassigned++;
}
}
if (isUnassigned <= 0)
{
<td>
<input type="checkbox" class="chkSectionVisibility" checked="checked" />
</td>
}
</tr>
}
}
Hopefully this will solve your issue.
I am pretty new to knockoutJS, have completed their interactive tutorials, it was extremely informative.
I read a few posts, like :
KnockoutJS observableArray with template and foreach
It was quite helpful but I couldn't grasp the whole concept as to how to apply it in my context, cause I am fetching data from URL.
One way I thought of solving the problem is maybe by nesting templates.
Cause two things need to be done
Send the observable array to template
Loop over each element and access it's property so as to display in the tmplate
ToDo :
Load the observablearray(), got from a JSON into the template.
The output should be like this :
https://screenshots.firefoxusercontent.com/images/c72ec9d9-075b-40d0-b8cb-f3a4eca76c0d.png
The rendering element
<div class="row">
<div class="col-sm-12">
<div id="template_wallet" data-bind="template: { name: 'wallets_display_tmp', data: coins() }"></div>
### Template
<script type="text/html" id="wallets_display_tmp">
<h2>Wallets<br>
</h2>
<hr />
<table class="table table-striped table-hover">
<thead>
<tr>
<th class="text-center">Currency Name</th>
<th class="text-center">Symbol</th>
<th class="text-center">Amount</th>
<th class="text-center">Deposit/Withdraw</th>
</tr>
</thead>
<tbody>
<tr data-bind="foreach: coin()">
<td class="text-center" data-bind="text: name"></td>
<td class="text-center" data-bind="text: symbol"></td>
<td class=" options">
<div class="center-block text-center">
// <button type="button" class="btn btn-primary btn-xs text-center pop" data-toggle="modal" data-target=".modal" style="margin:-6px 2px" data-purpose="deposit" data-coin=<%= wallet.get('name') %>><span class="fa fa-plus"></span></button>
// <button type="button" class="btn btn-primary btn-xs text-center pop" data-toggle="modal" data-target=".modal" style="margin:-6px 2px" data-purpose="withdrawal" data-coin=<%= wallet.get('name') %> ><span class="fa fa-minus"></span></button>
</div>
</td>
<td></td>
</tr>
</tbody>
</table>
</script>
</div>
</div>
</body>
<script>
$(document).ready(function() {
function walletCoin(){
this.name = ko.observable();
this.symbol = ko.observable();
this.balance = ko.observable();
}
function WalletViewModel(){
var self = this;
self.coins = ko.observableArray([]);
$.getJSON("https://api.myjson.com/bins/13ed0p", function(allData){
console.log(allData);
var mappedCoins = $.map(allData, function(item){
return new walletCoin(item);
});
self.coins(mappedCoins);
});
}
// var walletBalance = new WalletViewModel();
// ko.components.register('', {
// template: {element: 'wallets_display_tmp'},
// });
ko.applyBindings(new WalletViewModel(), document.getElementById("template_wallet"));
});
</script>
The JSON data is
[
{
"name": "Bitcoin",
"symbol": "BTC",
"balance": 0.001212
},
{
"name": "Ripple",
"symbol": "XRP",
"balance": 123.1212
},
{
"name": "Litecoin",
"symbol": "LTC",
"balance": 13.1212
},
{
"name": "Digibyte",
"symbol": "DGB",
"balance": 1231.1212
}
]
Update : I have added a knockoutJS context debugger and added ko.toJSON($data)
Currently I am getting output like this :
$(document).ready(function() {
function walletCoin(){
this.name = ko.observable();
this.symbol = ko.observable();
this.balance = ko.observable();
}
function WalletViewModel(){
var self = this;
self.coins = ko.observableArray([]);
$.getJSON("https://api.myjson.com/bins/13ed0p", function(allData){
console.log(allData);
for (var i = allData.length - 1; i >= 0; i--) {
console.log(allData[i]);
var mappedCoins = ko.mapping.fromJS(allData[i], walletCoin);
self.coins.push(mappedCoins);
}
});
console.log(self.coins());
}
ko.applyBindings(new WalletViewModel(), document.getElementById("template_wallet"));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout.mapping/2.4.1/knockout.mapping.min.js"></script>
<div class="row">
<div class="col-sm-12">
<hr />
<h2>Debug</h2>
<div data-bind="text: ko.toJSON(WalletViewModel)"></div>
<div id="template_wallet" data-bind="template: { name: 'wallets_display_tmp', foreach:coins }"></div>
<script type="text/html" id="wallets_display_tmp">
<h2>Wallets <br>
<small>Estimated Value: 0.00318249 BTC / 47.39 USD</small>
</h2>
<hr />
<table class="table table-striped table-hover">
<thead>
<tr>
<th class="text-center">Currency Name</th>
<th class="text-center">Symbol</th>
<th class="text-center">Amount</th>
<th class="text-center">Deposit/Withdraw</th>
</tr>
</thead>
<tbody>
<tr>
<td class="text-center" data-bind="text: name"></td>
<td class="text-center" data-bind="text: symbol"></td>
<td class="text-center" data-bind="text: balance"></td>
<td class=" options">
<div class="center-block text-center">
// <button type="button" class="btn btn-primary btn-xs text-center pop" data-toggle="modal" data-target=".modal" style="margin:-6px 2px" data-purpose="deposit" data-coin=<%= wallet.get('name') %>><span class="fa fa-plus"></span></button>
// <button type="button" class="btn btn-primary btn-xs text-center pop" data-toggle="modal" data-target=".modal" style="margin:-6px 2px" data-purpose="withdrawal" data-coin=<%= wallet.get('name') %> ><span class="fa fa-minus"></span></button>
</div>
</td>
</tr>
</tbody>
</table>
</script>
<!-- <div class="container">
<canvas id="live_exchange_chart"></canvas>
</div> -->
</div>
</div>
Do check this :
<script>
$(document).ready(function() {
function walletCoin(){
this.name = ko.observable();
this.symbol = ko.observable();
this.balance = ko.observable();
}
function WalletViewModel(){
var self = this;
self.coins = ko.observableArray([]);
$.getJSON("https://api.myjson.com/bins/13ed0p", function(allData){
console.log(allData);
for (var i = allData.length - 1; i >= 0; i--) {
console.log(allData[i]);
var mappedCoins = ko.mapping.fromJS(allData[i], walletCoin);
self.coins.push(mappedCoins);
}
});
console.log(self.coins());
}
ko.applyBindings(new WalletViewModel(), document.getElementById("template_wallet"));
});
</script>
Here is the jsfiddle for the same.
self.coins = ko.computed(function () {
var coins = {};
self.days = '';
$.ajax({
type: "GET",
dataType: "json",
url: yoururl,
data: {},
async: false,
success: function (response) {
if (response.items) {
for (var i = 0; i < response.items.length; i++) {
coins[i] = {
name: response.items[i].name,
symbol: response.items[i].symbol,
balance: response.items[i].balance
};
}
}
},
error: function () {
console.log('error');
}
});
return coins;
});
I am not fully understand your question, but i guess you need something like this?
I am using onaftersave() function to save the table data. In dir-paginate i am using ng-repeat to read the table row values. I kept itemsperpage is 5. obviously 6th row will be display in second Page. When I try to edit the second page row values it is not updating the second page rows. How to resolve it?
<form editable-form name="tableform1" oncancel="cancel()" onaftersave="saveTable(Manage_Role.Description)">
<div class="row">
<div class="col-xs-12">
<div class="panel panel-default panel-table">
<div class="panel-heading">
<div class="row">
<div class="col col-xs-6">
</div>
<div class="col col-xs-6 text-right">
<button type="button" data-toggle="modal" data-target="#myModal" class="btn btn-sm btn-primary btn-create" title="Add New Role">Add New Role</button>
</div>
</div>
</div>
<div class="panel-body scrollbar">
<table class="table table-striped table-bordered table-list">
<thead>
<tr>
<th style="width: 25%" align="center">
Name
</th>
<th style="width: 35%">
Description
</th>
<th style="width: 30% " align="center">
IsActive?
</th>
<th style="width: 10% " align="center"><a href="" ng-click="orderByField='IsActive';
reverseSort = !reverseSort" title="Assign">Assign</a></th>
<th style="width: 1%" align="center" ng-show="false">DirtyFlag</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<input type="text" id="RoleNameID" ng-model="Manage_Role1.RoleName" placeholder="search" class="searchbox" onkeydown="return (event.keyCode!=13);"
/>
</td>
<td>
<input type="text" id="DescriptionID" ng-model="Manage_Role1.Description" placeholder="search" class="searchbox" onkeydown="return (event.keyCode!=13);"
/>
</td>
<td>
<select name="DropFilter" ng-model="Manage_Role1.IsActive" class="searchbox">
<option value="">All</option>
<option value=false>False</option>
<option value=true>True</option>
</select>
</td>
<!--<td></td>-->
</tr>
<tr dir-paginate="Manage_Role in Manage_Roleser | itemsPerPage:5 | filter:DropFilter | filter:Manage_Role1 | orderBy:orderByField:reverseSort "
ng-class-odd="'odd'">
<td ng-click="tableform1.$show()">
<span e-ng-change="applyHighlight($data,$index)" editable-text="Manage_Role.RoleName" ng-attr-title="{{Manage_Role.RoleName}}"
e-form="rowform" ng-click="rowform.$show()">
{{ Manage_Role.RoleName | uppercase }}
</span>
</td>
<td ng-click="tableform1.$show()">
<div>
<span e-ng-change="applyHighlight($data,$index)" editable-text="Manage_Role.Description" e-form="rowform" ng-attr-title="{{Manage_Role.Description}}"
ng-click="rowform.$show()">
{{ Manage_Role.Description || 'empty' }}
</span>
</div>
</td>
<td ng-click="tableform1.$show()" align="center">
<span e-ng-change="applyHighlight($data,$index)" ng-checked="Manage_Role.IsActive" editable-checkbox="Manage_Role.IsActive"
e-form="rowform" ng-click="applyHighlight($data,$index);tableform.$show()">
<input type="checkbox" ng-model="Manage_Role.IsActive" width="20" ng-attr-title="{{Manage_Role.IsActive}}" />
</span>
</td>
<td align="center">
<input type="image" data-toggle="modal" ng-click="Rolepopupclick(Manage_Role._id,Manage_Role.RoleName)" title="Assign UI"
data-target="#Div1" src="App_Themes/Images/AssignUser.png"
alt="Submit" width="28" height="28" />
</td>
<td ng-show="false">
<input type="text" ng-model="Manage_Role.DirtyFlag" ng-show="false" />
</td>
</tr>
</tbody>
</div>
</table>
</div>
<div class="panel-footer">
<div class="row">
<div class="col col-xs-8">
<dir-pagination-controls max-size="5" direction-links="true" boundary-links="true">
</dir-pagination-controls>
</div>
<div class="col col-xs-4">
<div class="btn-form" ng-show="tableform1.$visible" style="float:right">
<button type="submit" ng-disabled="tableform1.$waiting" title=" Save Changes" class="btn btn-primary fa fa-save" ng-click="NotifyClick()"> Save</button>
<input type="button" ng-disabled="tableform1.$waiting" title=" Close Edit Form Without Saving Changes" ng-click="refreshUI();tableform1.$cancel()"
class="btn btn-danger fa fa-close" value="X Cancel" />
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</form>
controller code:
$scope.saveTable = function ()
{
debugger;
if(NotifyCount!=1){
var RoleNamechk = '';
for (var i = 0; i < $scope.Manage_Roleser.length; i++) {
RoleNamechk = $scope.Manage_Roleser[i].RoleName;
for (var j = 0; j < $scope.Manage_Roleser.length; j++) {
var RoleName = $scope.Manage_Roleser[j].RoleName;
if (RoleNamechk == RoleName) {
count++;
}
}
}
if (count == $scope.Manage_Roleser.length)
{
for (var i = 0; i < $scope.Manage_Roleser.length; i++) {
var id = $scope.Manage_Roleser[i]._id;
var rolename = $scope.Manage_Roleser[i].RoleName;
var isactive = $scope.Manage_Roleser[i].IsActive;
var description = $scope.Manage_Roleser[i].Description;
if (description == "")
{
description = 'empty';
}
if ($scope.Manage_Roleser[i].DirtyFlag == "True")
{
$scope.LoggedinUsers = $cookieStore.get('LoggedinUser');
var updatedBy= $scope.LoggedinUsers;
$http.put('/Roleupdate/' + id + '/' + rolename + '/' + isactive + '/' + description + '/' + updatedBy).then(function (response) {
refresh();
});
}
count=0;
}
$notify.success('Success', 'Role is Updated Successfully');
}
else {
$notify.warning('Warning', 'Role Name is Already Exists');
count = 0;
return '';
}
}
};
in ng-change I am checking dirty flag of selected index because it is updating all the row values. for that I am updating only selected($dirty) row alone. In second page it is not taking table index correctly. If I select 2nd page 3 row it will assume or taking index of the particular page. It should take index of selected row from the over all table row
$scope.applyHighlight = function ($data,index) {
$scope.Manage_Roleser[index].DirtyFlag = "True";
var dataSpan = $(event.currentTarget).closest('td');
if (!dataSpan.hasClass("highlighted")) {
$(dataSpan).addClass("highlighted");
}
}