2016-10-31 107 views
1

我有問題拉動孩子divIDrow.map函數下面的每一行。使用量角器選擇子div ID

換句話說,對於每一行元素我重複,我想拉rowId ATTRIB:

<div id="rowId_21"> 

下面是一個HTML片段:

<div ng-repeat="row in vm.sourceData.data" ng-init="thatRow=$index"> 
 
<div id="rowId_21" ng-class-odd="'row-2'" ng-class-even="'row-3'" > 
 
    <div ng-repeat="column in vm.sourceData.columns" >  
 
    <div ng-if="row[column.field].length !== 0" class="ng-scope highlight21"> \t 
 
    \t <span ng-bind-html="row[column.field] | changeNegToPrenFormat" vm.highlightedrow="" class="ng-binding"> 
 
       Sales 
 
     </span> 
 
    </div>  
 
    </div> 
 
</div> 
 
</div> 
 
<div ng-repeat="row in vm.sourceData.data" ng-init="thatRow=$index"> 
 
    <div id="rowId_22" ng-class-odd="'row-2'" ng-class-even="'row-3'" ng-class="vm.hideRow(row)" class="row-3 height-auto"> 
 
    <!-- ... --> 
 
</div> 
 
</div>

我開始拉動rows對象,然後我重複它們:

// pulls the data rows 
 
var selDataRows = '.grid-wrapper.fluid-wrapper'; 
 
var rows = element(by.css(selDataRows)).all(by.repeater('row in vm.sourceData.data')); 
 

 
rows.map(function (row, idx) {    
 
    //var thisRowId = row.element(by.css('div[id^="rowId_"]')); // *** RETURNS NULL 
 
    
 
    var thisRowId = row.all(by.xpath("descendant::div[starts-with(@id, 'rowId')]")); 
 
    
 
    thisRowId.getText().then(function(txt){    
 
     // RETURNS A VERY LONG ID: [ '7\n4\n41\n113\n3.3\n(34)\n(1.1)\n7...] 
 
     console.log('--- Curr Div ID: ', txt); 
 
    }); 
 
    
 
}). 
 
then(function(){    
 
console.log('*** Rows.Map Done.');    
 
});

我認爲這將拉動第一子ID(即https://developer.mozilla.org/en-US/docs/Web/CSS/Attribute_selectors):

by.css('div[id^="rowId_"]')

或者可能是這樣的:

by.xpath("descendant::div[starts-with(@id, 'rowId')]")

但是似乎都不奏效。

諮詢讚賞...

鮑勃

回答

1

首先,你需要從映射功能回報。而且,使用.getAttribute()方法獲取id屬性:

var selDataRows = '.grid-wrapper.fluid-wrapper'; 
var rows = element(by.css(selDataRows)).all(by.repeater('row in vm.sourceData.data')); 

rows.map(function (row) {    
    return row.element(by.xpath("descendant::div[starts-with(@id, 'rowId')]")).getAttribute("id").then(function(rowId) {    
     return rowId; 
    }); 
}).then(function(ids) {    
    console.log(ids);    
}); 
+0

這位朋友你好,和thnk你的迴應。原來我需要映射函數中的rowId;但是,看起來像我通過使用'thisRowId.getText()'而不是'getAttribute(「id」)''發生了錯誤。我會在早上重新測試第一件事。謝謝 ! –

+1

@ bob.mazzo明白了,是的,這應該有所幫助。順便說一下,如果以後不需要id數組,那麼看看'each()'是否比map()'更適合工作。謝謝,讓我們在城市裏喝一兩杯啤酒吧! (如果有興趣,請通過linkedin ping我!) – alecxe

+0

我正在考慮'.each()',但我完全忘記發生了什麼。我會重新審視這個想法。 +1 –