0

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

Terence Bruwer
  • 229
  • 3
  • 16

2 Answers2

1

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>");
Community
  • 1
  • 1
Soullivaneuh
  • 3,553
  • 3
  • 37
  • 71
  • i need to find the lowest value of one of the td (in this case the date) and assign that to all the rows – Terence Bruwer Aug 20 '14 at 19:25
  • So you will have to code a little bit more... :p Just do a `.each` with a variable and comparison to keep the lowest value. Then, assign to all tr. – Soullivaneuh Aug 20 '14 at 19:26
  • unfortunately the data-start value is dynamic so it will change every time. which is why i am having a problem wrapping my mind around the code needed. i first have to find any matching rows if their are then I have to find the lowest value and then go back and assign it to the rows – Terence Bruwer Aug 20 '14 at 19:32
0

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

kirinthos
  • 452
  • 2
  • 9
  • looks good, only issue is start will probably be a string in more cases than not – Terence Bruwer Aug 20 '14 at 19:36
  • as in it will not be a number at all? if this is the case you will just have to amp up the comparison to look for number vs. string, otherwise you should be able to parseInt/parseFloat, etc. – kirinthos Aug 20 '14 at 19:43