I'm generating table rows using v-for"x in y". I'd also like to set some classes conditional based on one of the values in the loop.
example:
<tr v-for="file in fileList" class="bg-green if file.include">
<td><% file.filename %></td>
<td><% file.extension %></td>
<td><% file.mime %></td>
</tr>
If file.include is true I'd like the class bg-green applied, but its throwing an error.
note: Using custom delimiters as I'm using Flask.
It'd just be:
<tr v-for="file in fileList" :class="{'bg-green': file.include}">
:class is the shorthand form of v-bind:class. A binding is necessary to make the value an expression.
There are several ways to write the expression but in this case the simplest is to use the object form. The keys of the properties are the class names and the values are truthy/falsey values that determine whether or not to include that class name.
Alternatives include things like:
<tr v-for="file in fileList" :class="file.include ? 'bg-green' : ''">
See https://v2.vuejs.org/v2/guide/class-and-style.html for more information.
I have a users table that list out the users' ID, name, email and username. What I am attempting to do is verify that a specific entry is in that table.
So essentially what I would like to do is find the row that has ID = 22, then verify that name = John Smith, email = john#smith.com and username = jsmith. The able is setup as show below. What I don't understand is a couple of things...
How do I get a row with specific text, I.E. that as user.id = 22.
Once I have that row, how do I use it get each of the elements.
I thought I could do a sort of for loop but can't figure out how to set the has_selector? condition. (below is pseudocode)
page.all('tr').each do |tr|
next unless tr.has_selector?('td.id = 22') #psuedocode
expect(tr.get('td.name').to eq("John Smith")
#other expects here...
end
Table code
<table class="table table-striped table-condensed">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
<th>Username</th>
</tr>
</thead>
<tbody>
<% #users.each do |user| %>
<tr>
<td class="id"><%= user.id %></td>
<td class="name"><%= link_to user.name, user %></td>
<td class="email"><%= user.base_name %></td>
<td class="username"><%= user.username %></td>
</tr>
<% end %>
</tbody>
</table>
A nice clean approach would be to add a "data-user-id" attribute to each tr element and then find the row you want with tr = find('tr[data-user-id="22"]'), but if that's not an option there are a number of ways to do this. Either of
td = page.find(:css, 'td.id', text: /^22$/) # find the id td with text of exactly 22
tr = td.find(:xpath, './parent::tr') # get the parent tr of the td
expect(tr).to have_css('td.name', text: 'John Smith')
or finding the row using just an xpath like
tr = page.find(:xpath, ".//tr[./td[#class='id'][text()='22']]")
expect(tr).to have_css('td.name', text: 'John Smith')
should do what you want. If you want to stick with the looping approach (not recommended because it will be slow - and the way your loop is structured it could just not verify anything) it would be
page.all('tr').each do |tr|
next unless tr.has_css?('td.id', text: /^22$/)
expect(tr).to have_css('td.name', text: "John Smith")
#other expects here...
end
Why not just do like this?:
feature "index users" do
let!(:user) { create(:user, name: "John Smith") }
it "shows users" do
visit users_path
within ".name_#{user.id}" do
expect(page).to have_content("John Smith")
end
end
end
This is not so much a technical answer as it is an alternative approach to avoid the need to write what could be brittle, and overly-complicated tests.
Often times, I am just verifying that some object appeared on the page by looking for some clever text. Maybe it is in a table, or maybe it is in a responsive grid... If I make my test care about the UI implementation, it could break when something changes in the UI.
My "trick" is to use some really unusual text for the test. While "John Smith" should work, you can use "John Zebra" and you will definitely not accidentally have that text appearing on the page.
But when I do need to care about data appearing in a table, I use dom_id for each row. In this example of a list of orgs for the super admin, the reseller org name appears in two rows -- the first being the reseller org's row, and he seond being in the reseller column for a referral org:
The table view code snippet...
%table.orgs-list.stripe
%thead
%tr
%th Created
%th Name
%th Reseller
...
%tbody
- organizations.each do |org|
%tr{id: dom_id(org)}
%td= org.created_at....
%td= link_to org.name, org
%td= org.reseller.try(:name)
...
And the cucumber test code snippet:
Then(/^I should see "([^"]*)" showing "([^"]*)" as the reseller$/) do |org_name, reseller_name|
org = Organization.where( name: org_name).first
reseller = Organization.where( name: reseller_name).first
dom_id = "organization_#{org._id}"
within("tr##{dom_id}") do
expect(page).to have_text(reseller.name)
end
end
def row_containing_cell_with_text(text, exact = false)
find('td,th', text: text, exact: exact).ancestor('tr')
end
I currently have a MySQL database with a table 'description' containing a 'title' and 'contents' variables. What I'd like to do, is dynamically create buttons who's value attributes are the 'title' of each 'description' row. Then I'd like to have the buttons display the 'contents' value when their respective 'title' is clicked.
The problem is I'm not sure how to go about inserting a JSP String variable into a button 'value' attribute dynamically. Is there any way of doing this without javascript?
This is the code I have:
Getting the description objects:
<%
List<Description> descriptions = DescriptionDB.getDescriptions();
%>
Scriptlet for the table of buttons:
<table border="1" id="titleTable">
<%
if (descriptions != null) {
for (Description description : descriptions) {
String title = description.getDescriptionTitle();
%>
<tr>
<td><a id="bt" type="button" value="title"</td>
</tr>
<%
}
}
%>
</table>
I would like the value="title" to be the String title in the scriptlet.
It should be as easy as
%>
<tr>
<td><a id="bt" type="button" value="<%= title %>"</td>
</tr>
<%
Be aware though that the use of scriptlets seems to be considered sort of bad practice by some today.
I am developing a web application using JSP and Servlets.
In that application I have to show data from database table stud(studID, name, add) in html table, And each row in the table will have a hyperlink associated with it at the last column. After clicking on that hyperlink I wants to get the (studID) from the table...
so far I have done getting the data from database and then putting it into the column and then adding hyperlink for each row.. But I am not able to get the (studID) from the html table associated with hyperlink..
Thanks in advance....
Source code :
<%
String[][] data = (String[][])request.getAttribute("data");
String[] cNames = (String[])request.getAttribute("columnNames");
//headings
%>
<table>
<tr>
<%
for(int i=0;i<cNames.length;i++) {
%>
<th>
<%= cNames[i] %>
</th>
<%
}
//data if(data!=null)
for(int i=0;i<data.length;i++) {
%>
<tr>
<%
for(int a=0;a<3;a++) {
%>
<td>
<%=
data[i][a]
%>
</td>
<%
//hyperlink
if(a==2) {
%>
<td>
<a href="PlanProtocol" id=<%=i%> onclick="<% session.setAttribute("ID","p2"); %>" >Edit</a></td>
<%
}
}
%>
</tr>
<% } %>
<tr>
</table>
You can pass the id as a query string in the url. Simply:
My Link
Will work. But if you are using JSTL or another tag library then you can do something like this:
<c:url value="/myservlet" var="myURL">
<c:param name="id" value="1234"/>
</c:url>
mylink
And this has its advantages such as url encoding etc.
So to add the id to the URL in your posted code you can:
<a href="PlanProtocol?id=<%=i%>" >Edit</a>
And the url will end up like this: PlanProtocol?id=1234.
In the Servlet you can get the Parameter by:
request.getParameter("i");
However, as I mentioned above, you probably want to use a tag library like the JSTL rather than placing these scriptlets in your page. There are several advantages.
think you should pull out the studID in JSP and format the studID into the query string of the URL, html page. (?studID=xxxxx) So the servlet will know the studID.
You can use request.setAttribute("studID","value"); in your jsp page to set the value and use request.getAttribute("studID"); in servlet to get value
I'm using Rails to display a set of data. The problem is that data is so large I dont really do the usually for each loop since I creates this insanely long list.
My solution would be to create some form of table where after 10 records create a new cell and after 5 cells create a new row. I'm not really that comfortable with for loops in rails so I figured throw the question out.
Right now I have...
<strong> Person Data Set: </strong><br />
<% for person in #persons %>
<%= interest.name %> <br />
<% end %>
So I can I create a loop similar to this?
<strong> Person Data Set: </strong><br />
<table>
<tr>
*****for each 5 cells???? *****
<td>
*****For each 10 records?? ***
</td>
</tr>
</table>
Has anyone had to deal with an issue like this before?
There is an each_slice method. With HAML (I really don't like ERB but the idea is the same):
%strong
Person Data Set:
%br
%table
- #persons.each_slice(10) do |ten_people|
%tr
- ten_people.each_slice(5) do |five_people|
%td
- five_people.each do |person|
%p= person.name