Help in javascript and asp.net mvc

3

In my project I have a field that is the Status of a given occurrence. That is, whether it is pending or resolved. I made a function in javascript that, when clicking the input field that is in readonly mode, changes the background. I mean, if the value inside the input is solved, the input goes green, and if the input value is Pending it turns red.

What happens is that it works perfectly, but to have a problem: this function is only done in the first occurrence of the input. I have an index, and it has a foreach to iterate over all the database data for occurrences, that is, I have several inputs on the status.

What happens is that the function only runs on the first occurrence of the list, the rest nothing happens.

Another thing is that, I wanted to save the or edit the occurrence, when I returned to the index to show, I already applied the function, without the user having to click or hover over. If it were automatic, I say, if the value of the input is Pending, it would turn red, if the value is resolved, it would turn green.

Can anyone help me?

View (where is the whole code)

@*@model IEnumerable<CEF01.Models.Ocorrencia>*@
    @model PagedList.IPagedList<CEF01.Models.Ocorrencia>
        @using PagedList.Mvc;
       <link href="~/Content/PagedList.css" rel="stylesheet" type="text/css" />
   <style>
       #Status {
           text-align: center;
             }

        .resolvido {
    background-color: green;
    font-family: 'Times New Roman';
    color: white;
           }

         .pendente {
    background-color: red;
    font-family: 'Times New Roman';
    color: white;
        }
       </style>

        @{
          ViewBag.Title = "Index";
     }


       @Html.ActionLink("Criar uma nova ocorrência", "Adiciona", null, new { @class = "btn btn-primary" })
              @Html.ActionLink("Lista de Alunos", "Index", "Alunos", null, new { @class = "btn btn-primary" })

             <span class="pull-right">
            @using (Html.BeginForm("Index", "Ocorrencias", FormMethod.Get))
            {
    <p>
        Busca Aluno: @Html.TextBox("busca")
        <input type="submit" value="Procurar" class="btn btn-info" />
    </p>
           }

           </span>

            <table class="table">
             <tr>
    <th>
        @*@Html.DisplayNameFor(model => model.Aluno.Nome)*@
        Nome do Aluno
    </th>
    <th>
        @*@Html.DisplayNameFor(model => model.Tipo)*@
        Tipo
    </th>
    <th>
        @*@Html.DisplayNameFor(model => model.Causa)*@
        Causa
    </th>
    <th>
        @*@Html.DisplayNameFor(model => model.Descricao)*@
        Descrição
    </th>
    <th>
        Status
    </th>
</tr>

@foreach (var item in Model)
{
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Aluno.Nome)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Tipo)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Causa)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Descricao)
        </td>
        <td>
            @*@Html.DisplayFor(modelItem => item.Status)*@
            <input type="text" name="Status" id="Status" value="@Html.DisplayFor(modelItem => item.Status)" readonly onmousemove="trocaCor()" />
        </td>
        <td>
            @Html.ActionLink("Editar", "Edita", new { id = item.Id }) |
            @* @Html.ActionLink("Details", "Details", new { id=item.Id }) | *@
            @Html.ActionLink("Remover", "Remove", new { id = item.Id })
        </td>
    </tr>
        }

        </table>
          <br />
              Pagina @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) de @Model.PageCount
        @Html.PagedListPager(Model, pagina => Url.Action("Index", new { pagina, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter }))

       @section Scripts {
@Scripts.Render("~/bundles/jqueryval")

<script>
    function trocaCor() {
        var troca = document.getElementById("Status");
        if (troca.value == "Pendente") {
            $("#Status").addClass("pendente");
        }
        if (troca.value == "Resolvido") {
            $("#Status").addClass("resolvido")
        }
    }
</script>
      }
    
asked by anonymous 02.06.2014 / 20:16

2 answers

4

If you want javascript to interact with all objects, use classes. By Id only one will be affected.

Add the Status class

<input type="text" name="Status" id="Status" class="Status" value="@Html.DisplayFor(modelItem => item.Status)" readonly onmousemove="trocaCor()" />

Script to load in the index to show already the correct color:

<script>
$(document).ready(function() {
    $('.Status').each(function(){
        if ($(this).val() == "Pendente") {
            $(this).addClass("pendente");
        }
        if ($(this).val() == "Resolvido") {
            $(this).addClass("resolvido")
        }
    });
});
</script>

You have an option that does not even need Javascript. Just with css and do not need the class tbm. Simply: link

<style>
    input[value=resolvido] { background-color: green }
    input[value=pendente] { background-color: red }
</style>
    
02.06.2014 / 20:47
4

If you want to color input according to status when loading page, you can make a jquery selector for this. In your input , you are using id="Status" , this is invalid, we can not have multiple elements with the same id , change id="Status" to class="status" , see example:

View HTML:

<input type="text" name="status" class="status" value="Pendente" readonly />
<input type="text" name="status" class="status" value="Resolvido" readonly />

Jquery

$(document).ready(function(){
    $("input[value='Resolvido'].status").addClass('resolvido');
    $("input[value='Pendente'].status").addClass('pendente');
});

Example: JSFiddle

    
02.06.2014 / 21:15