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

Unable to retrieve element using getElementById with dynamically assigned id

After researching on Google and browsing through various Stack Overflow questions (such as this & this), I have not been able to find a solution. I am currently working on validating dynamically generated rows in a table. The loop and alert functions a ...

Preventing ngclass animation in Angular 1.2 from running when re-compiled

Example: http://codepen.io/anon/pen/ixEdu I am currently working on an angular animation using ngClass. In the scenario where the element containing the animation is re-compiled, the animation triggers again. Is there a way to prevent this unwanted behavi ...

Is the div empty? Maybe jQuery knows the answer

I currently have a <div id="slideshow"> element on my website. This div is fully populated in the index.php file, but empty in all other pages (since it's a Joomla module). When the div is full, everything works fine. However, when it's emp ...

Create an unordered list using the <ul> tag

Creating a ul as input is my goal, similar to this example on jsfiddle: http://jsfiddle.net/hailwood/u8zj5/ (However, I want to avoid using the tagit plugin and implement it myself) I envision allowing users to enter text in the input field and upon hitt ...

Maintain the functionality of an object even when it is created using the "bind" method

I'm working with a function that has the following structure: var tempFun = function() { return 'something'; } tempFun.priority = 100; My goal is to store this function in an array and bind another object to it simultaneously, like so ...

Strategies for efficiently retrieving delayed ajax data to display in a Rails view

When working with an API action that takes around 10 seconds to retrieve data, I typically use an alert to ensure the data is successfully fetched. However, my main concern is how to effectively transmit this data to a Rails view for displaying purposes. B ...

Could you display the picture prior to the function commencing?

This is the image that needs to be loaded before the loop begins. <div id="attendenceGridDivLoader" style="display:none"> <img src="<?php echo base_url() . 'images/loader.gif'; ?>" /> </div> <select onchange=checkAll ...

designing a presentation with customizable durations for each slide

I am trying to implement a slideshow using the slides.js jquery plugin and I have a specific requirement for the duration of each slide. My goal is to have the first slide, which contains an embedded video, display for 7 seconds, while the subsequent slid ...

Rails javascript not triggering even after the page is loaded with `page:load`

My experience with using Stripe in Rails has been smooth, except for some issues with Ajax calls not working properly after page refresh. I suspect this might be related to Turbolinks as the 'ready page:load' event doesn't behave as expected ...

Utilizing Unix timestamps for x-values while displaying dates as x-labels in an ECharts line chart

I'm struggling with incorporating date-converted unix timestamps as values in my ECharts graph. The x-axis of my chart is meant to display the recording time for each buy or sell price, represented by respective lines. Everything functions properly wh ...

`Using angular-ui-tree: Retrieving the html element of the most recent node that was added`

Perhaps this question may seem trivial, but despite my efforts to research on stackoverflow and angular-ui-tree documentation, I have not been able to find a solution. I am in need of obtaining the HTML element of the most recently added node. To be spec ...

What is the process for retrieving the AJAX response text?

When it comes to my AJAX development, I rely on prototype and utilize the following code: somefunction: function(){ var result = ""; myAjax = new Ajax.Request(postUrl, { method: 'post', postBody: postData, content ...

What is the process for retrieving data on the server side using node.js that has been sent from the frontend via the post method

After diving into learning node.js with the express framework, I encountered a roadblock. I'm experimenting with a basic query search webapp and trying to send some data (a boolean named all) from front-end plain javascript via ajax to the server sid ...

Is the default behavior of Ctrl + C affected by catching SIGINT in NodeJS?

When I run my nodejs application on Windows, it displays ^C and goes back to the cmd prompt when I press Ctrl + C. However, I have included a SIGINT handler in my code as shown below: process.on('SIGINT', (code) => { console.log("Process term ...

Tips for making Ajax crawlable with jQuery

I want to create a crawlable AJAX feature using jQuery. I previously used jQuery Ajax on my website for searching, but nothing was indexed. Here is my new approach: <a href="www.example.com/page1" id="linkA">page 1</a> I display the results ...

Scrolling jqgrid to focus on the current day column and implementing a blinking effect on cells with stored data

I have a jqgrid with 38 columns of data, where the first 6 columns are frozen and the rest are unfrozen. Depending on the combo box selection, it shows either dates or months (see sample image here). After loading the grid, I want it to automatically scrol ...

When using NodeJS and MongoDB together, a POST request may receive a 404 error code

Despite having all the routes set up correctly, I'm encountering a 404 error when trying to send a POST request. I've searched through numerous similar questions but haven't come across a solution that addresses my specific issue. Below is ...

I experienced an issue with Firestore where updating just one data field in a document caused all the other data to be wiped out and replaced with empty Strings

When updating data in my Firestore document, I find myself inputting each individual piece of data. If I try to edit the tag number, it ends up overwriting the contract number with an empty string, and vice versa. This issue seems to stem from the way th ...

Issue with Semantic-UI Special PopUp not displaying on screen

I'm currently working on creating a unique pop-up feature using custom HTML, with the intention of adding content to it later on. My console is displaying the following message: Popup: No visible position could be found for the popup. $(document) ...

Is it possible for a method within a class to retrieve properties from a different object within the same class?

I'm facing an issue with accessing the necessary object properties within a method. In my scenario, I have a Game class that generates a new game object. Once the object is created, I attempt to execute the draw method. This draw method requires infor ...