Icet
Icet

Reputation: 688

Get value of each cell from dynamic table

I have dynamic table which is create like this

<table id="sort" class="table">
                <thead>
                    <tr>
                        <th>Nazwa kolumny z BD*</th>
                        <th>Długość rekordu*</th>
                        <th>Nazwa kolumny wyświetlana</th>
                        <th>Format*</th>
                    </tr>
                </thead>
                <tbody id="tblsort">
                    @for (var i = 0; i < Model.listOfFormats(Model.field.TemplateName).ColumnNamesDB.Count; i++)
                    {
                        <tr>
                            <td>@Html.TextBoxFor(m => Model.listOfFormats(Model.field.TemplateName).ColumnNamesDB[i], new { @class = "form-control", })</td>
                            <td>@Html.TextBoxFor(m => Model.listOfFormats(Model.field.TemplateName).LengthColumns[i], new { @class = "form-control" })</td>
                            <td>@Html.TextBoxFor(m => Model.listOfFormats(Model.field.TemplateName).ColumnNamesUser[i], new { @class = "form-control" })</td>
                            <td>@Html.DropDownListFor(m => Model.listOfFormats(Model.field.TemplateName).Formats[i], new SelectList(Model.formatFieldValues, "Value", "Text"), Model.listOfFormats(Model.field.TemplateName).Formats[i], new { @class = "form-control" })</td>
                        </tr>
                    }
                </tbody>
            </table>

How can I get value from each cel in this table? Thanks for help !

edit:

Ok I mean every cell data.

I tried your's solutions but nothing works

here is mine .html which is generate by browser

<tbody id="tblsort">
                        <tr>
                            <td name="cos"><input class="form-control" id="ColumnNamesDB_0_" name="ColumnNamesDB[0]" type="text" value="zzzz" /></td>
                            <td><input class="form-control" id="LengthColumns_0_" name="LengthColumns[0]" type="text" value="111" /></td>
                            <td><input class="form-control" id="ColumnNamesUser_0_" name="ColumnNamesUser[0]" type="text" value="zzzzz" /></td>
                            <td><select class="form-control" id="Formats_0_" name="Formats[0]"><option value=""></option>
<option value="DD-MM-YYY">DD-MM-YYY</option>
<option value="DDMMYYY">DDMMYYY</option>
<option value="NRB">NRB</option>
</select></td>
                            </tr>
                    </tbody>

When I run this code:

 var value = $("#ColumnNamesDB[0]").attr('value');
    var bla = $('.ColumnNamesDB_0_').val();

    alert(bla);
    alert(value);

I'm getting "undefined". Whats wrong?

Upvotes: 0

Views: 425

Answers (2)

Alex
Alex

Reputation: 10216

I dont know what you mean by "value", but you can iterate over each <td> using jQuery each()

$(function() {

  var myArray = [];

  $('#tblsort td input').each(function() {
    var input = $(this),
      value = input.attr('value');

    myArray.push(value);
  });
  console.log(myArray);
});

Upvotes: 1

wan.Lynnfield
wan.Lynnfield

Reputation: 27

If what you mean is you want to grab ALL the cell's data. So you have to give the "name" attribute to each cell, and grab ALL the data using the name..

<td name="example[]"></td>

And you can grab it using the language you wanted (Jquery or PHP)..

Upvotes: 0

Related Questions