can i use two ng-repeat in inside td table? - javascript

my current result
Expected result
Please guide me how to use two ng-repeat inside one td. when i use span tag after i am not getting expected result.i have use one ng-repat in td and other i have use in span tag so i am not getting expected result so how to use two ng-repeat in one td.
var app = angular.module("myApp", []);
app.controller("myCtrl", function($scope) {
$scope.records = [
{"year":2006,"quarters":{"q4":{"kk":5.0,"fl":0.0}}},{"year":2007,"quarters":{"q1":{"kk":9.0,"fl":0.0},"q2":{"kk":8.0,"fl":0.0},"q3":{"kk":7.0,"fl":0.0},"q4":{"kk":6.0,"fl":0.0}}},{"year":2008,"quarters":{"q1":{"kk":5.0,"fl":0.0},"q2":{"kk":4.0,"fl":0.0},"q3":{"kk":3.0,"fl":0.0},"q4":{"kk":4.0,"fl":0.0}}},{"year":2009,"quarters":{"q1":{"kk":5.0,"fl":0.0},"q2":{"kk":6.0,"fl":0.0},"q3":{"kk":7.0,"fl":0.0},"q4":{"kk":6.0,"fl":0.0}}},{"year":2010,"quarters":{"q1":{"kk":7.0,"fl":0.0},"q2":{"kk":5.0,"fl":0.0},"q3":{"kk":7.0,"fl":0.0},"q4":{"kk":6.0,"fl":0.0}}},{"year":2011,"quarters":{"q1":{"kk":8.0,"fl":0.0},"q2":{"kk":7.0,"fl":0.0},"q3":{"kk":9.0,"fl":0.0},"q4":{"kk":8.0,"fl":0.0}}},{"year":2012,"quarters":{"q1":{"kk":0.0,"fl":0.0},"q2":{"kk":9.0,"fl":0.0},"q3":{"kk":8.0,"fl":0.0},"q4":{"kk":7.0,"fl":0.0}}},{"year":2013,"quarters":{"q1":{"kk":6.0,"fl":0.0},"q2":{"kk":5.0,"fl":0.0},"q3":{"kk":4.0,"fl":0.0},"q4":{"kk":0.0,"fl":0.0}}},{"year":2014,"quarters":{"q1":{"kk":3.0,"fl":0.0},"q2":{"kk":2.0,"fl":0.0},"q3":{"kk":3.0,"fl":0.0},"q4":{"kk":4.0,"fl":0.0}}},{"year":2015,"quarters":{"q1":{"kk":5.0,"fl":0.0},"q2":{"kk":0.0,"fl":0.0},"q3":{"kk":6.0,"fl":0.0},"q4":{"kk":7.0,"fl":0.0}}},{"year":2016,"quarters":{"q1":{"kk":0.0,"fl":0.0},"q2":{"kk":5.0,"fl":0.0},"q3":{"kk":4.0,"fl":0.0},"q4":{"kk":4.0,"fl":0.0}}},{"year":2017,"quarters":{"q1":{"kk":3.0,"fl":0.0},"q2":{"kk":8.0,"fl":0.0},"q3":{"kk":3.0,"fl":0.0},"q4":{"kk":5.0,"fl":0.0}}},{"year":2018,"quarters":{"q1":{"kk":6.0,"fl":0.0},"q2":{"kk":7.0,"fl":0.0},"q3":{"kk":8.0,"fl":0.0},"q4":{"kk":3.0,"fl":0.0}}},{"year":2019,"quarters":{"q1":{"kk":2.0,"fl":0.0},"q2":{"kk":4.0,"fl":0.0},"q3":{"kk":6.0,"fl":0.0},"q4":{"kk":7.0,"fl":0.0}}},{"year":2020,"quarters":{"q1":{"kk":8.0,"fl":0.0}}}
]
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.2/angular.min.js"></script>
<table class="table table-bordered" ng-app="myApp" ng-controller="myCtrl">
<tr>
<th colspan="2">Year</th>
<td colspan="4" data-ng-repeat="years in records" ng-bind="years.year"></td>
</tr>
<tr>
<td style="border:0">Question</td>
<td style="border:0"> </td>
<td data-ng-repeat="years in records">
<span class="yearStyle text-center" data-ng-repeat="(key, value) in years.quarters"
data-ng-init="asdasd()" ng-bind="key "></span>
</tr>
<tr>
<td rowspan="3" colspan="1">A205</td>
<td>KK</td>
<td data-ng-repeat="years in records"><input type="number" class="yearStyle text-center" data-ng-repeat="(key, value) in years.quarters"
ng-bind="value.kk" ng-model="value.kk"></td>
</tr>
<tr>
<td>FL</td>
<td data-ng-repeat="years in records"><input type="number" class="yearStyle text-center" data-ng-repeat="(key, value) in years.quarters"
ng-bind="value.fl" ng-model="value.fl"></td>
</tr>
<tr>
<td>Total</td>
<td data-ng-repeat="years in records"><input type="number" class="yearStyle text-center" data-ng-repeat="(key, value) in years.quarters"
ng-bind="value.fl + value.kk" ng-model="value.fl + value.kk"></td>
</table>

Problem is in your json data. You should have an Array of objects in years.quarters json property so that it can loop through in data-ng-repeat="(key, value) in years.quarters"
Currently, your years.quarters property is an object

Related

rowspan in angular application

I have the following table in an Angular 8 application where I am looping through some data and displaying the data in the table. In the table I want to use rowspan for "Status" and "Create Date" since these will always be the same. "Name" and "Amount" will differ so need different rows for them but a single entry for Status. I tried rowspan="abc.length" for {{data.status}} but it does not work.
<table class="table tableBorder table-responsive table w-100">
<tr style="text-align: center;" class="bg-gray">
<td class="w-12 bold text-left">Name</td>
<td class="w-10 bold text-left">Amount (Rs.Cr.)</td>
<td class="w-13 bold text-left">Status</td>
<td class="w-8 bold text-left">Create Date</td>
</tr>
<tr *ngFor="let data of abc; let i = index">
<td class="w-12 truncate-text ref-number-truncate line-height23">
{{data.name}}
</td>
<td class="w-8 truncate-text">{{data.amount}}</td>
<td class="w-13 truncate-text">{{data.status}}</td>
<td class="w-10 truncate-text">{{data.createdDate}}</td>
</tr>
</table>
Try [attr.rowspan] as
<td [attr.rowspan]= "abc.length">

How to find find value in table cell value with jquery

I have a table html code that comes from out source via ajax like this:
<table class="stripped">
<tr class="red">
<td style="font-weight:bold;">City</td>
<td class="red">xyz</td>
</tr>
<tr class="red">
<td style="font-weight:bold;">Country</td>
<td class="red">abc</td>
</tr>
<tr class="red">
<td style="font-weight:bold;">Date</td>
<td class="red">05.10.2017</td>
</tr>
</table>
<table class="stripped">
<tr class="red">
<td style="font-weight:bold;">Category</td>
<td class="red">This is category</td>
</tr>
</table>
I want to get value of country value in this html. Jquery nth-child can find but I could not find. I find nht tr item but can not find value ("abc") of country.
Not sure what you want to do with it, but if you want to get the second td of a tr that contains Country, use the following $("table tr:contains(Country) td:eq(1)").text()
Demo
var text = $("table tr:contains(Country) td:eq(1)").text();
console.log(text)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class="stripped">
<tr class="red">
<td style="font-weight:bold;">City</td>
<td class="red">xyz</td>
</tr>
<tr class="red">
<td style="font-weight:bold;">Country</td>
<td class="red">abc</td>
</tr>
<tr class="red">
<td style="font-weight:bold;">Date</td>
<td class="red">05.10.2017</td>
</tr>
</table>
<table class="stripped">
<tr class="red">
<td style="font-weight:bold;">Category</td>
<td class="red">This is category</td>
</tr>
</table>
let country = document.querySelectorAll("tr")[1].querySelectorAll("td")[1].innerText;
document.querySelectorAll("tr")[1] (second tr)
.querySelectorAll("td")[1] (second td in second tr)
.innerText (gets value)
How about following selector:
$( "table.stripped tr:nth-child(2) td:nth-child(2)" ).val();
The only real reference point besides nth-child is your text headings.
Since this is the case we can just look through your table cells for the heading Date and then get the text from the following cell (that cell's index + 1).
It should be pointed out that this is less than ideal. This solution, like every other one we can offer given the circumstances, can very easily be defeated if mark-up changes in the future. Just something to keep in mind, though I do understand that sometimes you have no choice.
$("td").each(function(ind, ele) {
if(ele.textContent === "Date"){
console.log("date is", $("td").eq(ind+1).text() );
}});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class="stripped">
<tr class="red">
<td style="font-weight:bold;">City</td>
<td class="red">xyz</td>
</tr>
<tr class="red">
<td style="font-weight:bold;">Country</td>
<td class="red">abc</td>
</tr>
<tr class="red">
<td style="font-weight:bold;">Date</td>
<td class="red">05.10.2017</td>
</tr>
</table>
<table class="stripped">
<tr class="red">
<td style="font-weight:bold;">Category</td>
<td class="red">This is category</td>
</tr>
</table>
Code: http://tpcg.io/Vy9IwJ
<script>
$(document).ready(function() {
$(".stripped td").get().forEach(function(entry, index, array) {
if ($(entry).text()=='Country') {
$('#result').html( $('#result').html()+'Country: '+$(entry).next().text() );
}
});
});
</script>
<div id="result">
</div>
<table class="stripped">
<tr class="red">
<td style="font-weight:bold;">City</td>
<td class="red">xyz</td>
</tr>
<tr class="red">
<td style="font-weight:bold;">Country</td>
<td class="red">abc</td>
</tr>
<tr class="red">
<td style="font-weight:bold;">Date</td>
<td class="red">05.10.2017</td>
</tr>
</table>
<table class="stripped">
<tr class="red">
<td style="font-weight:bold;">Category</td>
<td class="red">This is category</td>
</tr>
</table>

jquery sorting with each element of itsown

So here is my Concern. I am trying to sort the data i have in the following format so that every parent elemnts has child elments and when the sort is selected for that, it should only sort its childrens
The Code for this is:
<!--- first list --->
<tr>
<td colspan="2"><li class="childrens" data-id="99" style="list-style:none;margin-left:0px;"> <strong style="font-size:16px;">Information</strong> [Desc] </li></td>
</tr>
<tr>
<td colspan="2"><li data-id="81" style="margin-left:20px;"> Running </li></td>
</tr>
<tr>
<td colspan="2"><li data-id="113" style="margin-left:40px;"> Coping</li></td>
</tr>
<tr>
<td colspan="2"><li data-id="71" style="margin-left:40px;"> Printing </li></td>
</tr>
<tr>
<td colspan="2"><li data-id="65" style="margin-left:20px;"> references </li></td>
</tr>
<!--- Second List --->
<tr>
<td colspan="2"><li class="childrens" data-id="85" style="list-style:none;margin-left:0px;"> <strong style="font-size:16px;">Papers</strong> [Desc] </li></td>
</tr>
<tr>
<td colspan="2"><li data-id="116" style="margin-left:20px;"> Opening </li></td>
</tr>
<tr>
<td colspan="2"><li data-id="109" style="margin-left:20px;"> Closng </li></td>
</tr>
what i am trying is: when i click the desc of the first list, it should only the elements which are under the first list using data-id
same for second list ..
both lists should work independently and sorting should be on heir own, i gave a shot but not successful
here is my code
function sortdesc(){
$('li.childrens').sort(function(a,b){
return parseInt(a.getAttribute('data-id'),10)-parseInt(b.getAttribute('data-id'),10)
});
}
$(document).on('click',".sort",function(e) {
sortdesc();
});
tried here in fiddle but no luck [updated to add table to each li]
http://jsfiddle.net/HWmz3/85/
According to this answer:
function sortdesc(elem) {
// get the table of the clicked link, find its tbody
var $tbody = $(elem).closest('table').find('tbody');
// sort the tr of this tbody
$tbody.find('tr')
.sort(function(a, b) {
var a = $(a).find('li'); // get the li of a tr
var b = $(b).find('li'); // get the li of b tr
return a.data('id') < b.data('id') ? -1 : 1; // compare and return the result
})
.appendTo($tbody); // to make the sort
}
$(document).on('click', ".sort", function(e) {
// pass the link that was clicked to the function:
sortdesc(this);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!--- first list --->
<table>
<thead>
<tr>
<td colspan="2">
<li class="childrens" data-id="99" style="list-style:none;margin-left:0px;"> <strong style="font-size:16px;">Information</strong> [Desc]
</li>
</td>
</tr>
</thead>
<tbody>
<tr>
<td colspan="2">
<li data-id="81" style="margin-left:20px;">Running</li>
</td>
</tr>
<tr>
<td colspan="2">
<li data-id="113" style="margin-left:40px;">Coping</li>
</td>
</tr>
<tr>
<td colspan="2">
<li data-id="71" style="margin-left:40px;">Printing</li>
</td>
</tr>
<tr>
<td colspan="2">
<li data-id="65" style="margin-left:20px;">references</li>
</td>
</tr>
</tbody>
</table>
<!--- Second List --->
<table>
<thead>
<tr>
<td colspan="2">
<li class="childrens" data-id="85" style="list-style:none;margin-left:0px;"> <strong style="font-size:16px;">Papers</strong> [Desc]
</li>
</td>
</tr>
</thead>
<tbody>
<tr>
<td colspan="2">
<li data-id="116" style="margin-left:20px;">Opening</li>
</td>
</tr>
<tr>
<td colspan="2">
<li data-id="109" style="margin-left:20px;">Closng</li>
</td>
</tr>
</tbody>
</table>

Angular 1.5 not update ng-repeat

I display a table through ng-repeat
<div ng-app="spApp">
<div ng-controller="spListCtrl as MyList">
<table width="100%" cellpadding="10" cellspacing="2">
<thead>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
<th>CellPhone</th>
<th>Update</th>
</thead>
<tbody>
<tr ng-repeat="item in MyList.Contacts track by $index">
<td class="align-center"><input type="text" ng-model="MyList.Contacts[$index].FirstName"> </input></td>
<td class="align-center">{{MyList.Contacts[$index].Title}}</td>
<td class="align-center">{{MyList.Contacts[$index].Email}}</td>
<td class="align-center">{{MyList.Contacts[$index].CellPhone}}</td>
<td class="align-center"><button ng-click="ShowNewForm(MyList.Contacts[$index])">Изменить</button></td>
</tr>
</tbody>
</table>
Loading through the service on the ajax data
spApp.controller('spListCtrl', function spListCtrl($scope,dataService){
var Contacts;
var promiseObj=dataService.getContacts();
promiseObj.then(function(value) {
Contacts=value;
});
I check in debugging, and data are assigned to come normal, but not displayed. What did not tried it, tell me what I'm doing wrong.
while using controllerAs pattern, do bind data binding variables to this(controller function context), so that you could access them on HTML using it alias MyList(which is instance of controller function).
Code
spApp.controller('spListCtrl', function spListCtrl(dataService){
var self = this
var promiseObj=dataService.getContacts();
promiseObj.then(function(value) {
self.Contacts=value.data;
});
});
And inside ng-repeat use item to have binding working.
<tr ng-repeat="item in MyList.Contacts track by $index">
<td class="align-center"><input type="text" ng-model="item.FirstName"> </input></td>
<td class="align-center">{{item.Title}}</td>
<td class="align-center">{{item.Email}}</td>
<td class="align-center">{{item.CellPhone}}</td>
<td class="align-center"><button ng-click="ShowNewForm(item)">Изменить</button></td>
</tr>

use button to pass td data

I have a table that I want to give the user the ability to select. I added an button, value="Update", to the beginning of the row, and assigned an onclick value. My problem is that I want to send other items(in td>s on the same row) from the row to the function called by the button.
How do I get the information from the row the button is on? I would like to pass the "Name" and "Last Update Time" to the function the button calls. I tried using the following:
$("#Report input[name=btn_id]").closest('td').attr('text')
but it returns "undefined," which I am not surprised by, as I think this is due to it not knowing what row of the table to pull from.
Here is a view of the table:
Here is the code behind the table:
<table align="center" border="1" class="report" id="report">
<thead>
<tr>
<th width="75">Update</th>
<th width="500">Name</th>
<th width="50">Info</th>
<th width="100">location</th>
<th width="100">Last Update Time</th>
</tr>
</thead>
<tbody>
<tr class="parent" id="other_app">
<td align="center">
<input type="button" name="btn_id" value="Update" onclick="UpdateRec(d1)">
</td>
<td name="App_Name">Test1</td>
<td align="center">None</td>
<td align="center">Desktop</td>
<td align="center">2014-06-30 18:22:39</td>
</tr>
<tr class="parent" id="other_app">
<td align="center">
<input type="button" name="btn_id" value="Update" onclick="UpdateAppRec(d1)">
</td>
<td name="App_Name">Test1</td>
<td align="center">None</td>
<td align="center">Server</td>
<td align="center">2014-03-30 16:20:15</td>
</tr>
</tbody>
Any help would be appreciated.
Embrace the power of this.
Using:
onclick="UpdateRec(this)"
..in your function:
function UpdateRec(el) {
var targetRow = $(el).parents('tr')
...
}
Using this passes a reference to the clicked element. You can then use jQuery to select the parent table row. From there you can use .find() to select anything in that row.
Another way to do this would be to use HTML5 data- attributes on this button itself:
<input type="button" name="btn_id" value="Update" onclick="UpdateRec(d1)" data-appName="something" />
In the same function you can then use $(el).data('appName') to get the value directly without looking up values in other DOM elements.
//Add a click listener on all your buttons using event delegation
$('#Report').click(onUpdateButtonClicked, 'input[name=btn_id]');
function onUpdateButtonClicked() {
var rowValues = $(this)
.parent() //select parent td
.nextAll() //select all next siblings of that parent td
.map(function () { //loop through the tds, collecting their text value
return $(this).text();
}).get(); //return the result as an array
//do what you want with rowValues
}
I would suggest you to use common class for all update buttons with common click event handler.
Here is the fiddle: http://jsfiddle.net/jwet6z6x/
HTML:
<table align="center" border="1" class="report" id="report">
<thead>
<tr>
<th width="75">Update</th>
<th width="500">Name</th>
<th width="50">Info</th>
<th width="100">location</th>
<th width="100">Last Update Time</th>
</tr>
</thead>
<tbody>
<tr class="parent" id="other_app">
<td align="center">
<input class="updateBtn" type="button" name="btn_id" value="Update">
</td>
<td name="App_Name">Test1</td>
<td align="center">None</td>
<td align="center">Desktop</td>
<td align="center">2014-06-30 18:22:39</td>
</tr>
<tr class="parent" id="other_app">
<td align="center">
<input class="updateBtn" type="button" name="btn_id" value="Update">
</td>
<td name="App_Name">Test1</td>
<td align="center">None</td>
<td align="center">Server</td>
<td align="center">2014-03-30 16:20:15</td>
</tr>
</tbody>
Javascript:
$(".updateBtn").click(function(){
var name = $(this).parent().parent().find('td').eq(1).html()
var time = $(this).parent().parent().find('td').eq(4).html()
alert (name);
alert (time);
})
I would do as follow : http://jsfiddle.net/Lk91cpup/2/
<table>
<tr id="row_1">
<td>
<input type="button" id="btn_row_1" class="btn" value="Update">
</td>
<td id="text_row_1">Test1</td>
<td>None</td>
<td>Desktop</td>
<td >2014-06-30 18:22:39</td>
</tr>
<tr id="row_2">
<td>
<input type="button" id="btn_row_2" class="btn" value="Update">
</td>
<td id="text_row_2">Test2</td>
<td>None</td>
<td>Server</td>
<td>2014-03-30 16:20:15</td>
</tr>
</table>
And Javascript
$(document).ready(function(){
$(".btn").click(function(){
var id = this.id.substring(this.id.lastIndexOf("_") + 1);
alert($("#text_row_" + id).text());
});
});
<tr class="parent" id="other_app">
<td align="center">
<input type="button" name="btn_id" value="Update" onclick="UpdateRec(d1, 'Test1', 'None', 'Desktop', '2014-06-30')">
</td>
<td name="App_Name">Test1</td>
<td align="center">None</td>
<td align="center">Desktop</td>
<td align="center">2014-06-30 18:22:39</td>
</tr>
Since you have already written a javascript function call why not just include the things you need right?
This is simplier but not the best practise.

Categories

Resources