0

Im new to jquery and need some help.

I have written a table that act like a grid with help from jquery.

When a user click on Name column the user will be redirected to the details page.

Now I want to know if I can do this in a better way?

And should the JS code be in the page or in a separated JS file?

Here is the code.

    <table id="grid">
    <thead>
        <tr>
            <th data-field="name">Namn</th>
            <th data-field="location">Ort</th>
            <th data-field="phone">Telefon</th>
            <th data-field="buildinmonth">Bygga inom</th>
            <th data-field="houselot">Har tomt</th>
            <th data-field="created">Skapad</th>
        </tr>
    </thead>
    <tbody>
        @foreach (var item in Model)
        {
            <tr>
                <td><span id="open" data-id="@item.Id">@item.Name</span></td>
                <td>@item.Location</td>
                <td>@item.Phone</td>
                <td>@item.BuildInMonth</td>
                <td>@item.HouseLot</td>
                <td>@String.Format("{0:d}", item.CreatedDate)</td>
            </tr>
        }
    </tbody>
</table>

<script>
        $("#grid").kendoGrid({
            scrollable: false,
            sortable: true
        });

    $("#grid #open").click(function () {
        window.location.replace("/lead/details/" + $(this).data("id"));
    });
</script>
1
  • 1
    javacode? What is this language you speak of? Commented Nov 6, 2012 at 22:55

2 Answers 2

2

In this scenario, dont need to use any jquery click event, when the html table is loading that time we can give a <a> tag like,

<td><a href="../lead/details/"+"@item.Id" ><span id="open" data-id="@item.Id">@item.Name</span></a></td>
Sign up to request clarification or add additional context in comments.

Comments

1

When a user click on Name column the user will be redirected to the details page. Now I want to know if I can do this in a better way?

This seems ok. The only thing I feel can be adde to this is on hover of the Name column, you can show a tooltip of "Show more details" or something more intuitive.

And should the JS code be in the page or in a separated JS file?

Yes I always prefer to have all my JS code in a seperate file. The advantage of doing so is that you can minify the JS code later on.

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.