Customizing arrays in DataTables allows for creating new columns and displaying them in the table according to the specified customizations

Displaying dynamic data in a data table involves taking an array as input and customizing the fields within that array.

Answer №1

Add a specific class to the <table> tag that is designated as the data table

Javascript

var ApplyDataTableClass = function (data) {
        table = $('.datatable').DataTable({
            retrieve: true,
            data: data,
            columns: [
                { data: 'EmployeeID' },
                { data: 'EmployeeName' },
                { data: 'DOB', type: 'date-dd-mmm-yyyy', targets: 0, width: '100px' },
                { data: 'Salary' },
                { data: 'DepartmentName' },
                { data: 'CountryName' },
                { data: 'StateName' },
                { data: 'CityName' },
                {
                    data: null,
                    render: function (data, type, row) {
                        var html = "";
                        html += '<button class="btn btn-primary" data-viewtype="' + data.EmployeeID + '" id="' + data.EmployeeID + '" title="View"><span class="fa fa-user"></span></button>&nbsp';
                        html += '<button class="btn btn-success" data-edittype="' + data.EmployeeID + '" id="' + data.EmployeeID + '" title="Edit"><span class="fa fa-pencil"></span></button>&nbsp';
                        html += '<button class="btn btn-danger" data-deletetype="' + data.EmployeeID + '" id="' + data.EmployeeID + '" title="Delete"><span class="fa fa-trash"></span></button>';
                        return html;
                    }
                }
            ],
        });
    };

HTML

    <table id="table_id" class="datatable datagrid">
        <thead>
            <tr>
                <th>EmployeeID</th>
                <th>EmployeeName</th>
                <th>DOB</th>
                <th>Salary</th>
                <th>Department</th>
                <th>Country</th>
                <th>State</th>
                <th>City</th>
                <th>Action</th>
            </tr>
        </thead>
    </table>

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

Press the button to update several span elements

Imagine I have multiple span elements like this: <span>A</span> <span>B</span> <span>C</span> <span>D</span> and a div element (which will be converted to a button later) named "change". <div id="chan ...

Problems with radio button serialization in jQuery form plugin

I've created a basic form: <form class="dataform" method="post" id="settings" action="/"> <input type="radio" name="shareSetting" value="n"/> <input type="radio" name="shareSetting" value="y"/> <input type="button" na ...

Retrieving JSON data and transforming it into HTML

After reading several examples, I am still struggling with a particular issue. I am receiving a feed from an API that is structured like this: { total:123, id: "1234", results: [ { id: "1234", name: "bobs market", ...

The instance is referencing "greet" during render, but it is not defined as a property or method

At the same time, I encountered another error stating "Invalid handler for event 'click'". <template> <div id="example-2"> <!-- `greet` is the name of a method defined below --> <button v-on:cli ...

Why is my column getting devoured even though it has custom content tailored to it?

In my layout, I have one column, one row, and three nested columns. Each column contains a custom-designed button instead of using the default Bootstrap button. However, there seems to be an issue where one of the columns is getting cut off. To better und ...

Validating decimals in a text field with either JavaScript or jQuery

I need to implement text field validation on the keyup event. The text field should only accept money type decimals such as: (12.23) (.23) (0.26) (5.09) (6.00) If any incorrect value is entered, it should revert back to the previous value and remove the ...

What is the best way to delay form submission until all asynchronous ajax calls have completed in jQuery?

Continuing from a previous inquiry regarding the disabling of a submit button until all ajax calls have completed... It appears that individuals are still able to submit the form despite the button being disabled and a warning displayed. This could possib ...

What is the method used by Chrome dev tools to create CSS selectors?

When setting a new style rule for an element, Chrome generates a selector that includes the entire hierarchy of elements instead of just the class name. For instance: <body> <div class="container"> <span class="helper"> ...

Manipulating div positions using JQuery and CSS

Whenever I hover over the #HoverMe div, a hidden #hidden div appears, and when I unhover it, the hidden div disappears again. The #hidden div contains a sub-div that functions like a dropdown list. The #hidden div is styled with position: absolute;. How ca ...

Is there a way to create an animation for changing .text in response to an on click event?

I'm currently developing a simple calculator that increases a variable when a button is clicked, displaying the updated value in a div. Each click on the 'amount-upwards' button adds 200 to the displayed number. Below is the jQuery code I a ...

Text Separation Within a Singular JSON Object

Currently, I am offering my assistance as a volunteer to develop a fast AngularJS App for a non-profit organization. Although I have successfully set up Angular, the main issue I am encountering has to do with parsing JSON data. The JSON object that I am ...

Take out the "href" attribute from the parent element

I'm looking to disable the link for the "parent li" in a dropdown menu when viewed on mobile. Here's the HTML structure: <ul> <li class="parent"></li> <li class="parent"></li> <li class="parent"></li&g ...

form for submitting multiple data via Ajax

I am working with two forms (request and feedback) where I need to use jQuery Ajax to send the data. When a user submits a request, the subject line will display "Request". If they submit feedback, the subject line will display "Feedback". Here is my cur ...

Is it advisable to opt for window.webkitRequestAnimationFrame over setInterval?

Trying to figure out the best method for moving game characters in my JavaScript game - should I go with window.webkitRequestAnimationFrame or stick with setInterval? Any advice is appreciated! ...

Deactivate the button once the form has been submitted

Seems like a simple and common question, right? I also thought so. How can I achieve this in angularJS? CSHTML @using (Html.BeginForm("Order", "Shop", FormMethod.Post, new { @class = "form-horizontal", role = "form" })) { <div class="container" ng-ap ...

Use the Segoe UI typeface on Internet Explorer for a sleek

I have implemented a custom Segoe UI font in my CSS file. While it displays correctly in Chrome, I am facing an issue where IE and Firefox are showing the default font in certain areas. Below is the snippet of the CSS code: body { margin:0px auto; ...

Laravel 8 - sweet alert functions properly, however, the ajax functionality is not functioning as expected

As a newcomer to Ajax, I am facing an issue with deleting records from the database using Sweet Alert2. Although my swal is working fine, the ajax function seems to be malfunctioning. Can anyone point out where the problem might be and suggest a solution? ...

core.js:15723 ERROR TypeError: Unable to access the 'OBJECT' property because it is undefined

Whenever I attempt to run this function, I encounter an issue. My goal is to retrieve the latitude and longitude of an address from Google's API. This error message pops up: core.js:15723 ERROR TypeError: Cannot read property 'geometry' of ...

Submit button on Ajax form causes automatic page refresh

After adding a simple Ajax form to my WordPress website, I encountered an issue where the validation works fine, but instead of sending the email, the form refreshes after submitting. Here is the code snippet: <script type="text/javascript"> jQ ...

Strangely Bizarre CSS Quirk

At this website, there seems to be an issue with how the footer displays on Internet Explorer and certain older versions of Firefox. However, it appears perfectly fine on Firefox 3.6.13. Any suggestions on what could be causing this discrepancy? ...