ng-repeat, do not update table after data was updated? - javascript

I have a table that is populating with ng-repeat, and I also have navigation button that change table data. Before I had custom filter on the data in html file, like so:
ng-repeat="car in carList | filter:tableFilter"
Even so it worked, it slowed my website, a lot. So now I am updating my table data in my controller. But there is another problem, ng-repeat do not want to update. So no matter how much I will update my data, the table will be still the same.
How can I fix that?
Here is my ng-repeat:
<tbody>
<tr ng-repeat="car in sortedCarList">
....
Here is my nav-bar:
<section class="tab" ng-controller="TablePanelCtrl as panel">
<ul class="nav nav-pills car-navigation">
<li ng-class="{ active: panel.isSelected(1)}">
<a href ng-click="panel.selectTab(1); initCarList(1); resetActiveRow(); formattedTable(2); hideTypeBox()">Sort by Reviews</a>
</li>
<li ng-class="{ active: panel.isSelected(2)}">
<a href ng-click="panel.selectTab(2); initCarList(2); resetActiveRow() formattedTable(2); hideTypeBox()">Sort by Rating</a>
</li>
</ul>
initCarList() changes the data according to tab number.
formattedTable() filters table data according to filter values that can be changes by user.
UPDATE:
my formatting function:
$scope.formattedTable = function(index){
$scope.initCarList(index);
$scope.sortedCarList = [];
var carlistLength = $scope.carList.length;
for (var i=0;i<carlistLength;i++){ // just check every row if values passes user requirements
var rowBool = $scope.tableFilter($scope.carList[i]);
if (rowBool){
$scope.sortedCarList.push($scope.carList[i]);
}
}
}

While I agree with cerbrus that we need to see a bit more of your code, I'll take a stab in the dark : you call formattedTable(2) in both tabs, which means that your filtering never changes.

Related

How do I properly access and output API object and array data?

My issue is probably very simple, but I am a complete beginner studying Javascript and I have a project where I am supposed to create an anime info web application by fetching data from an API.
I have an issue with a "genres" category. It is listed as an array with various items and item names which I need to have displayed in a 'Genres' column on my page.
Here is an example of some of the data I need to retrieve.
genres array data: https://imgur.com/a/pApGU59
And here is an example of what my current output looks like:
e.g. column output: https://imgur.com/a/1rRwG5L
If I am supposed to accomplish this by means of a forEach loop, what would be the proper syntax? And if there is another or a simpler way, an example of that would also be appreciated.
Here is an example of how I was printing data into their respectful columns through JavaScript.
<div className="col-md-8">
<h2>${movie.title}</h2>
<ul className="list-group">
<li class="list-group-item">
<strong>Japanese Title:</strong> ${movie.title_japanese}
</li>
<li class="list-group-item">
<strong>Genres:</strong> ${movie.genres}
</li>
<li class="list-group-item">
<strong>Type:</strong> ${movie.type}
</li>
<li class="list-group-item">
<strong>Premiered:</strong> ${movie.premiered}
</li>
<li class="list-group-item">
<strong>Episodes:</strong> ${movie.episodes}
</li>
</ul>
</div>;
Of course, using "movie.genres.name" returns 'undefined' and just using "movies.genres" would print many [object Object] results into said column.
I would just need the "name" item of the genres array to be automatically filled into my Genres column upon loading up a page.
movie.genres is an array and you will have to loop it to get each property
let output = "";
for(let i=0;i< movie.genres.length; i++){
output+= movie.genres[i].name;
}

can we still apply filter from ui on the filteredlist based on the tab which is active in Angular js

I have the below two tabs and the ng-repeat which displays the data in the table.
<div class="details-table">
<ul class="table-toggle">
<li ng-class="{active: activeTab == 'personal'}">Personal</li>
<li ng-class="{active: activeTab == 'professional'}"><a href="#" ng-click="setActiveTab('professional')" class="" >Professional</a></li>
</ul>
<div class="scrollable-table-box">
<table>
//rest of the code here.......
<tr ng-repeat="item in pagedItems[currentPage] | filter:item.usertype = (activeTab == 'Personal' ? 'Personal': 'Professional')">
// rest of the code here........
</table>
</div>
when user clicks on the personal tab the pagedItems which is already a filtered list should be further filtered by item.usertype == "personal" and similarly when clicked on professional tab it should be further filtered by item.usertype == "professional". The reason I want to do it this way is original collection pagedItems (which is filtered using search input earlier) will always be the same and only based on the tab the data is filtered to show in the ui.
I tried implementing this by adding another level of filter in the ng_click() of each tab. But this way either I need to maintain an additional variable or overwrite the same variable which for me doesn't look like an elegant solution.
Is it possible to do the same from html using ng directive filter?

ng-repeat uknown number of items

Okay this seems fairly odd but i am allowing my users to create any number of category / subcategories they want
When i pull the category data out it might look something like this:
as you can see from the above example the sub categories can have subcategories this list tree could go on forever.
Now trying to make the menu i have a simple list:
<ul class="nav dker">
<li ui-sref-active="active">
<a ui-sref="app.ui.jvectormap" href="#/app/ui/jvectormap">
<span translate="aside.nav.components.ui_kits.VECTOR_MAP" class="ng-scope">Vector Map</span>
</a>
</li>
</ul>
The issue here is that i don't know how many times i have to repeat the subcategories which makes it impossible for me to know when to check for it?
i hope you know where im going with this how can i make a reliable list that follows the array pattern above when i don't know how many levels there are?
You are going to want to create a recursive template which will essentially look something like this:
<div data-ng-include="'displaySubcategory.html'"></div>
where displaySubcategory.html contains the ng-repeat and a recursive call to itself.
<div data-ng-repeat="category in category.subcategories">
<h1>{{category.name}}</h1>
<div data-ng-include="'displaySubcategory.html'"></div>
</div>
The idea is that everytime you call ng-repeat you are creating a scope around the child element, so a call to {{category}} will display the lowest level (current child) of the tree/data structure.

Angularjs : What is the best way to let Binding Data later update

If I have a ng-repeat directive bind with my initial data,
<!-- list 1-->
<li ng-repeat="data in datas">{{data.name}}</li>
and I change the data by another ng-repeat and ng-model directive,
<!-- list 2-->
<li ng-repeat="data in datas">
<input type="text" ng-model="data.name">
</li>
In Angular way, any method can do list 1 ng-repeat data refresh not immediately (after I click a Save button)?
<button ng-click="save()">Save</button>
You can use a second (temporary) clone to make the changes and copy the changes over to the actual object using angular.copy.
The actual list:
<ul><li ng-repeat="item in items">
{{item.name}} (id: {{item.id}})
</li></ul>
Edit the list:
<ul><li ng-repeat="item in tempCopy">
<input type="text" ng-model="item.name" />
</li></ul>
<button ng-click="persistChanges()">Save</button>
<button ng-click="discardChanges()">Discard</button
In your controller:
/* Persist the changes */
$scope.persistChanges = function () {
angular.copy($scope.model.tempCopy, $scope.model.items);
};
/* Discard the changes */
$scope.discardChanges = function () {
angular.copy($scope.model.items, $scope.model.tempCopy);
};
See, also, this short demo.
Finally, there is a similar example on the Angular docs on angular.copy.
It seems you are creating new items within datas by extending the array by one element? If this is so, why not use a different model for the form and push the result onto the array data when the save button is clicked?
Similarly, when editing an item, clone the array element and make it the model for the resulting form, then modify the original array element when the save button is clicked.

create functions for html hyperlinks to query database

i have created a web application in asp.net with C# and MySql Database at the backend called simple online shopping,
I have created a cascading dropdown horizontal navigation menu (navmenu) with unordered lists and html hyperlinks. i have 2 pages Homepage.aspx and ShowProducts.aspx.
Navmenu contains the categories in an hierarchy and the products and category data is stored in databases.
when a hyperlink is clicked on the navmenu i wanted the to show the products contained in the category in the ShowProducts.aspx
i have list item in unordered lists in navmenu in hierarchy of
1.Clothing
1.1:Mens
1.1.1:Shirts
1.1.2:Trousers
1.1.3:WinterWear
1.2:Womens
1.2.1:Casual Wear
1.2.2:Jeans
1.2.3 Bags
I have wrote the hyperlinks as:
<ul id="navmenu">
<li>Clothing
<ul class="sub1">
<li>Mens
<ul class="sub2">
<li>Shirts</li>
<li>Mens Trousers</li>
<li>Jeans</li>
<li>Winter wear</li>
<li>Watches & Bags</li>
<li>Footwear for men</li>
</ul>
</li>
<li>Womens
<ul class="sub2">
<li>Sarees & Dresses</li>
<li>Jewellery</li>
<li>Womens Jeans</li>
<li>Womens Bags</li>
<li>Footwear for Women</li>
</ul>
</li>
</ul>
</li>
I am using Devart linqconnect(Linq To MySql). some one told me to use querystrings to pass data between pages but it contains images and data so i wanted to use repeaters to show them in hiererchy. Could add an onlick handler to<a> tag so and write a javascript function
such as
<script type="text/javascript">
Function showproducts() {
-- Linq code to retrieve products based on id --
}
could anybody suggest a good method or tell how can i achieve this.
thanks.. please help me on this..
Use Repeater to render list of links on your page. Add query string value like ShowProduct.aspx?id=1 to every link.
On ShowProduct Page, use linq to retrieve data from DB:
using(DataContext db = new DataContext())
{
int id = Convert.ToInt32(Request.QueryString["id"]);
var product = db.products.Where(p => p.id = id).FirstOrDefault();
if(product != null)
{
//do your job here with product data
}
}

Categories

Resources