Input specific ng-if conditions

I'm a bit confused about the conditions for my ng-if and could use some assistance. I have a form on my page that is rendered using ng-repeat and a couple of custom filters. You can check out this plunker. The issue I'm facing is that I need to provide comboboxes for only three keys from an array, but the placeholder should display the current value of the data. Here's the structure of my div:

<div ng-repeat="k in rowKeys | filter: '!0' | filter: '!$$'" ng-model="rowVal">
     <div ng-if="(k === 'id' || k.toLowerCase().endsWith('id') === false) ? true : false">
      <label for="rowValue[{{$index}}]" class="col-sm-2">
       {{k | hide:'.name' | makeUppercase }}:
       </label>
              <div class=" col-sm-2">
       <input ng-if="!isObject(rowData[k])" ng-disabled="disableInput(k)"  class="form-control rowValue" id="rowValue[{{$index}}]"
       ng-model="rowData[k]"/>

       <input ng-if="isObject(rowData[k])"
       ng-disabled="disableInput(k)"   class="form-control rowValue" id="rowValue[{{$index}}]"
       ng-model="rowData[k].name"/>
       <select ng-if="isObject(rowData[k]) && k == 'status'" ng-model="rowData[k].status" class="form-control">
          <option ng-repeat="item in status" value="{{item.value}}">{{item.label}}</option>
           </select>
        <select  ng-if="isObject(rowData[k]) && k == 'priority'" ng-model="rowData[k].priority" class="form-control">
         <option ng-repeat="item in priorities" value="{{item.value}}">{{item.label}}</option>
        </select>
       <select  ng-if="isObject(rowData[k]) && k == 'severity'" ng-model="rowData[k].severity" class="form-control">
      <option ng-repeat="item in severities" value="{{item.value}}">{{item.label}}</option>
      </select>
     </div>

There are three main conditions in my code:

  1. A simple key-value pair.
  2. An object key-value pair.
  3. An object key-value pair with restrictions by key.

The problem I'm facing is that I can't seem to write an expression that will properly render the combobox and input fields as needed. For example, if I do something like this

ng-if="isObject(rowData[k]) && k == 'status'"
, the input disappears completely. Additionally, I'm struggling with how to populate the current value into the combobox as a placeholder. If anyone can help me figure out where I'm going wrong, I would greatly appreciate it.

Here's the JavaScript code:

$scope.load = function(){

  $http({
       method: "GET",
       url: 'test.json'
       })
       .then(function success(response) {
              $scope.rowData = response.data;
                  console.log($scope.rowData)
       }, function error(response) {
                 console.log("An error occurred in the response")
  }).then(function(){
      $scope.id = $scope.rowData.id;
            console.log($scope.id);
           $scope.rowKeys = Object.keys($scope.rowData);
  })
}

$scope.isObject = function(value) {
    if(angular.isObject(value)) {
      return true;
    } else {
      return false;
    }
}

$scope.disableInput = function(obj) {

         if (obj === 'eventType'){
                return true
            }
         else if (obj === 'id'){
                return true
            }
         else if (obj === 'createdDate'){
                return true
            }
         else if (obj === 'occuredDate'){
                return true
            }
         else if (obj === 'study'){
                return true
            }
};

$scope.priorities = [
                { value: 'high', label: 'High' },
                { value: 'medium', label: 'Medium'},
                { value: 'low', label: 'Low'}
            ];
            $scope.severities = [
                { value: 'benign', label: 'Benign' },
                { value: 'severe', label: 'Severe'},
                { value: 'medium', label: 'Medium' },
                { value: 'mild', label: 'Mild'}
            ];
            $scope.status = [
                { value: 'new', label: 'Open' },
                { value: 'closed', label: 'Closed'}
            ];

})

Answer №1

Success! I managed to solve it on my own with this functional code

<div ng-repeat="k in rowKeys | filter: '!0' | filter: '!$$'" ng-model="rowVal" >
    <div ng-if="(k === 'id' || k.toLowerCase().endsWith('id') === false) ? true : false">

        <label for="rowValue[{{$index}}]" class="col-sm-2">

            {{k | hide:'.name' | makeUppercase }}:
        </label>
        <div class=" col-sm-2">
            <input ng-if="!isObject(rowData[k])"
                   ng-disabled="disableInput(k)"
                   class="form-control rowValue"
                   id="rowValue[{{$index}}]"
                   ng-model="rowData[k]"/>

            <input ng-if="isObject(rowData[k]) && k !== 'status' && k !== 'priority' && k !== 'severity' "
                   ng-disabled="disableInput(k)"
                   class="form-control rowValue"
                   id="rowValue[{{$index}}]"
                   ng-model="rowData[k].name"/>

            <select ng-if="isObject(rowData[k]) && k == 'status'"
                    ng-model="rowData[k].name"
                    class="form-control rowValue"
                    id="rowValue[{{$index}}]"
                    ng-options='stat.value as stat.label for stat in status' >
                <option value=''>{{rowData[k].name}}</option>
            </select>
            <select ng-if="isObject(rowData[k]) && k == 'priority'"
                    ng-model="rowData[k].name"
                    class="form-control rowValue"
                    id="rowValue[{{$index}}]"
                    ng-options='prior.value as prior.label for prior in priorities' >
                <option value=''>{{rowData[k].name}}</option>
            </select>
            <select ng-if="isObject(rowData[k]) && k == 'severity'"
                    ng-model="rowData[k].name"
                    class="form-control rowValue"
                    id="rowValue[{{$index}}]"
                    ng-options='sev.value as sev.label for sev in severities' >
                <option value=''>{{rowData[k].name}}</option>
            </select>
        </div>
    </div>
</div>

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

Validating Password Consistency using Javascript

I'm having trouble with validating the password fields and ensuring they match, even when both input boxes contain the same password. JavaScript var validator = $("#signupform").validate({ rules: { password: { required: true, minle ...

Top method to create a timer in React/Redux for automatically refreshing authentication tokens

I am implementing an access refresh jwt authentication flow and need the client to automatically send a refresh token after 10 minutes of receiving the access token. I also want to ensure that if the user's device is idle for an hour, a new access tok ...

Statement after post is not yielding any result

Below is a javascript function that I'm struggling with: function loginsubmit() { var url = "../php/loginsubmit.php"; var data = ""; ajaxRequest(url, "POST",data , true, insertNewBody); } This function is responsible for sending an ajax ...

Trouble encountered with card flip style login form in Vue.js, as the card is not maintaining its position properly during transition animations

Need help styling a login/signup component in Vue.js where flipping between the two cards isn't working smoothly. The transition is causing one card to move down and right while the other comes in from the bottom right. It's hard to explain the i ...

What kind of output should a Server Side Component generate?

Recently, I decided to incorporate the NextPage type from Next.js into my component writing routine after hearing it's a beneficial practice. However, I discovered that it only functions properly with client-side components. When attempting to utilize ...

"Discover the process of sending a JSON value from a PHP page to an HTML page using AJAX, and learn how to display the resulting data

I am currently validating an email ID using PHP and AJAX, with the intention of returning a value from the PHP page to the HTML in JSON format. My goal is to store this return value in a PHP variable for future use. All of this is being executed within C ...

A common inquiry: how can one pinpoint a location within an irregular figure?

I have been studying Html5 Canvas for a few weeks now, and the challenge mentioned above has puzzled me for quite some time. An irregular shape could be a circle, rectangle, ellipse, polygon, or a path constructed by various lines and bezier curves... I ...

Bootstrap is causing issues with unidentified div elements

I recently embarked on creating a slideshow using HTML, CSS, and jQuery. After completing the slideshow, I decided to add an interactive page beneath it. To streamline the layout process, I opted to utilize Bootstrap. However, upon loading Bootstrap, I en ...

Appending a JSON object to an array does not result in the object being added to the

Can anyone help me with an issue I'm facing? I have a code snippet where I am trying to push a JSON Object into an array, but the array is not updating properly. It only shows the last pushed element. var myData = {}; var id = 0; $("a").on('cli ...

Managing dependencies with Yarn or npm

While experimenting with remix and mui v5, I encountered some issues. When using npm and running npm run dev, I received the following error: Error: Directory import '.../playground/remix-mui-dev/node_modules/@mui/material/styles' is not supporte ...

Next/image is encountering an error due to an invalid Element type being generated

Trying to utilize the next/image feature to display an SVG image is causing me some trouble. Every time I attempt this, an error message pops up: Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite ...

Typescript's Nested Type Assignments

Simply put, I'm making an API call and receiving the following data: { getUserInfo: { country: 'DE', email: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3c48594f487c59445d514c5059125f5351">[e ...

Struggling to uncheck all selected boxes using jQuery

I've attempted various methods, but I'm unable to create code that will uncheck or clear all checkboxes once they have all been selected. Here is the latest version of my code... $(".select_all").click(function(){ var state = ($(this).html( ...

What are the methods for altering the material of a glTF model using THREE.js?

I've created a model using Blender and baked all the lighting and textures. However, when I import it into THREE.js in .glb format, it automatically uses the standard material. While this may work in certain cases, my concern is that I want to retain ...

Tips for ensuring that a server waits until a database connection is successfully established using node.js, MongoDB, and Express

I have a server.js file and a database.js file In the server.js file, I have the following code: const express = require('express'); var db = require('./database'); const app = express(); . . some get/post methods app.listen(3000); . ...

Tips for updating or replacing items in a JavaScript array

Explaining what I need, here's the code snippet: Everything works smoothly when adding a product to the items array if it does not already exist (based on id). However, if it does exist, I only want to update the item. The distinction is based on the ...

Passing dynamically loaded parameters to a function during dropdown value selection in Angular 2

Could you please review if I am passing the parameters correctly in the li tag's click function of the updateId method? The console errors only point to that line. This is my app.component.html code: <div class='form-group' style="width ...

What are the steps to integrate and utilize DefinePlugin within your webpack configuration?

Hello, I'm currently trying to implement the DefinePlugin in order to update the version number and ensure that my JavaScript refreshes after a new build is released. Unfortunately, I am encountering issues with getting DefinePlugin to work properly. ...

The action is not being added to the HTML when the click event is triggered

I'm developing a GIF generator, with the aim of generating clickable buttons that will dynamically add 10 gifs based on the search term to the page. Although the click event is triggering the console log, it's not adding divs with gif images and ...

Altering the color of an Ionic Navbar by clicking on an Ionic tab

I'm working with Ionic directives generated by Ionic Creator. The layout includes a standard nav-bar at the top and tabbed navigation icons at the bottom, each with its own color. My goal is to update the navbar's color when an icon is clicked. ...