0

Since the past two days, I am getting confused by implementing a simple way to allow the user the following steps:

  1. User has a table with entries
  2. He can click per row on an edit button
  3. A modal view opens
  4. User enters values
  5. On click on save button these values PLUS the id from the table will be transfered to my controller

What I have achieved so far is:

I am able to filter out the clicked entry after the user clicked edit and before the modal window opens by either

$(this).closest('tr').find('.userID').text()

or

$(button.relatedTarget).attr('id')

But I am not able to hand over the necessary userID to my updateModal() JS function. And I need the ID for my Spring MVC Controller.

When I try to get these parameters inside the updateUser() function they are gone. So between the user click on the edit button and the show modal I lose these information. They are only available straight after click on the edit button, but not later.

What I need is a way to make these parameters available after the modal view opened and that I can send the userID to the controller when she user clicks send.

I found this option:

localStorage.setItem('key', 'value'); 

But it doesn't help me since my updateUser function has no idea about what ID the modal is working with.

Here is my JS:

function updateUser() {
    alert("try getting parameter");
    alert($(this).closest('tr').find('.userID').text()); //not working HELP pls
    alert($(button.relatedTarget).attr('id')); //not working too
    alert("success?");
    var newValues = {};
    newValues["newName"] = $("#newName").val();
    newValues["newAge"] = $("#newAge").val();
    var userID = "???";

    $.ajax({
        type: "POST",
        contentType: "application/json",
        url: "/api/edit/" + userID,
        data: JSON.stringify(newValues),
        dataType: 'json',
        timeout: 100000,
        success: function (data) {
            console.log("SUCCESS: ", data);
            display("SUCCESS");
            resultObj = data.result;
        },
        error: function (e) {
            console.log("ERROR: ", e);
            display("ERROR");
            //display(2);
        },
        done: function (e) {
            console.log("DONE");
            display("DONE");
            enableSearchButton(true);
        }
    });
}

Here my HTML:

<!--- start table -->
<div class="container">
    <br>
    <h3>Overview</h3>
    <br>

    <div class="container" id="modal-submit">
        <div class="col-12">
            <table class="table table-hover">
                <thead>
                <tr>
                    <th scope="col">Name</th>
                    <th scope="col">Age</th>
                    <th scope="col">ID</th>
                </tr>
                </thead>
                <tbody>
                <!--/*@thymesVar id="productList" type="java.util.List"*/-->
                <tr id="person" th:each="person : ${elementList}">

                    <td th:text="${person.getName()}" id="username"></td>
                    <!--/*@thymesVar id="getTradableBTC" type="java.lang.Double"*/-->
                    <td th:text="${person.getAge()}" th:id="${person.getName()+person.getAge()}" id="age"></td>
                    <!--/*@thymesVar id="getTop" type="java.lang.Double"*/-->
                    <td th:text="${person.getId()} " th:id="${person.getName()+person.getId()}" class="userID"></td>

                    <td>
                        <div class="btn-toolbar" role="toolbar">
                            <!-- Button trigger modal -->
                            <div class="btn-group mr-2" role="group" aria-label="First group">
                                <button type="button" class="btn btn-outline-primary btn-sm" data-toggle="modal"
                                        data-target="#myModal" data-th-id="${person.getId()}">
                                    Edit
                                </button>
                            </div>
                            <div class="btn-group mr-2" role="group" aria-label="Second group">
                                <button type="button" class="btn btn-outline-danger btn-sm" id="delete">Delete
                                </button>
                            </div>
                        </div>
                    </td>
                </tr>
                </tbody>
            </table>
        </div>
        <button class="btn btn-primary" type="button" id="addElement">Add Element</button>
        <button class="btn btn-light" type="button" id="DeleteAll">Delete all Elements</button>
    </div>
</div>
<br>
<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog"
     data-keyboard="false" data-backdrop="static">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">Change data</h5>
                <button type="button" class="close" data-dismiss="modal"
                        aria-label="Close"><span
                        aria-hidden="true">&times;</span></button>
            </div>
            <div class="row">
                <div class="col-sm-6">
                    <div class="modal-body">
                        <input name="referencia" id="newName" type="text"
                               class="form-control"
                               placeholder="Enter new name">
                        <br>
                        <input name="referencia" id="newAge" type="text"
                               class="form-control"
                               placeholder="Enter new age">
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-light"
                        data-dismiss="modal">Discard
                </button>
                <button type="button" class="btn btn-primary"
                        onclick="updateUser()">Save
                </button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->
Marvin
  • 229
  • 1
  • 3
  • 11
Anna Klein
  • 1,441
  • 20
  • 46

1 Answers1

1

One possible solution to your problem is

  • When user clicks on table row get the UserID for that respective row i.e. $(this).closest('tr').find('.userID').text()
  • Store the User ID you retrieved above to model DOM

    var userId = $(this).closest('tr').find('.userID').text();
    $("#myModal").data('currentUserId', userId);
    
  • In updateUser get user ID from DOM.

    var userId = $("#myModal").data('currentUserId');
    

Basically you need to store the UserID in a global context where you can access that once model closes as updateUser and table row click both will be called in different context.

Further, I think you need to read this https://stackoverflow.com/a/3127440/2509344 to understand how this works in JavaScript.

Adnan Umer
  • 3,409
  • 2
  • 14
  • 35
  • I want to say special thanks not only for the solution more that you even provide an additional link that I understand what my problem was about and increase my understanding. Thats nice! – Anna Klein Mar 28 '18 at 09:12