Escape characters in cell values (strings)

Escape characters in cell values (strings)

AgerAger Posts: 2Questions: 1Answers: 0

I'm changing the behaviour of a table in Bootstrap; by directing to an object's detail page when clicking its row on the table. I just find the default bootstrap "Edit - Details - Delete" links at the end of a row to be a bit inelegant.

I'm having trouble getting a string from one of my tables. Instead of returning just the value, it's got a couple newline escape characters and whole bunch of spaces surrounding it.

Let's say I want to look up a user by their e-mail address, with the value in the last cell in the row being @item.Email

$(document).ready(function () {
$('#tbl_users').DataTable();

    $("#tbl_users").find("tr").click(function () {
        var userEmail = $(this).closest("tr").find("td.hidden").text();
        $.ajax({
            type: "GET",
            url: "/Account/UserDetails",
            data: { "userEmail ": userEmail }
        })
        .done(function (partialViewResult) {
            $("#tab_users").html(partialViewResult);
        })
    });
});

My controller receives the following string: "\n \n \n test@test.com\n "

I tried adjusting the string with Replace() and Trim() methods, both in javascript and in the controller, to no avail.
The strange thing is that I managed to make this kind of redirect work in a different table using the same method; the difference is that the object in question is being queried with an integer instead of a string value.

Is there a better way to retrieve a string from a datatable's cell, or is there a better work method you would recommend?

This question has an accepted answers - jump to answer

Answers

  • bindridbindrid Posts: 730Questions: 0Answers: 119
    Answer ✓

    Assuming the hidden td is still a part of your table, does this give you a clean address?

    var rowData =   $("#tbl_users").DataTable().rows($(this).closest("tr")).data()[0];
    var email = rowData.email
    

    If that does not work, then use a regular expression that removes whitespace, tabs, enter key, etc.

  • AgerAger Posts: 2Questions: 1Answers: 0
    edited May 2017

    That code returned "undefined", but it put me on the right track! I still took the whole row as an array, but changed the second part.

    var email = rowData[rowData.length-1]

    This got me the value inside the last cell in the row (the hidden cell where I put whatever I use for querying), with no weird formatting.

    Thanks a lot for your help!

This discussion has been closed.