How to skip undefined item in ng-repeat using a filter? - angularjs-ng-repeat

Please consider this plnkr:
http://plnkr.co/edit/EGSiUxkmqwBhLVPgatRp
The 10th item should be filtered out.. I know how to filter on an item's property but i cant seem to be able to get it working for the item itself.
please help

A cheat would be to filter by an empty string:
<li class="animate-repeat" ng-repeat="friend in friends | filter:{name : ''}">
But hardly ideal.
Another way would be to define your own filter that only returns items that are defined - something like:
.filter('removeUndefined', function(){
return function(listitems){
var results = [];
angular.foreach(listitems, function(item, key){
if(item != undefined){
results.push(item);
}
});
return results;
}
})

Related

Laravel 8 & Yajra Datatables complicated search

I have a datatable that fetches orders and is working and displaying properly. I show the orders to the users that initiated them. Then they can only search on their owns orders. Now I need to display a message to a user, if an order was found but it was initiated by another user, instead of displaying an empty result in the datatable. This will happen after typing in the search box and not when loading the datatable in the beggining. The problem is that the query already filters the results by user id so I cannot change it during manual search.
I can display code if needed but the function is quite big and I don't really need code but the logic/way of doing that.
Do you have any suggestions on how I could accomplish this?
Well, maybe not the best way to do it but that's how I solved it:
In the controller, I check for the search field, and run a query on the relationship but only on the orders that have different seller than the logged in user:
$otherSeller = "";
if(!empty($request->search['value']))
{
$ordersOtherSeller = Order::where('status_id', "!=", 3)->where('seller_id', '!=', $loggedUser->id)->whereHas('user', function ($q) use ($request){
$q->searchFullName($request->search['value']);
})->first();
if($ordersOtherSeller != NULL && $ordersOtherSeller->count() > 0)
$otherSeller = $ordersOtherSeller->user->full_name . ' ' . $ordersOtherSeller->seller->full_name;
}
And I set a custom variable with the table's json:
...
->with('otherSeller', $otherSeller)
->make(true);
Then on the datatable jquery drawCallBack, I check for a populated string that guarantees that the result is not returned by a query from the current user:
fnDrawCallback: function( oSettings ) {
var api = this.api();
if(api.ajax.json().otherSeller != "")
{
$('#alert-info span').text(api.ajax.json().otherSeller);
$('#alert-info').show();
}
else
$('#alert-info').hide();
},
And last is the toggling of the materialert element with updated text:
<div id="alert-info" class="materialert info" style="display: none;">
<i class="material-icons">info</i> <span></span>
<button type="button" class="close-alert">×</button>
</div>

Shopify: How can I render the recommended products?

I am trying to use Shopify Ajax API to get recommended products inside the cart. I am able to get the recommended product's json but not the section rendering.
The script (note section_id):
jQuery.getJSON('/cart.js', function(cart) {
// first recommendation
jQuery.getJSON("/recommendations/products.json?product_id=" + cart.items[0].product_id + "&limit=6&section_id=recommended_first", function(
response
) {
var recommendedProducts = response.products;
}
});
})
The HTML:
<div id="recommended_first" class="upsell_product">
</div>
I get some messages in the console:
Error: ShopifyAnalytics.meta.page.pageType is empty: undefined
Fallback logic initiated
What am I missing? I didn't find any examples in the Shopify doc.
Thanks a lot!
Your code will not work because you have an extra } on line 7. Assuming the cart request returns valid data, the following code should work (also a good idea to check if the cart request returns any items before using the cart.items variable):
jQuery.getJSON('/cart.js', function(cart) {
jQuery.getJSON("/recommendations/products.json?product_id=" + cart?.items?[0]?.product_id + "&limit=6&section_id=recommended_first", function(response) {
var recommendedProducts = response.products;
var recommendedProductsHTML = "";
for (i = 0; i < recommendedProducts.length; i++) {
recommendedProductsHTML += `<div>${recommendedProducts[i].title}</div>`;
}
$("#recommended_first").html(recommendedProductsHTML);
});
});

Jsoup - Not able to get desired output , Help required

I am new to JSOUP. Was trying a few exercise and came across a scenario where i was not able to fetch the product links from the below url.
original URL - https://www.amazon.co.jp/gp/new-releases/digital-text/2275256051/ref=zg_bsnr_2275256051_pg_1?ie=UTF8&pg=1
Pasted the selected node for reference
<div class="zg_title">
僕が本当に好きな和食
</div>
my Code
Elements ele = doc.select("div.zg_title > a ");
for (org.jsoup.nodes.Element element : ele)
{
System.out.println(element.toString());
}
Required Output
https://www.amazon.co.jp/%E5%83%95%E3%81%8C%E6%9C%AC%E5%BD%93%E3%81%AB%E5%A5%BD%E3%81%8D%E3%81%AA%E5%92%8C%E9%A3%9F-%E7%AC%A0%E5%8E%9F-%E5%B0%86%E5%BC%98-ebook/dp/B01LYCVBW3/ref=zg_bsnr_2275256051_1
I get the correct output with xpath - "//div[#class='zg_title']//a/#href"
How to do this with Jsoup.
Here it is:
Elements ele = doc.select("div.zg_title > a");
for (org.jsoup.nodes.Element element : ele) {
System.out.println(element.absUrl("href"));
}
Things to check:
CSS query (you have an additional space in the query);
if you want to retrieve the href attribute you should use the element.absUrl("href") method.

read class attribute in CasperJs

I'm wondering whether its possible to get the class value of a li item, the html looks something like this:
<div id="cardsdeck">
<ul id="cards">
<li id="card-0" class="card-image card-shown" .... >
......
I'm trying to get card-show out of the li.
I'm unsure if this is what you're trying to do, but to get and array of the classes that an element has, you can use:
document.querySelector('#card-0').className.split(' ');
However, if you're trying to get elements that have the card-shown class, then you can use:
document.querySelector('.card-shown');
Edit: better suited for your comment below:
casper.then(function() {
var num = 0;
var shown = this.evaluate(function isShown(k) {
return document.querySelector('#cards li.card-shown').id == ('card-'+k);
}, num);
console.log(shown);
})
This will look for an element with the card-shown class and then check to see if the id matches card-k, with k being a number.

How to get values of jQuery Tags Input plugin

I use this jQuery Tags Input plugin:
jQuery Tags Input
but I can't get the values on my php file.
If you are trying to get the individual values using jQuery you can use:
(This is assuming the text input you made into a tag input has an id of "keywords")
$('#keywords').tagsInput({
'height':'auto',
'width':'350px',
'defaultText':'',
'delimiter': '|'
});
/*The delimiter option above overrides the default comma delimiter in the plugin allowing commas in tags if you prefer that...*/
var $keywords = $("#keywords").siblings(".tagsinput").children(".tag");
var tags = [];
for (var i = $keywords.length; i--;) {
tags.push($($keywords[i]).text().substring(0, $($keywords[i]).text().length - 1).trim());
}
/*Then if you only want the unique tags entered:*/
var uniqueTags = $.unique(tags);
alert(uniqueTags.toSource());
To get the list of emails from:
<input type="text" name="to_addresses" class="to_addresses" data-role="tagsinput" >
I use:
$emails = []
$.map($(".tagsinput span span"),function(e,i){
$emails.push($(e).text().trim());
})
Works for me, thought I'd share it.
$("#btn").click(function () {
var $tagWord = $("#tags_2").siblings(".tagsinput").children(".tag");
var tags = [];
for (var i = $tagWord.length; i--; ) {
tags.push($($tagWord[i]).text().substring(0, $($tagWord[i]).text().length - 1).trim());
}
/*Then if you only want the unique tags entered:*/
var uqTags = $.unique(tags);
alert(uqTags.toSource());
});
using jquery you can do it in one line:
$.map($('.tag span'),function(e,i){return $(e).text().trim();})
$("input").tagsinput('items')
["Amsterdam","Washington","Sydney","Beijing","Cairo"]
$("input").val()
"Amsterdam,Washington,Sydney,Beijing,Cairo"
can you provide an example of what is POSTed? you can do so by pointing your form to go to api.fatherstorm.com?query and copying the json data that it gives you
it change the hidden input value, and will post the data when you click submit , you can test it by a simple php script. print_r($_POST) to see it.
Based on sagivo answer you can write a function like this :
function getKeywords() {
return $.map($('.tag span'),function(e,i){
return $(e).text().trim();
});
}
That will return an array of keywords present in the input.
Like this [ "there", "it", "is" ]

Resources