2

I have a following razor to create a table.

@model IEnumerable<iBoxV5.Model.Common.AdvancedSearch>
<table id="SearchFieldsTable">
  <thead>
    <tr>
    <th>
        ColumnName
    </th>
    <th>
        FilterValues
    </th>
    <th>Updated Before</th>
    </tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr [email protected] [email protected] [email protected] [email protected] id=@(index++)>
    <td>
        @Html.DisplayFor(modelItem => item.ColumnName)
    </td>
    <td>
        @Html.TextBoxFor(modelItem => item.FilterValue, new { @class = "FrmTextBox advSearch" });

        @Html.TextBoxFor(modelItem => item.FilterValue, new { @class = "FrmTextBox advSearch" });
    </td>
    <td>
        @Html.TextBoxFor(modelItem => item.FilterValueTo, new { @class = "FrmTextBox advSearch" });
    </td>
</tr>
}
</tbody>
</table>

And In the above I have added few attributes for the like ColumnName.

I wants to build a JSON for the each row of the Table.

I tried with the following jquery Snippet.

var SearchFieldsTable = $("#SearchFieldsTable tbody");

var trows = SearchFieldsTable[0].rows;

$.each(trows, function (index, row) {
    var ColumnName=$(row).attr("ColumnName");
});

But the above is not returning the ColumnName I expected.

1
  • Please post the rendered markup instead of serverside codes. Commented Sep 20, 2012 at 10:28

5 Answers 5

5

try this;

$("#trowId").attr("attrbuteName");
Sign up to request clarification or add additional context in comments.

Comments

2

you can get the value by either class or id like

var ColumnName=$(row).attr(".ColumnName").val();

or

var ColumnName=$(row).attr("#ColumnName").val();

Comments

2

May be this will be helpful.

var SearchFieldsTable = $("#SearchFieldsTable tbody");

var trows = SearchFieldsTable.children("tr");

$.each(trows, function (index, row) {
    var ColumnName=$(row).attr("ColumnName");
    // .... Your codes here 
});

FYI: "tbody" don't have "rows". The following is wrong

SearchFieldsTable[0].rows

You can use the following code instead to use above code.

var SearchFieldsTable = $("#SearchFieldsTable");

Comments

0

Some browsers won't support custom attributes. So, you can better use data().

var ColumnName = $(row).data("ColumnName");

And in the row, define it as:

<tr [email protected] ...

Comments

0

You are defining a local variable in each iteration, now your ColumnName variable only stores the attribute of last row. Also ColumnName in not a valid attribute. You can use data-* attributes and map method.

var columnName = $("#SearchFieldsTable tbody tr").map(function(){
                      return $(this).data('ColumnName')
                 })

Now columnName is an array of column names.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.