2017-05-18 27 views
0

内の特定のTDの最高値を取得:は私が怒鳴るようなテーブル構造を持つ動的テーブル

function highestPosInBlock(posId) { 
 
    // determine the highest number in the current block 
 
    console.log($("body").data("block")); // ... 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<table data-block="1"> 
 
    <tr> 
 
    <td data-position="1">1</td> 
 
    </tr> 
 
    <tr> 
 
    <td data-position="2">2</td> 
 
    </tr> 
 
    <tr> 
 
    <td data-position="3">3</td> 
 
    </tr> 
 
</table> 
 

 
<table data-block="2"> 
 
    <tr> 
 
    <td data-position="1">1</td> 
 
    </tr> 
 
    <tr> 
 
    <td data-position="2">2</td> 
 
    </tr> 
 
</table>

私は与えられたブロックの最高data-position属性を取得します。これまでのJS関数は次のようになります。

function highestPosInBlock(posId) { 
    // determine the highest number in the current block 
    console.log($("body").data("block") // ... 
} 

これは私が立ち往生した場所です。私が渡す引数は与えられたデータブロックです。それは2

任意のアイデアどのようにそれを行うに戻りshoudl私はここで3

私は呼びたいhighestPosInBlock(2)を取得したい

highestPosInBlock(1):たとえば、私はこのような関数を呼び出すと思いますか?私は完全なブラックアウトをしています。

もう一つ:このたわごとはIE 7で動作するように持っているので、私は、jQueryの各-イテレータを使用することはできません、(それぞれをサポートしていません)

+0

最初にあなたが望むテーブルを取得する:$( 'table [data-block = "1"]') 'それから[find](https://api.jquery.com/find/) ](https://api.jquery.com/each/)それらの、[データの位置を取得する](http://stackoverflow.com/questions/5309926/how-to-get-the-data-id-属性)および[最大値を追跡する(http://stackoverflow.com/questions/28767330/javascript-biggest-number-from-array-using-loop) – musefan

+0

@DasSaffe)が(使用各々を防止するために、ループのために添加しましたあなたがあなたの質問で言ったように –

答えて

1

ベロー例により、我々が得ますブロック1の最大位置、

これは(データブロック属性によって)テーブルを取得してループし、td(.find("\[data-position\]")を使用)をスローしてそれらをスローし、それらの最大値を取得することによって行われました。あなたが怒鳴るように、ループのためのfind()使用を使用しないようにしたい場合は

function highestPosInBlock(posId) { 
 
    // determine the highest number in the current block 
 
    console.log($("[data-block="+posId+"]")) 
 
    var max = null; 
 
    $("[data-block="+posId+"]").find("[data-position]").each(function(){ 
 
     var value = parseInt($(this).attr('data-position')); 
 
     max = (value > max) ? value : max; 
 
    }); 
 
    
 
    console.log(max); 
 
    alert("Max position value of the table with data-block = "+posId+" is -> "+max) 
 
} 
 

 

 

 
highestPosInBlock(1)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<table data-block="1"> 
 
    <tr> 
 
     <td data-position="1">1</td> 
 
    </tr> 
 
    <tr> 
 
     <td data-position="2">2</td> 
 
    </tr> 
 
    <tr> 
 
     <td data-position="3">3</td> 
 
    </tr> 
 
</table> 
 

 
<table data-block="2"> 
 
    <tr> 
 
     <td data-position="1">1</td> 
 
    </tr> 
 
    <tr> 
 
     <td data-position="2">2</td> 
 
    </tr> 
 
</table>

:ここ

は、作業の抜粋である

function highestPosInBlock(posId) { 
 
    // determine the highest number in the current block 
 
    var max = null; 
 
    var tds = $("[data-block="+posId+"]").find("[data-position]"); 
 
    
 
    if(tds.length>0){ 
 
     for(var i=0;i<tds.length;i++) { 
 
     var value = parseInt($(tds[i]).attr('data-position')); 
 
     max = (value > max) ? value : max; 
 
     } 
 
    } 
 
    
 
    console.log(max); 
 
    alert("Max position value of the table with data-block = "+posId+" is -> "+max) 
 
} 
 

 

 

 
highestPosInBlock(1)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<table data-block="1"> 
 
    <tr> 
 
    <td data-position="1">1</td> 
 
    </tr> 
 
    <tr> 
 
    <td data-position="2">2</td> 
 
    </tr> 
 
    <tr> 
 
    <td data-position="3">3</td> 
 
    </tr> 
 
</table> 
 

 
<table data-block="2"> 
 
    <tr> 
 
    <td data-position="1">1</td> 
 
    </tr> 
 
    <tr> 
 
    <td data-position="2">2</td> 
 
    </tr> 
 
</table>

+0

それはちょうど私は、または私がコードショーを実行すると質問としての正確な出力ですか? – Edwin

+0

@Edwinの結果がコンソールにあります。また、警告として追加されました –

+1

編集後に見た:D gj – Edwin

関連する問題