Terence Bruwer
Terence Bruwer

Reputation: 239

how to assign the same value to matching rows in a table dynamically

I have a table with multiple rows where one or more rows with have the same source data attribute. I need to loop through all these rows and assign the lowest common denominator belonging to the matching group to each row that matches that specific data source so:

<table>
 <tr data-start="1"><td>one</td><td>2014-04-01</td></tr>
 <tr data-start="2"><td>two</td><td>2014-11-23</td></tr>
 <tr data-start="1"><td>three</td><td>2014-03-13</td></tr>
 <tr data-start="3"><td>four</td><td>2014-06-02</td></tr>
 <tr data-start="1"><td>five</td><td>2014-03-08</td></tr>
</table>

so in the above table the matches are row 1,3 and 5. and i need to assign the lowest date to all of the matches

Thanks in advance for any help

Upvotes: 0

Views: 77

Answers (2)

kirinthos
kirinthos

Reputation: 452

is this what you're trying to do?

$(document).ready(function () {
    var table = $('#test');
    var output = $('#output');
    var minNumber = table.find('tr').first().data('start');
    table.find('tr').each(function () {
        minNumber = Math.min($(this).data('start'), minNumber);
    });

    //get current date
    var dt = new Date();
    dt = dt.getFullYear() + "-" + dt.getMonth() + 1 + "-" + dt.getDate();

    table.find('tr[data-start=' + minNumber + ']').each(function () {
        $(this).children().last().html(dt);
    });

    output.append(minNumber);
});

edit: oh I didn't read "lowest date to all the matches" properly, but changing this code to do that should be rather trivial one more edit: forgot to find the lowest number, added that

Upvotes: 0

Soullivaneuh
Soullivaneuh

Reputation: 3863

If I understand, you want to set the same value to all data-start="1" for example ?

Why not using this solution with jQuery ?

$('tr[data-start="1"]').html("<b>Your content</b>");

Upvotes: 1

Related Questions