Am I using the if statement correctly?

Can someone confirm if I am using the if statement correctly in this script? Or is it not possible to use an if statement within this code?

<script type="text/javascript" charset="utf-8">
$(document).ready(function () {
    $('#customerTable').dataTable({
        "bServerSide": true,
        "sAjaxSource": "Customer/AjaxHandler",
        "bProcessing": true,
        "aoColumns": [{
                "sName": "CustomerId",
                "bVisible": false
            }, {
                "sName": "NamaPerusahaan",
                "bSearchable": true,
                "bSortable": true,
                "fnRender": function (oObj) {
                    return '<a href="/Customer/Details/' +
                        oObj.aData[0] + '">' + oObj.aData[1] + '</a>';
                }
            }, {
                "sName": "Alamat1"
            }, {
                "sName": "Telephone"
            },
            //below is the datetime
            {
                "sName": "NonActiveDate",
                "bSearchable": true,
                "bSortable": true,
                "fnRender": function (oObj) {
                    if(oObj.aData[4] <= @DateTime.Now) {
                        return '<span style="color:green">' + oObj.aData[4] + ' </span>';
                    } else {
                        return '<span style="color:red">' + oObj.aData[4] + ' </span>';
                    }
                }
            }, {
                "sName": "Edit",
                "bSearchable": false,
                "bSortable": false,
                "fnRender": function (oObj) {
                    return '<a href="/Customer/Edit/' + oObj.aData[0] + '" class="btn mini blue"><i class="icon-edit"></i>Edit</a>';
                }
            }, {
                "sName": "Delete",
                "bSearchable": false,
                "bSortable": false,
                "fnRender": function (oObj) {
                    return '<a href="/Customer/Edit/' + oObj.aData[0] + '" class="btn mini red"><i class="icon-trash"></i>Delete</a>';
                }
            }
        ]
    });
});
</script>

The font color for TglNonAktif becomes all green with this code. I suspect there might be an issue with the if statement. Any assistance would be greatly appreciated!

Answer №1

Q: "Am I using the if statement correctly?"

A: Not quite, but it's a good start :)

By injecting Razor into lengthy JavaScript code, you limit your ability to place that code into a separate file for bundling and caching purposes.

If you aim to inject a server-side variable onto the client side, consider injecting it into a global variable like window as shown below:

<script type="text/javascript">
    window.loadDate = @(DateTime.Now.ToString('appropriateformattinghere'));
</script>

This allows you to then reference this variable from any JavaScript file, such as:

if(oObj.aData[4] <= window.loadDate)

The example assumes a numeric value will be generated (like a date).

The important question here is, what is the format of oObj.aData[4]? Is it a JavaScript Date object? Please provide specifics or examples for clarity.

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

Refresh a chart in vue using the chart.js library

Within my Vue application, I have a function that generates a chart. This function looks like this: startChart: function (canvas, type) { // initialize chart.js var ctx = document.getElementById("myChart"); var myDoughnut = new Chart(ctx, { ...

Unable to execute xmlHttp.responseXML on a server that is offline

Currently, I am diving into the world of AJAX and XML. However, I recently encountered a frustrating issue. I attempted to create a basic program that would display everything I input into an input box within a <div>. Strangely enough, my program fa ...

In what way does the Node console showcase decimal numbers in floating point format?

While I understand the challenges of representing base 2 numbers in base 10 due to rounding issues in programming languages, my experience with the NodeJs console has left me puzzled. It is a known fact that base 2 numbers cannot perfectly represent 0.1 in ...

What is the correct method for accessing and reading each line in a file using node.js and jade?

Just starting out with Node.js and I'm looking to open a file and read each line. Can anyone provide guidance? Here's the code snippet, which can be implemented in Ruby (html.erb format). <% File.open("/home/ubuntu/test.text", "r").each_line ...

Watching a Computed Value in EmberJS

What causes the discrepancy between the two sets of code? Utilizing computed: computed: Ember.computed('selected', function() { console.log('computed'); return this.get('selected'); }), observer1: Ember.observer(&ap ...

Tips on arranging text around an image to prevent any overlapping

CLICK HERE FOR THE ISSUE This is the outcome. I prefer the text to be separate from the image. ...

Accordion A and B both require the first category of accordion A to be opened first in order for the first category of accordion B to also be

Greetings, this is my initial inquiry, please excuse any errors as English is not my first language. For my project, I am utilizing Angular 6, jQuery, and BS. I have two accordions, each with different categories: A1 and A2. My goal is for clicking on A ...

Using Angular's $q service in the run block

I have a scenario where I need to load data from local files using the global loadJSON function (though it's not mandatory, it's recommended). Once the data is loaded from all the documents, I want to print the string 'i ve loaded'. T ...

Creating responsive images in Bootstrap columns

I've found a solution to my issue with large images, but I am still struggling with smaller images not fitting well into larger spaces. Essentially, I created a CMS for a user who required templates with various bootstrap columns as content areas. Cu ...

Show concealed elements above everything else

I've encountered an issue with my custom dropdown list as it displaces other elements when it appears. I want it to overlay on top of everything else, similar to the default select behavior. Despite trying to set position: relative; and z-index:100;, ...

Utilizing JavaScript Objects within AMD modules using RequireJS: A step-by-step guide

A module example is available for testing. define([ 'components/user/list/usersList.require', 'components/user/manage/userManage.require' ], function (usersListRequire, userManageRequire) { "use strict"; var userPath = ...

Unable to process JavaScript function

Still in the process of developing my "mvc/social" PHP project, I am currently focusing on securing user input for the status message. I have created both a PHP and JavaScript function for this purpose, but it seems like the JavaScript function is not bein ...

Attempting to display a singular form

Currently, I am working on a MERN app and encountering a small issue... I am developing an application where users can create rooms and within those rooms, they can plan their daily activities. It's similar to a TODO app but more intricate. I wanted ...

I need to loop through an array of ids and add a new mongodb document for any ids that are missing. What is the best way to ensure that they are saved permanently?

Currently, I am utilizing Node JS and MongoDB to manage a collection of documents containing IDs ranging from 1 to 5000. Nevertheless, there are certain IDs that are absent, and I would like each document to be assigned one unique ID. Below is the snippet ...

Issues with Response Time in jQuery Reaction Tests

I recently encountered a couple of challenges while working on a reaction test: Firstly, I wanted to incorporate a random pause (2-5 seconds) after the user clicks on a div. Secondly, I aimed to have the divs appear five times in total, providing the ...

Utilizing the "or" operator instead of "and" when integrating validation methods in jQuery Validation plugin

When using the jQuery Validation plugin, you may come across multiple methods that serve the same purpose but for different locales. For instance, dateISO and dateDE both validate date formatting. How can these be combined to allow the input element to acc ...

Trouble parsing JSON in Classic ASP

After receiving a JSON Response from a remote server, everything looks good. I discovered an helpful script for parsing the JSON data and extracting the necessary values. When attempting to pass the variable into JSON.parse(), I encountered an error which ...

Storing numerous JSON records into an array by parsing them from a file

As a beginner in JS programming, I have a question that may seem trivial. Despite hours of searching online, I haven't been able to find a solution that works for me. I am dealing with multiple JSON feeds where each URL provides a multilayer JSON rec ...

Unable to vertically align radio buttons

Using Bootstrap 4, I want to create a list of three radio buttons. Here is the HTML structure I have tried: <div class="mt-3 text-center"> <div class="custom-control custom-radio"> <input type="radio" id=& ...

Attempted to execute a Vuex mutation outside of the designated store handler

Within my program, I have designed a form that consists of two checkboxes and a submit button for demonstration purposes. Upon clicking a checkbox, the selection is saved to a variable, which is then pushed to the vuex store when the submit button is click ...