How to set limit to pagination? - java

I want to use pagination and I did it but I am not getting that part I want. I want through some solution but still not getting.
What am I doing is:
query part in model:
public static Page<User> page(int page, int pageSize, String sortBy, String order, String filter) {
return find.where()
.ilike("name", "%" + filter + "%")
.orderBy(sortBy + " " + order)
.findPagingList(pageSize)
.getPage(page);
}
view part:
#(currentPage: com.avaje.ebean.Page[User], currentSortBy: String, currentOrder: String, currentFilter: String,ud: String)
#****************************************
* Helper generating navigation links *
****************************************#
#link(newPage:Int, newSortBy:String) = #{
var sortBy = currentSortBy
var order = currentOrder
if(newSortBy != null) {
sortBy = newSortBy
if(currentSortBy == newSortBy) {
if(currentOrder == "asc") {
order = "desc"
} else {
order = "asc"
}
} else {
order = "asc"
}
}
// Generate the link
routes.paging.pag(newPage, sortBy, order, currentFilter)
}
#header(key:String, title:String) = {
<a class="#key.replace(".","_") header #if(currentSortBy == key) #{if(currentOrder == "asc") "headerSortDown" else "headerSortUp"}" href="#link(0,key)">#title</a>
}
#main("welcome to EXTR ") {
<div class="container-narrow">
<div class="masthead">
<ul class="nav nav-pills pull-right">
<li><a href="#routes.paging.extrad(ud)" >Extr resume</a></li>
<li>Logout</li>
</ul>
<h3 class="muted">
welcome admin</b>
</h3>
</div>
<hr><div align="center">
<h1> #currentPage.getTotalRowCount() user found</h1>
#if(currentPage.getTotalRowCount == 0) {
<div class="well">
<em>Nothing to display</em>
</div>
} else {
<table border="1" >
<tr>
<th>#header("name","name")</th>
<th>#header("email","email")</th>
<th>#header("","")</th>
</tr>
<tbody>
#for(user <- currentPage.getList) {
<tr>
<td>#user.name</td>
<td>
#user.getEmail()
</td>
<td>delete</td>
</tr>}
</tbody>
</table>
<div id="pagination" class="pagination">
<ul>
#if(currentPage.hasPrev) {
<li class="prev">
← Previous
</li>
} else {
<li class="prev disabled">
<a>← Previous</a>
</li>
}
#for(i <- 0 until currentPage.getTotalPageCount()){
<li>#(i+1)</li>
}
#if(currentPage.hasNext) {
<li class="next">
Next →
</li>
} else {
<li class="next disabled">
<a>Next →</a>
</li>
}
</ul>
</div>
}
</div>
</div>
}
and what am I getting is:
but what I want is
at a time I want to show to 5 pages and when click on 5th page it again get to 5-10 pages. But my pagination is getting all pages at a time.
I want the numbers to be displayed in this format..
1 2 3 4 5 ^
where if I press 5, then it should display from 5 to 10
5 6 7 8 9 10
till the max records are available. I just want to know a how to do this for displaying numbers.
give me some idea to do this.

Related

Restrict number of table pages in Thymeleaf + Java

I followed this tutorial:
https://www.youtube.com/watch?v=Aie8n12EFQc
Pagination works. How can I restrict the number of page links below the table? If there is to much records, it can be to much numbers.
Numbers are below closing /table tag, it's not inside the table, but below (like in tutorial).
I know there are some solutions in Stackoverflow but they are mostly in php code. Can it be done somehow with java server-side or with Thymeleaf? Or can you send me an already good answered question on this topic.
My thymeleaf:
<div th:if = "${totalPages > 1}">
<nav aria-label="Page navigation" class="paging">
<ul class="pagination">
<li class="page-item">
<a class="page-link" th:href="#{/customer_list?pageNo=1}">First</a>
</li>
<li class="page-item">
<a class="page-link" th:href="#{/customer_list?pageNo={currentPage}(currentPage=${currentPage-1})}">Previous</a>
</li>
<li th:each="i: ${#numbers.sequence(1, totalPages)}" th:classappend="${i==currentPage} ? 'page-item active' : 'page-item'">
<a class="page-link" th:href="#{/customer_list?pageNo={i}(i=${i})}">[[${i}]]</a>
</li>
<li class="page-item">
<a class="page-link" th:href="#{/customer_list?pageNo={currentPage}(currentPage=${currentPage+1})}">Next</a>
</li>
<li class="page-item">
<a class="page-link" th:href="#{/customer_list?pageNo={totalPages}(totalPages=${totalPages})}">Last</a>
</li>
</ul>
</nav>
</div>
Controller method in Java: (pageSize only 2, for experimenting)
#GetMapping("/customer_list")
public String customerList(#RequestParam(name = "pageNo", required = false) Integer pageNo ,Model model)
{
if(pageNo == null)
pageNo = 1;
int pageSize = 2;
try
{
Page<CustomerDTO> pageOfCustomers = customerService.findPaginated(pageNo, pageSize);
List<CustomerDTO> listOfCustomers = pageOfCustomers.getContent();
model.addAttribute("listOfCustomers", listOfCustomers);
model.addAttribute("currentPage", pageNo);
model.addAttribute("totalPages", pageOfCustomers.getTotalPages());
model.addAttribute("totalItems", pageOfCustomers.getTotalElements());
} catch (Exception e)
{
log.error("Error in retrieving the list of customers!", e);
e.printStackTrace();
}
return "customer_list";
}
Have a look to this post : pagination
It should answer your question.
In a nutshell you add to your template an array with the length equals to the number of pages.
Then with some tests you display either dots or the link to the page.
It will give something like :
<< < ... 3456789 ... > >>

How to handle 2 similar unordered dropdown lists which has same class in selenium webdriver?

First dropdownlist HTML Code:
<div id="boundlist-1018" class="x-boundlist x-boundlist-floating x-layer x-boundlist-default x-boundlist-above" tabindex="-1" style="left: 655px; top: 356px; height: auto; width: 295px; z-index: 19001;">
<div id="boundlist-1018-outerEl" class="x-boundlist-outer-ct" style="overflow:auto">
<div id="boundlist-1018-listEl" class="x-boundlist-list-ct" style="overflow: auto; height: auto;">
<ul>
<li class="x-boundlist-item x-boundlist-selected x-boundlist-item-over" role="option">aa</li>
<li class="x-boundlist-item" role="option">eGDP132</li>
<li class="x-boundlist-item" role="option">oracle</li>
</ul>
</div>
</div>
Second dropdownlist HTML Code:
<div id="boundlist-1012-listEl" class="x-boundlist-list-ct" style="overflow: auto; height: 88px;">
<ul>
<li class="x-boundlist-item x-boundlist-selected" role="option">eGDP132</li>
<li class="x-boundlist-item" role="option">Buddy_agt</li>
<li class="x-boundlist-item" role="option">New_agt</li>
</ul>
</div>
Frist Dropdown list code:
List<WebElement>internal_agt=driver.findElements(By.xpath(".//[#class='xboundlist-item']"));
for (WebElement ele : internal_agt)
{
String str = ele.getText();
System.out.println("Dropdown values are***************" +str);
}
To get first dropdown value use the x-path (//*[#class='x-boundlist-list-ct'])[1]/ul/li
List<WebElement>internal_agt=driver.findElements(By.xpath("(//*[#class='x-boundlist-list-ct'])[1]/ul/li"));
for (WebElement ele : internal_agt)
{
String str = ele.getText();
System.out.println("Dropdown values are***************" +str);
}
For second list use the x-path (//*[#class='x-boundlist-list-ct'])[2]/ul/li
List<WebElement>internal_agt=driver.findElements(By.xpath("(//*[#class='x-boundlist-list-ct'])[2]/ul/li"));
for (WebElement ele : internal_agt)
{
String str = ele.getText();
System.out.println("Dropdown values are***************" +str);
}
IWebElement[] options1 = driver.findElements(By.Xpath(".//div[#id='boundlist-1018-listEl']/ul/li")).ToArray();
IWebElement[] options2 = driver.findElements(By.Xpath(".//div[#id='boundlist-1012-listEl']/ul/li")).ToArray();
foreach(IWebElement q in options1)
{
System.out.println(q.Text);
}
foreach(IWebElement q in options2)
{
System.out.println(q.Text);
}
List options1 =
driver.findElements(By.cssSelector("#boundlist-1018-listEl .xboundlist-item"));
List options2 =
driver.findElements(By.cssSelector("#boundlist-1012-listEl .xboundlist-item"));
these should work
I was stuck in a similar situation. Below code worked for DropDown like magic. Hope this helps.
public void selectValueFromDropDown(By Selector, String value) { //You can change "By Selector" to "WebElement element"
List<WebElement> DropDownlist = driver.findElements(Selector);
System.out.println(DropDownlist.size());
Assert.assertEquals(DropDownlist.size(), 12, "Provinces List does not match: ");
for (int i = 0; i < DropDownlist.size(); i++) {
String strText = DropDownlist.get(i).getText();
if(strText.contentEquals(value)) {
DropDownlist.get(i).click();
break; //Comment this statement to print all values from DropDown on list
}
System.out.println("Province " + i + "==========> " + strText);
}
}
Try:
List<WebElement>internal_agt=driver.findElements(By.cssSelector("ul"));

parsing a table with jsoup

I'm trying to extract the e-mail adress and the phone number from a linkedin profile using jsoup, each of these informations is in a table. I have written a code to extract them but it doesn't work, the code should work on any linkedin profile. Any help or guidance would be much appreciated.
public static void main(String[] args) {
try {
String url = "https://fr.linkedin.com/";
// fetch the document over HTTP
Document doc = Jsoup.connect(url).get();
// get the page title
String title = doc.title();
System.out.println("Nom & Prénom: " + title);
// first method
Elements table = doc.select("div[class=more-info defer-load]").select("table");
Iterator < Element > iterator = table.select("ul li a").iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next().text());
}
// second method
for (Element tablee: doc.select("div[class=more-info defer-load]").select("table")) {
for (Element row: tablee.select("tr")) {
Elements tds = row.select("td");
if (tds.size() > 0) {
System.out.println(tds.get(0).text() + ":" + tds.get(1).text());
}
}
}
}
}
here is an example of the html code that i'm trying to extract (taken from a linkedin profile)
<table summary="Coordonnées en ligne">
<tr>
<th>E-mail</th>
<td>
<div id="email">
<div id="email-view">
<ul>
<li>
adam1adam#gmail.com
</li>
</ul>
</div>
</div>
</td>
</tr>
<tr class="no-contact-info-data">
<th>Messagerie instantanée</th>
<td>
<div id="im" class="editable-item">
</div>
</td>
</tr>
<tr class="address-book">
<th>Carnet d’adresses</th>
<td>
<span class="address-book">
<a title="Une nouvelle fenêtre s’ouvrira" class="address-book-edit" href="/editContact?editContact=&contactMemberID=368674763">Ajouter</a> des coordonnées.
</span>
</td>
</tr>
</table>
<table summary="Coordonnées">
<tr>
<th>Téléphone</th>
<td>
<div id="phone" class="editable-item">
<div id="phone-view">
<ul>
<li>0021653191431 (Mobile)</li>
</ul>
</div>
</div>
</td>
</tr>
<tr class="no-contact-info-data">
<th>Adresse</th>
<td>
<div id="address" class="editable-item">
<div id="address-view">
<ul>
</ul>
</div>
</div>
</td>
</tr>
</table>
To scrape email and phone number, use css selectors to target the element identifiers.
String email = doc.select("div#email-view > ul > li > a").attr("href");
System.out.println(email);
String phone = doc.select("div#phone-view > ul > li").text();
System.out.println(phone);
See CSS Selectors for more information.
Output
mailto:adam1adam#gmail.com
0021653191431 (Mobile)

How to verify checkboxes on maximum 2 in a row

How to verify the checkbox on a section, maximum 10, 2 in a row, and the checkbox is checked?
To verify checkbox is checked
Boolean isCheckboxChecked =
driver.findElement(By.xpath("//*[contains(#class, 'checkboxSection')]//input[#type='checkbox']")).isSelected();
To verify max checkbox on that section is 10
Boolean isCheckboxMaxTen =
driver.findElements(By.xpath("//*[contains(#class, 'checkboxSection')]//input[#type='checkbox']")).size() <= 10;
To verify max 2 checkbox in a row
Any idea how to verify max 2 checkbox in a row
if ((isCheckboxChecked == true) && (isCheckboxMaxTen == true) && (isCheckboxTwoInARow == true)){
// Pass
}
else
{
// Fail
}
Added html code
<form name="formApp" method="post" action="/apps/GeneralManager">
<div class="USK_A">
<img class="BOL_B" alt="" src="http://apps.com"/>
<div class="MOV_HOL_A">
<p class="bold">
<ul class="form_movie">
<li>
<input type="checkbox" checked="checked" value="3000000190795" name="point_id"/>
Movie_A
</li>
</ul>
<p class="mv5">
</div>
</div>
</form>

Unable to parse value from HTML using jsoup

I'm relatively new to using jsoup, and I can't seem to find the correct query to parse out the value I'm looking for. The HTML is as follows.
<img src='http://rootzwiki.com/public/style_images/ginger/t_unread.png' alt='New Replies' /><br />
</a>
</td>
<td class='col_f_content '>
<h4><a id="tid-link-12251" href="http://rootzwiki.com/topic/12251-romlte-rootzboat-403-v61/" title='View topic, started 17 December 2011 - 09:32 AM' class='topic_title'>[ROM][LTE] RootzBoat 4.0.3 V6.1</a></h4>
<br />
<span class='desc lighter blend_links'>
Started by <a hovercard-ref="member" hovercard-id="5" class="_hovertrigger url fn " href='http://rootzwiki.com/user/5-birdman/'>birdman</a>, 17 Dec 2011
</span>
<ul class='mini_pagination'>
<li><a href="http://rootzwiki.com/topic/12251-romlte-rootzboat-403-v61/" title='Go to page 1'>1</a></li>
<li><a href="http://rootzwiki.com/topic/12251-romlte-rootzboat-403-v61/page__st__10" title='Go to page 2'>2</a></li>
<li><a href="http://rootzwiki.com/topic/12251-romlte-rootzboat-403-v61/page__st__20" title='Go to page 3'>3</a></li>
<li><a href="http://rootzwiki.com/topic/12251-romlte-rootzboat-403-v61/page__st__1990" title='Go to page 200'>200 →</a></li>
</ul>
</td>
<td class='col_f_preview __topic_preview'>
<a href='http://rootzwiki.com/topic/12251-romlte-rootzboat-403-v61/' class='expander closed' title='Preview this topic'> </a>
</td>
<td class='col_f_views desc blend_links'>
<ul>
<li>
<span class='ipsBadge ipsBadge_orange'>Hot</span>
1,999 replies
</li>
<li class='views desc'>180,213 views</li>
</ul>
</td>
<td class='col_f_post'>
<a href='http://rootzwiki.com/user/49940-jakeday/' class='ipsUserPhotoLink left'>
<img src='http://rootzwiki.com/uploads/profile/photo-thumb-49940.jpg' class='ipsUserPhoto ipsUserPhoto_mini' />
</a>
<ul class='last_post ipsType_small'>
<li><a hovercard-ref="member" hovercard-id="49940" class="_hovertrigger url fn " href='http://rootzwiki.com/user/49940-jakeday/'>jakeday</a></li>
<li>
<a href='http://rootzwiki.com/topic/12251-romlte-rootzboat-403-v61/page__view__getlastpost' title='Go to last post'>Today, 04:20 AM</a>
</li>
</ul>
</td>
I need to parse out birdman from there. I know that once I've defined the element, I can get "birdman" out with author.text();, but I cant figure out how to define the author element. I thought perhaps the following block of code would work, but as I mentioned, I'm pretty new to jsoup and html and it obviously didnt work. Theres nothing wrong with the connection, and jsoup is working for the other values I parsed out.
TitleResults titleArray = new TitleResults();
Document doc = null;
try {
doc = Jsoup.connect(Constants.FORUM).get();
} catch (IOException e) {
e.printStackTrace();
}
Elements threads = doc.select(".topic_title");
for (Element thread : threads) {
titleArray = new TitleResults();
//Thread title
threadTitle = thread.text();
titleArray.setItemName(threadTitle);
//Thread link
String threadStr = thread.attr("abs:href");
String endTag = "/page__view__getnewpost"; //trim link
threadStr = new String(threadStr.replace(endTag, ""));
threadArray.add(threadStr);
titleArray.setAuthorDate("Author/Date");
results.add(titleArray);
}
Elements authors = doc.select("a[hovercard-ref]");
for (Element author : authors) {
if (author.attr("abs:href").contains("/user/")){
Log.d("POC", "SUCCESS " + author.attr("abs:href"));
} else {
Log.d("POC", "FAILURE " + author.text());
}
}
}
I think you're thinking too hard ;)
To get the birdman portion of the link, just use the following:
Elements authors = doc.select("a");
for (Element author : authors) {
Log.d("POC", author.text());
}
The "a" retrieves all links. After that you can just use the .text() like you said to retrieve the value.
Selvin answered it in the comments. I wasnt getting the source correctly and it was causing errors.
http://pastebin.com/xfUQkGw0

Categories