Guide on breaking down a series of values within a jQuery datatable

I am facing an issue with a datatable where one column contains a list of values, such as Activity Phase Dates in a comma-separated format. Using

render: function (data) { return moment(data).format("MM/DD/YYYY");
directly is not feasible because a list of dates is not considered a valid date.

How can I split this list of dates to apply the

render: function (data) { return moment(data).format("MM/DD/YYYY");
method? I have attempted to use split(','), but it does not yield the desired result, most likely due to the fact that the list of values is not considered a string. Another attempt was made using replace(',', ' '), which also did not work for the same reason.

Actions:

    public JsonResult LoadApplications()
    {
        return Json(new { data = GetApplications("") }, JsonRequestBehavior.AllowGet);
    }

    private IEnumerable GetApplications(string keyword)
    {
        var applications = from a in _db.Applications
                           where a.AppNumber.ToString().Contains(keyword)
                               || a.NonCityMortgageDate.ToString().Contains(keyword)
                               || a.ApplicationActivityPhas.Any(d => d.ActivityPhaseDate.ToString().Contains(keyword))
                           select new
                           {
                               a.AppNumber, a.NonCityMortgageDate, 
                               ActivityPhaseDates = a.ApplicationActivityPhas.Select(d => d.ActivityPhaseDate).ToList(),
                               a.Id
                           };

        return applications;
    }

DataTable:

    $(document).ready(function () {
        $("#ApplicationDataTable").DataTable({
            ajax: {
                url: '@Url.Action("LoadApplications", "Application")',
                datatype: "json",
                type: "GET"
            },
            columns: [
                {
                    data: "AppNumber",
                    render: function (data, type, row) {
                        var applicationDetails = '@Url.Action("Details", "Application")/' + row.Id;
                        return '<a href="' + applicationDetails + '">' + data + '</a>';
                    }
                },
                {
                    data: "NonCityMortgageDate",
                    type: "date",
                    render: function (data) {
                        if (data != null) {
                            return moment(data).format("MM/DD/YYYY");
                        }
                        else {
                            return "";
                        }
                    }
                },
                {
                    data: "ActivityPhaseDates",
                    type: "date",
                    render: function (data) {
                        return moment(data).format("MM/DD/YYYY");
                    }
                },
                { data: "Id" }
            ]
        });
    });

Index:

    <div class="pt-2">
        <table class="table table-bordered table-sm" id="ApplicationDataTable">
            <thead class="table-info">
                <tr>
                    <th>Application Number</th>
                    <th>Non City Mortgage Date</th>
                    <th>Activity Phase Dates</th>
                    <th>Id</th>
                </tr>
            </thead>
        </table>
    </div>

Answer №1

Here is a unique solution:

{
    type: "ActivityDates",
    display: function (dates) {
        var formattedDates = "";
        for (var index = 0; index < dates.length; index++) {
            formattedDates += moment(dates[index]).format("MM/DD/YYYY") + " ";
        }
        return formattedDates;
    }
}

The result shows the following dates:

03/14/2022 03/31/2022 03/31/2022 03/31/2022 03/31/2022

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Why is it necessary to click twice with AjaxUpload?

Utilizing AjaxUpload.2.0.min.js, the following code facilitates file uploads to the server. An issue arises where multiple clicks on the “Add File” button are required for the OS window (for selecting the file to upload) to appear, rather than just on ...

Is jQuery's show() function compatible with tables in Internet Explorer?

I have a table where some of the rows are initially hidden (with display:none) and I am using jQuery to make them visible. As per my observation in Firebug, jQuery recognizes these as table rows and makes them appear with display:table-row instead of block ...

Updating the button's appearance after submission using jQuery and Django

I have implemented an ajax "follow" button on my website. Currently, the button's attribute disabled changes to "disabled" after submission. However, I would like to modify the button from "follow" to "unfollow" upon submission, similar to how it work ...

Launch the desired div in a fancybox from a separate webpage

i have a table with links to another html doc like this <div id="gallery_box"> <ul> <li> <a href="http://www.geestkracht.com" target="_blank"><img src="images/gallery/Geestkracht.jpg" alt="G ...

Monitor responses from ajax POST requests using jQuery

Currently, I am tackling a project involving various ajax actions. The problem arises from the fact that these ajax calls are executed by scripts that I would rather not delve into. In addition, the responses they generate are erratic and lack a consisten ...

Display Numerous Values Using Ajax

Having difficulty showing data from the 'deskripsisrt' table in a modal bootstrap? I have successfully displayed from the 'srtptr' table, but not sure how to proceed with the 'deskripsisrt' table. Here's a snippet from my ...

Using Jquery to update links based on the cookie value, then replacing the original link with the new one after it has

I have a unique cookie value: snackAttack Here are some links with the same class "t": <a href="link1.html" class="t">link1</a> <a href="link2.html" class="t">link2</a> <a href="link2.html" class="t"><img src="image2.jpg ...

Understanding how to retrieve a particular list item in JQuery without having the index in advance

I have a lengthy list that is broken down into various subheadings. How can I retrieve the second to last element of the list before a specific subheading, provided it is not the final element? Is it possible to do this if I know the ID of the subheading? ...

Leveraging Masonry.js with dynamically created divs using jQuery

Recently, I discovered Masonry.js and was excited to incorporate it into my projects. To test my skills, I decided to create a page that would display 16 divs with random heights and colors every time I clicked a button. However, I'm encountering an i ...

What is the best way to target an HTML element that is only connected to a particular class?

My HTML code includes the following 3 elements: <span class="a b"></span> <span class="a"></span> <span class="a b"></span> I am trying to select the element that only has the class "a". When I use $("span.a"), it sele ...

To enhance user experience, consider incorporating a 'Next Page' feature after the completion of every four paragraphs,

Here is a code snippet that can be used to print 'n' number of paragraphs: <% while(rs.next()){ String para=rs.getString("poems"); %> <p> <%=para%> </p> <!-- n number of p tags are printe ...

Updating a div element dynamically using AJAX in DJANGO

Currently in the process of developing a chat application. Using jquery $.post() to add chat messages has been successful thus far. My next step is to fetch the most recent chat message from the table and update the list on the chat page. As a beginner in ...

Comparing two datetime objects with time zone offsets in JavaScript: How to determine if one is greater than or less than the other?

So I'm faced with a situation where I need to compare two dates where the first date is 05.01.2008 6:00 +5:00 and the second date is 05.01.2008 7:00 +5:00 I'm struggling to find a way to convert these datetimeoffsets into a specific forma ...

Having trouble displaying HTML content in jQuery autocomplete for MVC4

Encountering an issue with the error message "unable to get property of renderItem". Jquery Code $("#term").autocomplete({ // minLength: 1, // require atleast 2 characters to use source: function (req, resp) { // retrieving JSON object from Sear ...

How can I achieve a similar functionality to array_unique() using jQuery?

When I select values from a dropdown, they are stored as an array like ["1","2","3"] Upon each change, the code below is executed to generate a new array based on the selected values: $('#event-courses-type').on('change', function(){ ...

Invalid entry detected in JSON object

I developed a code to deserialize the JSON data from this API To start off, I created a class structure: public class Self { public string href { get; set; } } // Other class definitions omitted for brevity... public class RootO ...

Clear the modal form in Codeigniter and AJAX upon closing the edit modal

Having an issue with my modal form - when I open the edit modal, the data is fetched and that part works well. However, when I close the modal and try to add a new user, the data is automatically fetched again. Why is this happening? Shouldn't the for ...

Incorporating JS objects into HTML: A comprehensive guide

Here is a snippet of code from my JavaScript file: var formStr = "<h5>How many books?:</h5><input type='number' id='bookinput' value='' /><input type='button' value='submit' onclick=& ...

jQuery import children into output

When inserting a div every nth-child and later calling the children of the parent, the jQuery inserted elements do not appear in this new list. How can I retrieve the inserted elements as well? Apologies if this is confusing. If it's easier to under ...

Create a sleek transition for your Bootstrap 4 fixed Navbar by having it smoothly slide down and change to a

Throughout my experience with HTML/CSS, I have predominantly relied on themes that included this feature by default. However, as I now venture into building from scratch, I find myself struggling to recreate it. You'll notice that I have a stylish fi ...