Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the table row data with a click [duplicate]

I have a table and I wanted to allow user to click on any elements on the table to get the row of data. How do I do that?

For example, I have a table like this in my view.

      <table class="myTable">
      :     <tr class="table">
                <th class="table">
                    Client ID
                </th>
                <th class="table">
                    Client Name
                </th>
                <th class="table">
                    Client Address
                </th>
            </tr>

When I run the project I'll get something like this:

enter image description here

If user click on column Client Name: BBB. It will have a pop out window saying: Hi, you've selected Client ID: 002, Client Name: BBB, Client Add: xxxxx.

User can click on all columns and it will still return the whole row of data in the pop out window.

How to do this? Please help.

HTML: @model IEnumerable @{ ViewBag.Title = "Client"; Layout = "~/Views/Shared/_Layout.cshtml"; }

<div class="body">

<div class="outer">
    <div class="inner">
        <table class="myTable">
            <tr class="table">
                <th class="table">
                    Client Name
                </th>
                <th class="table">
                    Client Code
                </th>
                <th class="table">
                    Client Address
                </th>
                <th class="searchTable">
                    Client Lead Partner
                </th>
            </tr>
            @foreach (var i in Model)
            {
                <tr class="myTable">
                    <td class="table">@i.ClientName
                    </td>
                    <td class="table">@i.ClientCode
                    </td>
                    <td class="table">@i.ClientAddress
                    </td>
                </tr>
            }
        </table>
    </div>
</div>

like image 853
sniggy Avatar asked Jun 28 '13 02:06

sniggy


1 Answers

You can do this:

$("tr.myTable").click(function() {
    var tableData = $(this).children("td").map(function() {
        return $(this).text();
    }).get();

    console.log(tableData);
});

Returns a nice array of your data, then you can display it how you want.

Demo: http://jsfiddle.net/Sc5N7/

like image 130
tymeJV Avatar answered Oct 13 '22 04:10

tymeJV