Managing configuration records through a web interface often requires common operations such as adding, updating, deleting, and changing the status of records. This article demonstrates how to implement a RAG (Red, Amber, Green) configuration management interface using jQuery, AJAX, Bootstrap, and DataTables.
The implementation uses AJAX requests to communicate with the server without requiring a full page reload. It also provides client-side validation, confirmation dialogs, toast notifications, and a searchable DataTable for displaying RAG configuration records.
In this article, we will cover the following operations:
Loading RAG configuration data
Adding a new RAG configuration
Updating an existing RAG configuration
Deleting a configuration
Changing the active or inactive status
Validating input fields
Displaying success notifications
Displaying records using DataTables
Prerequisites
Before using the code, make sure the project includes the required client-side libraries:
jQuery
Bootstrap
Bootstrap JavaScript
DataTables
DOMPurify
The application should also provide the corresponding server-side endpoints used by the AJAX requests:
../RAGConfig/List
../RAGConfig/Add
../RAGConfig/GetbyID/{RAGID}
../RAGConfig/Update
../RAGConfig/Delete/{ID}
../RAGConfig/ChangeStatus/{ID}
These endpoints are responsible for retrieving and modifying the RAG configuration data.
Creating Toast Notifications
The application uses Bootstrap toast notifications to display operation results to users.
function ToastSuccess(Message) {
ToastMessage('Success', Message);
}
function ToastWarning(Message) {
ToastMessage('Warning', Message);
}
function ToastDanger(Message) {
ToastMessage('Danger', Message);
}
function ToastInfo(Message) {
ToastMessage('Info', Message);
}
The common ToastMessage function determines the appropriate CSS class based on the notification type.
function ToastMessage(tType, Msg) {
var number = Math.floor(Math.random() * 90000) + 10000;
var typeLoc = "text-bg-purple";
if (tType.toLowerCase() == "info")
typeLoc = "text-bg-blue";
else if (tType.toLowerCase() == "success")
typeLoc = "text-bg-Success";
else if (tType.toLowerCase() == "warning")
typeLoc = "text-bg-warning";
else if (tType.toLowerCase() == "danger")
typeLoc = "text-bg-danger";
var str =
'<div id="Toast' + tType + number +
'" class="toast ds-toast ' + typeLoc +
' border-0" role="alert" aria-live="assertive" aria-atomic="true">' +
'<div class="d-flex">' +
'<div class="toast-body">' +
DOMPurify.sanitize(Msg) +
'</div>' +
'<button type="button" class="btn-close me-2 m-auto" ' +
'data-bs-dismiss="toast" aria-label="Close"></button>' +
'</div>' +
'</div>';
$('#ToastContainer').append(str);
const toastE = document.getElementById("Toast" + tType + number);
const toastd = new bootstrap.Toast(toastE);
toastd.show();
}
The DOMPurify.sanitize() method is used before inserting the message into the generated HTML. This helps sanitize dynamically generated content before it is added to the DOM.
Loading Data When the Page Loads
When the document is ready, the loadData() function is called to retrieve the existing RAG configuration records.
$(document).ready(function () {
loadData();
$('#btnUpdate').hide();
$('#btnAddNew').click(function () {
ClearTextBox();
$('#Modal_Label').html('Add New RAG');
$('#RAGModel').modal('show');
});
$('#btnUpdate').click(function () {
Update();
});
$('#btnAdd').click(function () {
Add();
});
});
The loadData() function sends a GET request to the List endpoint.
function loadData() {
$.ajax({
url: "../RAGConfig/List",
type: "GET",
contentType: "application/json;charset=utf-8",
dataType: "json",
success: function (Data) {
if (AjaxSuccess(Data)) {
// Build and display the table
}
}
});
}
The response is used to dynamically construct an HTML table. Each record can contain information such as the RAG name, description, CSS class, hexadecimal color value, date, creation date, status, and available actions.
The records are processed using jQuery's $.each() method.
$.each(Data.ResponseData, function (key, item) {
html += '<tr>';
html += '<td>' + item.Row + '</td>';
html += '<td>' + item.RAG + '</td>';
html += '<td>' + item.Discription + '</td>';
html += '<td>' + item.CssClassName + '</td>';
html += '<td>' + item.ColorHexaValue + '</td>';
html += '<td>' + item.ShowDate + '</td>';
html += '<td>' + item.CreatedOn + '</td>';
html += '</tr>';
});
After the HTML is generated, it is inserted into the page.
$('#RAG_Data div').html('');
$('#RAG_Data').append(DOMPurify.sanitize(html));
$('#RAG_Data').show();
The DataTables plugin is then initialized to provide searching, pagination, and other table functionality.
$('#RAGTable').DataTable({
fixedHeader: true,
bFilter: true,
ordering: false,
paging: true,
searching: true,
info: true,
destroy: true,
language: {
sLengthMenu: "_MENU_",
searchPlaceholder: 'Search by Service Name'
}
});
Adding a New RAG Configuration
The Add() function is used to create a new RAG configuration. Before sending the data to the server, it calls the Validate() function to check the required fields.
function Add() {
var res = Validate();
if (res == false) {
return false;
}
var pageObj = {
Status: 0,
RAG: TrimData($('#RAG').val()),
Discription: TrimData($('#Discription').val()),
CssClassName: TrimData($("#CssClassName").val()),
ColorHexaValue: TrimData($("#ColorHexaValue").val())
};
// AJAX request
}
The values entered by the user are stored in the pageObj object. The object is converted into JSON using JSON.stringify() and sent to the server using an AJAX POST request.
$.ajax({
url: "../RAGConfig/Add",
data: JSON.stringify(pageObj),
type: "POST",
contentType: "application/json;charset=utf-8",
dataType: "json",
success: function (Data) {
if (AjaxSuccess(Data)) {
ToastSuccess('Success! - ' + Data.ResponseText);
loadData();
$('#RAGModel').modal('hide');
}
}
});
After a successful response, the table is refreshed and the modal is closed.
Retrieving a RAG Record for Editing
When the user selects the edit option, the GetbyID() function retrieves the selected record using its RAG ID.
function GetbyID(RAGID) {
$('#RAG').removeClass('border-red').addClass('border-green');
$('#Discription').removeClass('border-red').addClass('border-green');
$('#CssClassName').removeClass('border-red').addClass('border-green');
$('#ColorHexaValue').removeClass('border-red').addClass('border-green');
$.ajax({
url: "../RAGConfig/GetbyID/" + RAGID,
type: "GET",
contentType: "application/json;charset=UTF-8",
dataType: "json",
success: function (Data) {
if (AjaxSuccess(Data)) {
ClearTextBox();
$('#Modal_Label').html('Update RAG');
$('#RAG').val(Data.ResponseData[0].RAG);
$('#Discription').val(Data.ResponseData[0].Discription);
$('#RAGID').val(Data.ResponseData[0].RAGID);
$('#CssClassName').val(Data.ResponseData[0].CssClassName);
$('#ColorHexaValue').val(Data.ResponseData[0].ColorHexaValue);
$('#RAGModel').modal('show');
$('#btnUpdate').show();
$('#btnAdd').hide();
}
}
});
return false;
}
The returned values are assigned to their respective form fields. The same modal can then be used to update the selected record.
Updating a RAG Configuration
The Update() function is responsible for updating an existing RAG configuration.
First, it validates the input fields.
function Update() {
var res = Validate();
if (res == false) {
return false;
}
var empObj = {
Status: 0,
RAG: TrimData($('#RAG').val()),
Discription: TrimData($('#Discription').val()),
RAGID: TrimData($('#RAGID').val()),
CssClassName: TrimData($("#CssClassName").val()),
ColorHexaValue: TrimData($("#ColorHexaValue").val())
};
// AJAX request
}

Join the conversation! Your thoughts help the community grow.