How to insert last input as first table record in QwebKit - html

I'm using Qwebkit and I like to be able to insert into html table each data input that comes last
as first record (<tr><td>...my data ...</td></tr>) in to the table.
Here is my code this is only example :
ui.webView->page()->mainFrame()->setHtml("<html><body><p>HTML Table Test</p>"
"<table id=\"mainTable\" name=\"mainTable\" BORDER=1 BORDERCOLOR=RED></table>"
"</body></html>");
QWebElement body = ui.webView->page()->mainFrame()->documentElement();
QWebElement mainTable = ui.webView->page()->mainFrame()->findFirstElement("#mainTable");
mainTable.appendInside ("<tr><td>1111111<\/td></\tr>"); ///<-- this is i like to be last in the end
mainTable.appendInside ("<tr><td>2222222<\/td></\tr>"); ///<-- this is i like to be in the middle
mainTable.appendInside ("<tr><td>3333333<\/td></\tr>"); ///<-- this is i like to be in the first
The content of the records are coming dynamically and not as I show here, so I can't do it hard coded; in short I need LIFO algorithm here ..
How should I do that ?

The QWebElement::appendInside method add the parameter to the end of the web element.
The QWebElement::prependInside method add the parameter to the beginning of the web element.
If we have a QWebElement *elt containing a empty table such as :
<table><table>
to create the following table,
<table>
<tr><td>A</td></tr>
<tr><td>B</td></tr>
<tr><td>C</td></tr>
</table>
You can use one of the two following methods, they are equivalent.
Method 1, with appendInside
elt->appendInside("<tr><td>A</td></tr>");
elt->appendInside("<tr><td>B</td></tr>");
elt->appendInside("<tr><td>C</td></tr>");
or method 2, with preprendInside
elt->prependInside("<tr><td>C</td></tr>");
elt->prependInside("<tr><td>B</td></tr>");
elt->prependInside("<tr><td>A</td></tr>");
Using prependInside or appendInside gives you the control over the FIFO or LIFO behaviour of your algorithm.

Related

Second Scraper - If Statement

I am working on my second Python scraper and keep running into the same problem. I would like to scrape the website shown in the code below. I would like to be ability to input parcel numbers and see if their Property Use Code matches. However, I am not sure if my scraper if finding the correct row in the table. Also, not sure how to use the if statement if the use code is not the 3730.
Any help would be appreciated.
from bs4 import BeautifulSoup
import requests
parcel = input("Parcel Number: ")
web = "https://mcassessor.maricopa.gov/mcs.php?q="
web_page = web+parcel
web_header={'User-Agent':'Mozilla/5.0(Macintosh;IntelMacOSX10_13_2)AppleWebKit/537.36(KHTML,likeGecko)Chrome/63.0.3239.132Safari/537.36'}
response=requests.get(web_page,headers=web_header,timeout=100)
soup=BeautifulSoup(response.content,'html.parser')
table=soup.find("td", class_="Property Use Code" )
first_row=table.find_all("td")[1]
if first_row is '3730':
print (parcel)
else:
print ('N/A')
There's no td with class "Property Use Code" in the html you're looking at - that is the text of a td. If you want to find that row, you can use
td = soup.find('td', text="Property Use Code")
and then, to get the next td in that row, you can use:
otherTd = td.find_next_sibling()
or, of you want them all:
otherTds = td.find_next_siblings()
It's not clear to me what you want to do with the values of these tds, but you'll want to use the text attribute to access them: your first_row is '3730' will always be False, because first_row is a bs4.element.Tag object here and '3730' is a str. You can, however, get useful information from otherTd.text == '3730'.

Vertical html table without repeating th tags

I'm generating a table using xslt, but for this question I'll keep that side out of it, as it relates more to the actual generated structure of a html table.
What I do is make a vertical table as follows, which suits the layout needed for the data concerned that originated in a spreadsheet. Example is contrived for brevity, actual data fields contain lengthy strings and many more fields.
Title: something or rather bla bla
Description: very long desription
Field1: asdfasdfasdfsdfsd
Field2: asdfasfasdfasdfsdfjasdlfksdjaflk
Title: another title
Description: another description
Field1:
Field2: my previous field was blank but this one is not, anyways
etc.
The only way so far I found to generate such a html table is using repeating tags for every field and every record e.g.:
<tr><th>Title</th><td>something or rather bla bla</td></tr>
<tr><th>Description</th><td>very long desription</td></tr>
...
<tr><th>Title</th><td>another title</td></tr>
<tr><th>Description</th><td>another description</td></tr>
...
Of course this is semantically incorrect but produces correct visual layout. I need it to be semantically correct html, as that's the only sane way of later attaching a filtering javascript facility.
The following correct semantically produces an extremely wide table with a single set of field headers on the left:
<tr><th>Title</th><td>something or rather bla bla</td><td>another title</td></tr>
<tr><th>Description</th><td>very long desription</td><td>another description</td></tr>
...
So to summarise, need a html table (or other html structure) where it's one record under another (visually) with repeating field headers, but the field headers must not be repeated in actual code because that would wreck any record based filtering to be added later on.
Yo. Thanks for updating your question, and including some code. Typically you'd also post what you've tried to correct this issue - but I'm satisfied enough with this post.
Since you want the repeating headers in vertical layout (not something I've seen often, but I can understand the desire), you don't have to modify the HTML formatting, just use a bit more JavaScript to figure it out. I haven't gone through and checked to see if I'm doing things efficiently (I'm probably not, since there are so many loops), but in my testing the following can attach to a vertical table and filter using a couple variables to indicate how many rows there are in each entry.
Firstly, here's the HTML I'm testing this one with. Notice I have a div with the id of filters, and each of my filter inputs has a custom attribute named filter that matches the header of the rows they are supposed to filter:
<div id='filters'>
Title: <input filter='Title'><br>
Desc: <input filter='Description'>
</div>
<table>
<tr><th>Title</th><td>abcd</td></tr>
<tr><th>Description</th><td>efgh</td></tr>
<tr><th>Title</th><td>ijkl</td></tr>
<tr><th>Description</th><td>mnop</td></tr>
<tr><th>Title</th><td>ijkl</td></tr>
<tr><th>Description</th><td>mdep</td></tr>
<tr><th>Title</th><td>ijkl</td></tr>
<tr><th>Description</th><td>mnop</td></tr>
<tr><th>Title</th><td>ijkl</td></tr>
<tr><th>Description</th><td>mnop</td></tr>
</table>
Here are the variables I use at the start:
var filterTable = $('table');
var rowsPerEntry = 2;
var totalEntries = filterTable.find('tbody tr').size() / rowsPerEntry;
var currentEntryNumber = 1;
var currentRowInEntry = 0;
And this little loop will add a class for each entry (based on the rowsPerEntry as seen above) to group the rows together (this way all rows for an entry can be selected together with a class selector in jQuery):
filterTable.find('tbody tr').each(function(){
$(this).addClass('entry' + currentEntryNumber);
currentRowInEntry += 1;
if(currentRowInEntry == rowsPerEntry){
currentRowInEntry = 0;
currentEntryNumber += 1;
}
});
And the magic; on keyup for the filters run a loop through the total number of entries, then a nested loop through the filters to determine if that entry does not match either filter's input. If either field for the entry does not match the corresponding filter value, then we add the entry number to our hide array and move along. Once we've determined which entries should be hidden, we can show all of the entries, and hide the specific ones that should be hidden:
$('#filters input').keyup(function(){
var hide = [];
for(var i = 0; i < totalEntries; i++){
var entryNumber = i + 1;
if($.inArray(entryNumber, hide) == -1){
$('#filters input').each(function(){
var val = $(this).val().toLowerCase();
var fHeader = $(this).attr('filter');
var fRow = $('.entry' + entryNumber + ' th:contains(' + fHeader + ')').closest('tr');
if(fRow.find('td').text().toLowerCase().indexOf(val) == -1){
hide.push(entryNumber);
return false;
}
});
}
}
filterTable.find('tbody tr').show();
$.each(hide, function(k, v){
filterTable.find('.entry' + v).hide();
});
});
It's no masterpiece, but I hope it'll get you started down the right path.
Here's a fiddle too: https://jsfiddle.net/bzjyfejc/

HTML Table --> Selecting 1 Column to Filter in a Multi-Column Table

I want to perform an HTML table search on one column of a table. The table in this example here shows 2 columns. I have added classes to the tags to select column "Title 1" to filter only - however, the code is still looking at the "Title 2" column for the filter.
var $rows = $('#table tbody tr td[class = "col1"]');
$('#search').keyup(function() {
var val = '^(?=.*\\b' + $.trim($(this).val()).split(/\s+/).join('\\b)(?=.*\\b') + ').*$',
reg = RegExp(val, 'i'),
text;
$rows.show().filter(function() {
text = $(this).text().replace(/\s+/g, ' ');
return !reg.test(text);
}).hide();
});
Could anyone provide some advice to the mistake I am making?
The main problem of your code is that when you do hide(), you're hiding out the <td> instead of the <tr> so that you end up searching col2 when the <td> of col1 is hidden. So, in that regard you need to use the parent <tr> and hide it.
}).parent("tr").hide();
Another problem i saw is that your $row is good initially but once you
hide at least a row you end up with less rows to work with. So, in that regard you
need to preserve the original number of rows and use it to hide those that need to be hidden and show those need to be shown.
See my updated JSFiddle Demo
As per the OPs comments.
I think you're running into trouble because your selector is too detailed. try:
var $rows = $(".col1", "table");

IE table first column whitespace

Im making a page (for IE) where is shows some values in a table format. But somehow there's some kind of whitespace before the first column making it uneven with the following columns.
i just want the columns to be aligned
http://i49.tinypic.com/14b3kuh.jpg
out.println("<div id='tablePos'>");
out.println("<ul class='ulStyle'>");
out.println("<li>");
out.println("<table border='1'>");
out.println("<div id='divTableForms'>");
out.println("<tr id='process'><td>PROCESS</td></tr>");
while(rs.next()){
String process = rs.getString(2);
String processtype = rs.getString(1);
out.println("<tr id='process'><td id='process2'>"+processtype + "-" +process+"</td></tr>");
}
out.println("<tr id='process'><td id='process2'></td></tr>");
out.println("</div>");
out.println("</table>");
out.println("</li>");
First of all,I think its because of ur divTableForms div... And second, u have used out.println("< tr id='process'>< td id='process2'>< /td>< /tr>");
But i dont get for wat u have included it.... Abd finally never use the same id for multiple tag... Particularly inside loop never use it..
empty Table-Data (TD element) always was problem. view this http://bignosebird.com/docs/h34.shtml OR CSS to make an empty cell's border appear?.
out.println("<tr id='process'><td id='process2'> </td></tr>");
also id attribute value must be unique in html document . your code duplicate 'process' identifier in html document use class instead of Id Attribute

Remove entire row from table with HTML::TableExtract

Is there a way to remove an entire row (html tags 'n all) from an HTML Table with HTML::TableExtract?
Mucking around with the sample code from CPAN, this is what I've tried so far:
use HTML::TableExtract qw(tree);
my $te = HTML::TableExtract->new( headers => [qw(name type members)] );
# get $html_string out of a file...
$te->parse($html_string);
my $table = $te->first_table_found();
my $table_tree = $table->tree;
$table_tree->row(4)->replace_content('');
my $document_tree = $te->tree;
my $document_html = $document_tree->as_HTML;
# write $document_html to a file ...
Now, as the name suggests, 'replace_content()' in the line $table_tree->row(4)->replace_content(''); removes the content of row 4, but the row itself remains in markup. I need to get the tags and everything in-between removed as well.
Any ideas?
What you want is the parent and delete methods
See the docs for HTML::Element and for HTML::Element::delete
UPDATE
Ok, click that checkmark and mark this one as answered....Here it is:
my($p) = $table_tree->row(4)->parent();
$p->delete;
Also, NOTE, you need the () parens around $p! If you don't have parens don't get back a reference.
For me, with the above Perl code working on this HTML,
<table>
<tr><td>name</td><td>type</td><td>members</td></tr>
<tr><td>row1</td><td>row1</td> <td>row1</td></tr>
<tr><td>row2</td><td>row2</td> <td>row2</td></tr>
<tr><td>row3</td><td>row3</td> <td>row3</td></tr>
<tr><td>row4</td><td>row4</td> <td>row4</td></tr>
</table>
I get this as a result of printing $document_html
<table>
<tr><td>name</td><td>type</td><td>members</td></tr>
<tr><td>row1</td><td>row1</td><td>row1</td></tr>
<tr><td>row2</td><td>row2</td><td>row2</td></tr>
<tr><td>row3</td><td>row3</td><td>row3</td></tr>
</table>
Notice that there is no empty <tr></tr>