AngularJS Apply filters in multiple fields - html

My target is to search my notes by Title and truncate Body by a letters or words limit. I don't know if its possible to do that and how. I don't want to mess up with any custom filters , directives etc.
This is a idea of the html:
<section class="notespage" ng-controller="NotesController">
<h2>Notes list</h2>
<div>
Search:
<input type="text" ng-model="searchText">
</div>
<div class="notesleft">
<div class="notelist">
<div ng-repeat="note in notes | filter:searchText | truncate:{body:letterLimit}">
<div><h3>{{note.title}}</h3></div>
<div>{{note.body}}</div>
</div>
</div>
</div>
</section>
Where this should be ? In NotesController ?
$scope.letterLimit = 20 ;

Since you don't want to "mess up" with custom filters etc, you can use the built-in limitTo filter to limit the number of characters displayed as note.body:
$scope.letterLimit = 20;
<div>Search:<input type="search" ng-model="searchText" /></div>
<div ng-repeat="note in notes | filter:searchText">
<div><h3>{{note.title}}</h3></div>
<div>{{note.body | limitTo:letterLimit}}</div>
<div>
See, also, this short demo.

Related

How to hide something when no search result found in angular

In my homepage I have a search bar and a list of user favorite cards. Only If there is no favorited cards on the initial browser load and when a user removed all their favorite cards then it'll show a banner "Add a favorite...".
The problem I have right now is the banner is displaying when I search something on the search-bar and found no favorited card. How do I fix that?. I don't want to show my banner if there is no search result found.
If I do favorites.size == 0 instead then the banner is not showing up at all when browser load or user removed all their cards.
<div class="input">
<form class="search-form">
<input #searchValue id="input-field" class="input-field" type="search"
name="insightSearchFilter" (ngModelChange)="filterChanged($event)" [formControl]="inputCtrl"/>
<p class="hint" *ngIf="inputCtrl.value">
<strong> {{ favorites?.length }} Search Result for </strong><em>"{{ inputCtrl?.value }}".</em>
<strong> The Results listed below are exact matches for your query.</strong>
</p>
</form>
</div>
<div *ngIf="favorites">
<div *ngIf="favorites.length > 0">
<app-card-list [cards]="favorites"></app-card-list>
</div>
<div *ngIf="favorites.length == 0" class="empty-faves-container">
<div class="add-faves-container">
<div class="add-faves-ico"></div>
<div class="text">Add a Favorite</div>
</div>
</div>
</div>
Since you are filtering this.favorites in filterChanged methods. You should apply check for search text also along with length check.
like
(favorites.length == 0 && !(inputCtrl.value))

Passing a variable to href

I have this code that is getting data from a cell in a table
<div class="p-s-header">TITLE</div>
<div class="p-content">
<div class="row">
<div class="col-md-12 col-xs-12 col-sm-12">
<span di="76" ></span><br />
<span di="77" ></span><br />
</div>
</div>
</div>
This is producing:
TITLE
(data in cell 76)
(DATA in cell 77)
Now, data in cell 77 is a a link that is too long for the space, so I want to add the work "Click here" (hyperlinked) instead of showing the link.
So I wanted to change code, so output looks like:
TITLE
(data in cell 76)
Click here
"Click here" shoudl be built with the data in cell 77. I didn't code this but it seems the code to get the data from that cell is:
How would I build it?
I tried a few options, but nothing seems to work:
For example something like this:
<a href=<span di="77" ></span>Link</a><br />
thanks for your help
Try this, I hope this will encourage you on studying further how HTML and JS work together. (PHP is too advanced right now).
Change your code for this:
<div class="p-s-header">TITLE</div>
<div class="p-content">
<div class="row">
<div class="col-md-12 col-xs-12 col-sm-12">
<span id="cell76" ></span><br />
<span id="cell77" >http://www.google.com</span><br />
</div>
</div>
</div>
The attribute di doesn't exist, it is id and id's cannot start with numbers or be numbers
At the bottom of the code that you added write the following:
<script>
var link = document.getElementById('cell77').innerHTML;
document.write(''+link+'');
</script>
Here is the jsfiddle
https://jsfiddle.net/cz1Ltw3x/1/

XPath to select link containing text?

I tried to use this XPath:
//*[contains(normalize-space(text()),'Jira')]
Also tried:
//*[contains(text(),'Jira')]
In the below HTML example, there is space before and after text "Jira". I am not able to click on the link:
<a href="#/crm/usergroup-edit?id=572a3c84e4b07f6189958700"
ng-repeat="gp in groups | filter : userGroupSearch | orderBy:'-name':1"
class="ng-scope">
<div class="inventoryPanel" ng-style="myStyle" style="width: 15.8%;">
<h4 class="ng-binding">
<div class="groupIcon G">
<div class="text ng-binding">P</div>
</div>Jira
</h4>
</div>
</a>
The following XPath will select all a elements whose string value contains a Jira substring:
//a[contains(.,'Jira')]

Better way in bootstrap to display columns instead of using php to create rows after every 3

Sorry for the question title. I really didn't know how to describe it best in a short sentence.
Basically I want to loop through each of my images and display them. However when doing the front end I did this:
<div class="row">
<div class="col-sm-4">
<img .../>
</div>
<div class="col-sm-4">
<img .../>
</div>
<div class="col-sm-4">
<img .../>
</div>
</div>
<div class="row">
<div class="col-sm-4">
<img .../>
</div>
<div class="col-sm-4">
<img .../>
</div>
<div class="col-sm-4">
<img .../>
</div>
</div>
Because there can only be 3 columns in a row for my design this worked fine. However now if i want to do this in php i have to kind of check the $count of the loop. If its a multiple of 3 display
<div class="row">
and if its a multiple of 3 + 1 then show
</div>
to close the row.
IS there any way I can make the mark up look the same as the example above but using this html below:
<div class="row">
<div class="col-sm-4">
<img .../>
</div>
<div class="col-sm-4">
<img .../>
</div>
<div class="col-sm-4">
<img .../>
</div>
<div class="col-sm-4">
<img .../>
</div>
<div class="col-sm-4">
<img .../>
</div>
<div class="col-sm-4">
<img .../>
</div>
</div>
Its just making my mark up look very messy. Is there a better way in bootstrap to do this without using php at all?
Thanks
You'll want to include conditions in your PHP loop so that PHP knows when to add in the row.
Below, I've laid out an untested set of PHP code that will hopefully explain where you're going wrong.
<?php
$images = array(...);
$images_per_row = 3;
// Here is where we determine how the images
// should be divided into the rows.
$total_images = count($images);
$total_rows = ceil($total_images / $images_per_row);
// Loop through each `row`
for ($row=0; $row<$total_rows; $row++)
{
// Open the `row`
echo '<div class="row">';
// We have an `offset` to avoid displaying the same image
// multiple times in the loop.
$offset = $row * $images_per_row;
// Loop through each `col-sm-4` and `img`
for ($i=0; $i<$images_per_row; $i++)
{
echo '<div class="col-sm-4">';
echo "<img src='{$images[$offset + $i]}' />";
echo '</div>';
}
// Closing the `row`
echo '</div>';
}
There are countless ways to do this. You can even do it all within a single loop if you use proper conditions within the loop.
The point is, you need a way of telling PHP "Hey, we need to divide all of this into thirds or we're never going to get anywhere with this."
Side note, PHP + HTML is a very ugly marriage. Please avoid it at all costs (unless you only have 10 minutes and you're trying to help someone on Stack Overflow).

Multiline regular expression in Autoit3

I am trying to match multi-line HTML source code with a regular expression (using AutoIt). HTML source code to match:
<li class="mission">
<div>
<div class="missionTitle">
<h3>Eat a quarter-pounder with cheese</h3>
<div class="missionProgress">
<span>100%</span>
<div class="missionProgressBar" style="width: 100%;"></div>
</div>
</div>
<div class="missionDetails">
<ul class="missionRewards">
<li class="rewardCash">5,000–8,000</li>
<li class="rewardXP">XP +5</li>
</ul>
<div class="fightItems clearfix">
<h5><span>Prerequisites:</span></h5>
<div class="fightItemsWrap">
<div class="fightItem tooltip" title="Sunglasses" data-attack="Attack: 2" data-defence="Defence: 2">
<img src="/img/enhancement/3.jpg" alt="">
<span>× 1</span>
</div>
<div class="fightItem tooltip" title="Broad Shoulders" data-attack="Attack: 0" data-defence="Defence: 3">
<img src="/img/enhancement/1003.jpg" alt="">
<span>× 1</span>
</div>
<div class="fightItem tooltip" title="Irish Fond Anglia" data-attack="Attack: 4" data-defence="Defence: 8">
<img src="/img/enhancement/2004.jpg" alt="">
<span>× 1</span>
</div>
</div>
</div>
<form action="/quest/index/i/kdKJBrgjdGWKqtfDrHEkRM2duXVn1ntH/h/c0b2d58642cd862bfad47abf7110042e/t/1336917311" method="post">
<input type="hidden" id="id" name="id" value="17"/>
<button class="button buttonIcon btnEnergy"><em>5</em></button>
</form>
</div>
</div>
</li>
It is present multiple times on a single page (but items within <div class="fightItems clearfix">...</div> vary).
I need to match
<h3>Eat a quarter-pounder with cheese</h3>,
the first span <span>100%</span> and
<input type="hidden" id="id" name="id" value="17"/>.
Expected result (for every occurrence on a page):
$a[0] = "Eat a quarter-pounder with cheese"
$a[1] = "100%"
$a[2] = "17"
What I came up with:
(?U)(?:<div class="missionTitle">\s+<h3>(.*)</h3>\s+<div class="missionProgress">\s+<span>(.*)</span>)|(?:<form .*\s+.*<input\stype="hidden"\sid="id"\sname="id"\svalue="(\d+)"/>\s+.*\s+</form>)
But that leaves some array-items empty. I also tried the (?s) flag, but then it only captures first occurrence (and stops matching after).
I had not to use . to match words or integers, because of the (?s) flag. The correct regex is:
(?U)(?s)<div class="missionTitle">\s+<h3>([\w\s]+)</h3>(?:.*)<div class="missionProgress">\s+<span>(\d+)%</span>(?:.*)<input.* value="(\d+)"/>
Regular expression to match multi-line HTML source code:
As per documentation;
\R matches newline characters (?>\r\n|\n|\r),
dot . does not (unless (?s) is set).
\s matches white space characters.
Generally some combination is required (like \R\s*?).
Non-capturing groups are redundant (match without capturing instead).
If uniquely enclosed, single characters may be excluded instead (like attribute="([^"]*?)" for text between double-quotes).
Example (contains double-quotes; treat as per Documentation - FAQ - double quotes):
(?s)<div class="missionTitle">.*?<h3>(.*?)</h3>.*?<div class="missionProgress">.*?<span>([^<]*?)</span>.*?<input type="hidden" id="id" name="id" value="([^"]*?)"/>
Visual explanation:
If regular expressions should be used on HTML (beyond simple listings like this) is a different question (been, done, T-shirt).