突出显示表中的一行 - jQuery

时间:2014-11-18 18:12:38

标签: jquery highlighting tablerow

有!

我知道,我知道。关于此可能有数千个主题。

我正在尝试任何人都可以指出我这些代码的错误。

这就是重点:一个MVC应用程序。第一个视图(使用布局)有这个剃刀标记:

@model IEnumerable<ControleDeAcesso.Models.Entidades.Perfil>

@{
    ViewBag.Title = "Perfis";
}

<div class="container">
    <div class="row">
        <div class="col-md-4">
            <h2>Lista de Perfis</h2>
        </div>
        <div class="col-md-4 col-md-offset-4 text-right" >
            @Html.ActionLink("Novo Perfil", "CriarPerfil", null, new { @class = "btn btn-primary btn-lg" })
        </div>
    </div>
</div>

<table id="tblDados" class="table table-bordered">
    <thead>
        <tr class="tablehead">
            <th class="text-center">
                @Html.DisplayNameFor(model => model.Nome)
            </th>
            <th class="text-center">
                @Html.DisplayNameFor(model => model.Descricao)
            </th>
            <th></th>
        </tr>
    </thead>
    <tbody>
        @foreach (var item in Model)
        {
            <tr id="row-@item.Id">
                <td class="text-left">
                    @Html.DisplayFor(modelItem => item.Nome)
                </td>
                <td class="text-left">
                    @Html.DisplayFor(modelItem => item.Descricao)
                </td>
                <td class="text-right">
                    <button type="button" class="EditaPerfil btn btn-default btn-xs" data-id="@item.Id"><span class="glyphicon glyphicon-pencil" title="Edita o perfil"></span></button>
                    <button type="button" class="RemovePerfil btn btn-default btn-xs" data-id="@item.Id" data-nome="@item.Nome"><span class="glyphicon glyphicon-trash" title="Exclui o perfil"></span></button>
                </td>
            </tr>
        }
    </tbody>
</table>

<script src="~/Scripts/jquery-2.1.0.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
<script src="~/Scripts/meusscripts.js"></script>

meusscripts.js文件是这样的:

$(function () {
    $("#tblDados tr:gt(0)").hover(function () {
        $(this).addClass("highlightedRow");
    }, function () {
        $(this).removeClass("highlightedRow");
    });
});

使用此函数,我在使用jQuery的hover事件中突出显示id =“tblDados”的任何表中的一行。在这种观点中,它的工作非常好!

现在,我得到了另一个观点:

@{
    ViewBag.Title = "Novo Usuário";
}

<script type="text/javascript">
    $(document).ready(function () {

        $("#divListaAD").hide();

        $("#btnLimpar").click(function () {
            $('#txtPesquisa').val('');
            $("#tblDados > tbody").empty();
            $("#divListaAD").hide();
        });

        $("#btnPesquisar").click(function () {
            if ($("#txtPesquisa").val() != '') {
                var options = {};
                options.url = "/Administracao/ListaDeUsuariosDoAD";
                options.type = "GET";
                options.data = { strPesquisa: $("#txtPesquisa").val() };
                options.dataType = "json";
                options.contentType = "application/json";
                options.success = function (data) {
                    $("#tblDados > tbody").empty();
                    var result = data.Result;
                    for (var i = 0; i < result.length; i++) {
                        $("#tblDados > tbody").append(
                            "<tr>" +
                            "   <td>Selecionar</td>" +
                            "   <td>" + result[i].Login + "</td>" +
                            "   <td>" + result[i].DisplayName + "</td>" +
                            "   <td>" + result[i].Orgao + "</td>" +
                            "   <td>" + result[i].Email + "</td>" +
                            "</tr>");
                    };
                    $("#divListaAD").show();
                    $("#tblDados").tablesorter();
                };
                options.error = function () { alert("Erro ao buscar os usuários no AD!"); };
                $.ajax(options);
            }
            else {
                $("txtPesquisa").focus();
            }
        });

        $("#tblDados > tbody > tr").hover(function () {
            $(this).addClass("highlightedRow");
        }, function () {
            $(this).removeClass("highlightedRow");
        });

    });
</script>
<div class="panel panel-primary">
    <div class="panel-heading"><h4>Novo Usuário</h4></div>
    <div class="panel-body">
        <div class="col-md-9">
            <input type="text" name="txtPesquisa" id="txtPesquisa" class="form-control input-sm" placeholder="Digite o nome (ou parte) ou matrícula (Fx999999)" />
        </div>
        <div class="col-md-3 text-right">
            <button type="button" id="btnPesquisar" name="btnPesquisar" class="btn btn-primary btn-sm" title="Pesquisa em corp.furnas o nome ou matrícula">Pesquisar</button>
            <button type="button" id="btnLimpar" name="btnLimpar" class="btn btn-default btn-sm" title="Limpa os parâmetros de pesquisa">Limpar</button>
            @Html.ActionLink("Voltar", "Usuarios", null, new { @class = "btn btn-default btn-sm", @title = "Volta para a lista de usuários da aplicação" })
        </div>
        <br />
        <div id="divListaAD" style="margin-top: 30px;">
            <table id="tblDados" class="table table-bordered tablesorter">
                <thead>
                    <tr class="tablehead">
                        <th></th>
                        <th class="text-center">Logon</th>
                        <th class="text-center">Nome</th>
                        <th class="text-center">Órgão</th>
                        <th class="text-center">Email</th>
                    </tr>
                </thead>
                <tbody></tbody>
            </table>
        </div>
    </div>
</div>

<script src="~/Scripts/jquery-2.1.0.min.js"></script>
<script src="~/Scripts/jquery.tablesorter.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
<script src="~/Scripts/meusscripts.js"></script>
<link href="~/Content/style.css" rel="stylesheet" />

即使我将该功能带到我的视图文件的脚本部分,它仍然 工作!

我做错了什么?

感谢您的关注。

Paulo Ricardo Ferreira

0 个答案:

没有答案