Gcobani Mkontwana

Gcobani Mkontwana

  • 567
  • 1.9k
  • 402.9k

How to pass data to the table in ajax?

Mar 1 2024 10:12 AM

Hi Team

I have search tab, works fine but now i want to figure out once that data gets retrieved from the active directory. It must displayed to the table body(DisplayName, EmailAddress, Department columns here).

// ajax call

<!--Modal form popup for user checks details-->
                        <div class="modal fade" id="checkDetailsModal" tabindex="-1" role="dialog" aria-labelledby="checkNamesModalLabel" aria-hidden="true">
                            <div class="modal-dialog modal-xl" role="document">
                                <div class="modal-content">
                                    <div class="modal-header">
                                        <h5 class="modal-title" id="checkDetailsModalLabel">Select People and Groups</h5>
                                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                            <span aria-hidden="true">&times;</span>
                                    <div class="modal-body">
                                        <div class="container-fluid">
                                            <div class="row">
                                                <div class="col-md-6 border-right">
                                                    <!-- Left Border: Search -->
                                                    <div class="d-flex justify-content-between align-items-center mb-3">
                                                        <div class="input-group">
                                                            <input type="text" id="searchInput" class="form-control" placeholder="Search">
                                                            <div class="input-group-append">
                                                                <button class="btn btn-outline-secondary" id="searchButton" type="button">
                                                                    <i class="fas fa-search"></i> Search

                                                    <div class="border p-3 ">
                                                        <!-- Dropdown List or Other Content Here -->
                                                <div class="col-md-6">
                                                    <!-- Right Border: Table -->
                                                    <div class="d-flex justify-content-between align-items-center mb-3">
                                                        <h5 class="mb-0">Details</h5>
                                                    <div class="border p-3 table-container table-responsive">
                                                        <!-- Table with Display Name, E-mail Address, Title, Department, Presence, Work Phone, Location -->
                                                        <table class="table table-bordered">
                                                                    <th>Display Name</th>
                                                                    <th>E-mail Address</th>
                                                                    <th>Work Phone</th>
                                                                <!-- Table Body Content Here -->
                                                                <!-- Add more rows as needed -->

                                    <!--search for active users at the organisation-->
                                                                    $(document).ready(function () {
    // Function to handle search button click event
    $('#searchButton').on('click', function () {
        var searchTerm = $('#searchInput').val().trim();
        if (searchTerm !== '') {
            // Send an AJAX request to the controller action to search for users
                url: '@Url.Action("SearchUsers", "Home")',
                type: 'POST',
                dataType: 'json',
                data: { searchTerm: searchTerm },
                success: function (data) {
                    // Clear previous search results
                    $('#userTable tbody').empty();
                    // Iterate through the retrieved user details and append them to the table
                    $.each(data, function (index, user) {
                        var row = '<tr>' +
                            '<td>' + user.DisplayName + '</td>' +
                            '<td>' + user.Email + '</td>' +
                            '<td>' + user.Title + '</td>' +
                            '<td>' + user.Department + '</td>' +
                            '<td>' + user.Presence + '</td>' +
                            '<td>' + user.WorkPhone + '</td>' +
                            '<td>' + user.Location + '</td>' +
                        $('#userTable tbody').append(row);
                    // Show the modal with search results
                error: function (jqXHR, textStatus, errorThrown) {
                    // Handle error if any
                    console.error('Error:', textStatus, errorThrown);



Answers (2)