Select ID w/jQuery - jquery-selectors

I have a table with rows generated via a loop. Each TR has a unique ID.
How do I select that ID when I click span with .clickMe class?
<tr id="244">
<td>...</td>
<td><span class="clickMe"</td>
</tr>
<tr id="4554">
<td>...</td>
<td><span class="clickMe"</td>
</tr>

IF the structure is always going to be the same you could just do:
var theId = this.parentNode.parentNode.id
If its not always going to be the same then you could do:
var theId = $(this).closest('tr').attr('id');
So putting it all together:
$('.clickMe').click(function(){
var theId = $(this).closest('tr').attr('id'); // or the standard DOM approach
// other stuff
});
Also your Id's should not begin with numbers as per the spec.

Related

How to get the header from table in protractor?

I have table in my angular application. How to iterate through the table headers and compare the headings in it?
Following is sample html code.
<table>
<tbody>
<th>
<td>head1</td>
<td>head2</td>
<td>head2</td>
</th>
<tr>
<td>row1Col1</td>
<td>row1Col2</td>
<td>row1Col3</td>
</tr>
<tr>
<td>row2Col1</td>
<td>row3Col2</td>
<td>row4Col3</td>
</tr>
<tr>
<td>row3Col1</td>
<td>row3Col2</td>
<td>row3Col3</td>
</tr>
</tbody>
</table>
How to loop through the number of columns(headers).
Method 1
var expectHeaders = ['head1', 'head2', 'head3'];
var headers = element.all(by.css('table > tbody > th > td'));
expect(headers.getText()).toEqual(expectHeaders);
//or do assertion in then()
headers.getText().then(function(actualHeaders){
expect(actualHeaders).toEqual(expectHeaders);
// or compare header one by one in loop
expectHeaders.forEach(function(expectHeader, index){
expect(actualHeaders[index]).toEqual(expectHeader);
})
})
Method 2
if you prefer to iterate through each header
var expectedHeaders = ['head1', 'head2', 'head3'];
var headers = element.all(by.css('table > tbody > th > td'));
expectedHeaders.forEach(function(header, index){
expect(headers.get(index).getText()).toEqual(header);
// or compare in then()
headers.get(index).getText().then(function(actual){
expect(actual).toEqual(header)
})
});

how to show a single value of column razor view

here is my markup
<td class="subtotal">#Model.Where(s=>s.wcc.xyz=="abc").Select(s=>s.targetxyz.wcc)</td>
<td class="subtotal">#Model.Where(s=>s.wcc.xyz=="abc").Sum(s=>s.wcc.col1)</td>
<td class="subtotal">#Model.Where(s=>s.wcc.xyz=="abc").Sum(s=>s.wcc.col2)</td>
<td class="subtotal">#Model.Where(s=>s.wcc.xyz=="abc").Sum(s=>s.wcc.col3)</td>
<td class="subtotal">#Model.Where(s=>s.wcc.xyz=="abc").Sum(s=>s.wcc.col4)</td>
<td class="subtotal">#Model.Where(s=>s.wcc.xyz=="abc").Sum(s=>s.wcc.col5)</td>
<td class="subtotal">#Model.Where(s=>s.wcc.xyz=="abc").Sum(s=>s.wcc.col6)</td>
<td class="subtotal">#Model.Where(s=>s.wcc.xyz=="abc").Sum(s=>s.wcc.col7)</td>
<td class="subtotal">#Model.Where(s=>s.wcc.xyz=="abc").Sum(s=>s.wcc.col8)</td>
</tr>
my query
var data =
from b in re.wccs
join t in re.targetxyz on b.xyz equals t.dname
select new val { wcc = b, targetxyz = t };
return View(data);
my error
System.Linq.Enumerable+WhereSelectEnumerableIterator`2[db.Models.val,System.Int32]
i can load all the columns with sum but at the place of select command i get this error.
why any suggestion?
You will need to use First or Single to get that value, and possible some ordering on data (as not sure what you want to show here)
e.g.
#Model.Where(s=>s.wcc.xyz=="abc").Select(s=>s.targetxyz.wcc).FirstOrDefault()
#Model.Where(s=>s.wcc.xyz=="abc").Select(s=>s.targetxyz.wcc).SingleOrDefault()
read here for difference of single & first:
LINQ Single vs First

How Do I Set An HTML Attribute For a Scala List Item Using Lift CSS Selectors?

I have some Lift code that creates table rows from a List:
".row *" #> myList.map(x => {
val rowId = x.id
".cell1" #> x.data &
".cell2" #> x.moreData
})
Based on a template like this:
<table>
<tr class="row">
<td class="cell1"></td>
<td class="cell2"></td>
<tr>
<table>
I want output like this:
<table>
<tr class="row" id="123">
<td class="cell1">stuff</td>
<td class="cell2">junk</td>
<tr>
<tr class="row" id="456">
<td class="cell1">more stuff</td>
<td class="cell2">more junk</td>
<tr>
<table>
How do I set that id attribute to be rowId for each tr based on my List elements?
This should work for you:
".row" #> myList.map(x => {
val rowId = x.id
"tr [id]" #> rowId &
".cell1 *" #> x.data &
".cell2 *" #> x.moreData
})
To set an attribute, you usually just need to specify the name of the attribute inside of []. So, in addition to ID, if you wanted to add a class, it would be [class]. There is also a special modifier, + which will append to the current value. So [class+] will add whatever you specify to the current values.
It is also worth noting that some drafts of the HTML spec require at least one letter in the ID, see this question for an explanation of why.

Sort a table with multiple tbody?

I have a table structure as follows. Now I need to sort these nested tables separately. Forexample: sorting chapter's row will only update chapters order in a separate table. Whereas, sorting items will update their order in another table.
I managed to setup the code and sorting. However, when I drag the items from chapter 4, it pass on the order of the items in from chapter 1 since they come before chapter 4???
Could someone help me with sorting only relevant items??
NOTE: This list is dynamic coming from database. So I am interested in one jquery code covering all the ordering bits.
<table id=subsortsortable>
<tbody class=content>
<tr id="chapter_1"><td>Chapter one</td></tr>
<tr id="chapter_2"><td>Chapter two</td></tr>
<tr id="chapter_3">
<td>
<table>
<tbody class=subcontent>
<tr id="item_31"><td>three.one</td></tr>
<tr id="item_32"><td>three.two</td></tr>
</tbody>
</table>
</td>
</tr>
<tr id="chapter_4">
<td>
<table>
<tbody class=subcontent>
<tr id="item_41"><td>four.one</td></tr>
<tr id="item_42"><td>four.two</td></tr>
<tr id="item_43"><td>four.three</td></tr>
<tr id="item_44"><td>four.four</td></tr>
<tr id="item_45"><td>four.five</td></tr>
</tbody>
</table>
</td>
</tr>
<tr id="chapter_4"><td>Chapter Four</td></tr>
</tbody>
</table>
The code I am using is as follows:
//for sorting chapters - which is outer table
$("#subsortable tbody.content").sortable({
opacity: 0.7,
cursor: 'move',
placeholder: "ui-state-highlight",
forcePlaceholderSize: true,
update: function(){
var order = $('#subsortable tbody.content').sortable('serialize') + '&action=updateChaptersOrder';
$.post("/admin/ajax/ajax_calls.php", order, function(theResponse){
});
}
});
// For sorting and updating items within a specific chapter - which is nested tbody
$("tbody.sortItems").subcontent({
opacity: 0.7,
cursor: 'move',
placeholder: "ui-state-highlight",
forcePlaceholderSize: true,
update: function(){
var order = $('tbody.subcontent').sortable('serialize');// + '&action=updateListings';
$.post("/admin/ajax/ajax_calls.php", order, function(theResponse){
});
}
});
I have got the answer to my own question.. In case someone else encounter the same problem. I have changed the following code inside the internal table:
var order = $('tbody.subcontent').sortable('serialize');
to
var order = $(this).sortable('serialize');

ExtJS: How to append child element to Ext.select() result?

I have this table structure:
<table class="x-toolbar-ct">
<tbody>
<tr>
<td>col1</td>
<td>col2</td>
</tr>
</tbody>
</table>
No luck with this code:
var row = Ext.select('.x-toolbar-ct').select('tbody').select('tr');
row.appendChild('<td>col3</td>');
So how to append new <td> element? Thank you.
We have to use createChild(), and use first() to return the first element.
var row = Ext.select(".x-toolbar-ct").first().first().first();
row.createChild("<td>col3</td>");