Breaking down several components to reveal or conceal using Angular

In developing a semi-custom DataGrid that utilizes a table with fields populated via a Service call, one key feature is to allow users to modify entries (in this case, Documents) by selecting an action to "Replace" the file and its File Type. The current markup structure for this functionality is as follows:

<form action="#" method="post" ng-model="OtherDocumentsForm" style="margin-top: 15px;">
            <h3 style="margin: 0;">Additional Documents</h3>

            <table>
                  <tr>
                    <th>File Type</th>
                    <th>File Name</th>
                    <th>Date</th>
                </tr>

                <tr ng-repeat="record in DocumentsOther track by $index" id="row-{{record.file_url}}">
                    <td>
                        <span data-document-type="{{record.file_type}}" data-document-id='{{record.document_id}}'>{{record.file_type}}</span>
                        <select ng-model="newDocumentType" id="replace-file-doctype-{{record.document_id}}" placeholder="Choose file Type" ng-show="$scope.replaceFile" data-document-id="{{record.document_id}}" class="replaceComboBox"> <!-- Note replaceFile -->
                            <option label="" value="" selected="selected"></option>
                            <option label="PDF" value="pdf">PDF</option>
                            <option label="Image" value="image">Image</option>
                        </select>
                    </td>
                    <td>
                        <div ng-if="record">
                            <a href="{{record.file_url}}">{{record.document_name}}</a>
                            <!-- Replace Action-->
                            <button class="replaceAssociatedRecord" ng-click="ClearAssociatedFile($event, this, '{{record.file_url}}', '{{record.document_id}}');" data-document-type="{{record.file_type_raw}}">Replace</button>
                            <input type="file" ng-model="replaceFileField" id="replace-file-{{record.document_id}}" data-document-id="{{record.document_id}}" data-document-type="{{record.file_type_raw}}" onclick="checkIfAssocFileDefined(this);" ng-show="replaceFile" class="replaceFileField" /> <!-- Note replaceFile -->
                        </div>
                    </td>
                    <td>
                        {{record.date_created | dateConsistent}}
                    </td>
                </tr>
            </table>

            <button ng-click="addUncategorizedFile($event)" style="margin: 5px auto 15px; display: block;">Upload additional file</button>
        </form>

An issue arises from the use of $scope.replaceFile being applied universally. When triggered by $scope.ClearAssociatedFile() (see code snippet below), it sets the value to true for all fields, causing all to be displayed. I am looking for a better way to redesign these components so that the behavior of replaceFile (linked to ng-show and uninitialized within the parent controller) can be isolated and manipulated separately once ClearAssociatedFile is initiated. What would be the most effective approach to achieve this?

$scope.ClearAssociatedFile = function(event, elem, parentClassName, inheritDocumentId) {
    event.preventDefault();
    event.stopPropagation();

    //TODO reconfigure to use event
    //document.getElementById("row-"+parentClassName).children[0].children[0].innerHTML = "";
    event.currentTarget.parentElement.parentElement.parentElement.children[0].children[0].innerHTML = "";
    //event.currentTarget.parentElement.parentElement.parentElement.children[0].children[1].style.display = "inline-block"

    //document.getElementById("row-"+parentClassName).children[1].children[0].children[0].innerHTML = "";
    event.currentTarget.parentElement.parentElement.parentElement.children[1].children[0].children[0].innerHTML = "";

    //document.getElementById("row-"+parentClassName).children[1].children[0].children[1].style.display = "none";
    event.currentTarget.parentElement.parentElement.parentElement.children[1].children[0].children[1].style.display = "none";
    //event.currentTarget.parentElement.parentElement.parentElement.children[1].children[0].children[2].style.display = "inline-block";

    //Restore default File Type field value.
    document.getElementById("replace-file-doctype-"+inheritDocumentId).value = event.target.attributes[2].value;

    //Show existing fields
    var replaceFileKey = "replaceFileItem"+$filter('convert_dashes')(inheritDocumentId);
    $scope[replaceFileKey] = true;
};

Answer №1

To begin, create a new variable in your controller called

$scope.revisedFileKeys = {};

Substitute the final two lines within the ClearAssociatedFile function with

var updatedFileKey = inheritDocumentId;
$scope.revisedFileKeys[updatedFileKey] = true;

Revise the ng-show attribute to read

ng-show="$scope.revisedFileKeys[record.document_id]"

After implementing the above adjustments, only the selected item will be hidden upon interaction. Feel free to ask if you require further clarification on this modification.

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

How to utilize AngularJS to submit a Symfony2 form

I'm currently working on developing an application with Symfony2 for the backend and considering using AngularJS for the frontend. My plan is to integrate Symfony forms into the project as well. I have successfully set up the form with all the necessa ...

How can we use Selenium to inject and execute Javascript on a page before loading or running any other scripts on the page?

Currently, I am utilizing the Selenium Python WebDriver to navigate various web pages. My objective is to inject a JavaScript code into these pages before any other JavaScript codes are loaded and executed. However, it is crucial that my JS code is the fir ...

The conditional expression in AngularJS is failing to execute

Having trouble with my login page that uses AngularJS to evaluate the username and password. Even when I input correct values, the conditional statement in the controller seems to return false. Below is a snippet of my HTML file: <div class="row"> ...

Discovering the list of database names and table names in sqliteHere is how you can

I am in the process of developing a SQLite command editor for an Android application using Cordova. Within the app, users will have the ability to create unlimited tables and databases. When they enter the application, they must select a database from a ...

Dealing with an empty request.FILES using FileUploadParser in Django Rest Framework and Angular file upload technique

Uploading files in an angular view can sometimes be tricky, especially when using templates. The code snippet below shows how to upload multiple and single files using Angular File Upload library. Multiple <input type="file" name="file" nv-file-select= ...

What is the proper way to designate a manifest.json link tag on a limited-access website controlled by Apache shibboleth?

The issue arises when attempting to access the manifest.json file. It has been declared as follows: <link href="manifest.json" rel="manifest"/> Is it possible to declare the manifest tag inline, or what would be the most effective way to declare it ...

Show information from a JSON document within an Ionic application

I am facing an issue with retrieving the output of an Ionic project that fetches data from a JSON file. Despite checking for syntax errors multiple times, I am unable to identify the error. I have also attempted to use Ionic commands, but the result remain ...

A clearly defined function is being mistakenly labeled as undefined

I am currently working on developing a user-friendly link-sharing web application using NodeJS and AngularJS. However, I have encountered an issue where a function is being identified as undefined. Take a look at the specific code snippet related to this ...

Querying data from a promise and embedding it in a JSON object in AngularJS

Attempting to retrieve data from a promise within a JSON object for the first time has presented me with a challenging task. The typical approach looks something like this: Service JS app.factory("dataService", ["$http", function ($http) { fu ...

Experiencing problem with route and post request in my angular.js/express/node.js application

Below is the code for my Angular controller with a post request: $http.post('/formDevis', $scope.formData).then(function(response) { if (response.data) { $('#successModal').modal(); console.log(response); } }, function(resp ...

How can I utilize the repeat directive with various step conditions?

I am trying to organize an array of objects by displaying two objects per row. Here is what I have so far: <div class="row" ng-repeat="twoObject in objects> <div class="col-sm-6">{{twoObject(1).name}}</div> <div class="col-sm- ...

AngularJS item

I am a beginner in AngularJS and need help with printing object attributes in AngularJS controller. @RequestMapping(value = "/rest/getById", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) @RolesAllow ...

Decoding various JSON arrays received through AJAX requests

After returning two objects as JSON through AJAX, I am facing an issue with accessing the values in these two lists. Previously, when I had only one list, I could parse it easily. data = serialize("json", vm_obj) data2 = serialize("json", user_networks_li ...

Form containing a pair of buttons

I am attempting to design multiple forms with two buttons, each of which will submit the form to a different script. One button will use ajax for submission, while the other will simply submit the form without ajax. <?php foreach($objects as $object) : ...

Initiate upon successful completion of Ajax call

Is there a way to only trigger a function upon successful Ajax call? This is my Ajax function: $.ajax({ type: "POST", url: url, dataType: "json", success: function(msg) { console.dir(msg); if(ms ...

How to sort data in AngularJS by clicking on a column to change the order from ascending to

The code view below is what I am currently working with: <tr ng-repeat="c in clients | orderBy:'code'"> <td>{{c.firstname}} {{c.lastname}}</td> <td>{{c.telephone}}</td> <td>{{c.location}}</td> ...

Localhost is fine, but Github pages are just not cooperating with Bootstrap

I am currently in the process of building a portfolio website using Bootstrap 4, animate.css, typed.js and my own custom css. When testing the site on localhost through VS Code, everything appears to be functioning correctly. However, upon viewing the sit ...

Exploring Scope Inheritance in AngularJS

Within the parent controller scope, I have initialized selectedItem as 'x'. Subsequently, in the child scope, selectedItem is declared using ngModel: <div ng-app> <div ng-controller="CtrlA"> <div ng-controller="CtrlB"> ...

The JavaScript creates a select box, but the value does not get sent when the form is submitted

When a user selects a value from the first select box, a second select box is dynamically created using JavaScript. This JS triggers a PHP file to query a MYSQL database for relevant items based on the initial selection. The issue I am facing is that the ...

Is it possible to create an <h1> heading that can be clicked like a button and actually do something when clicked?

I am currently designing a web page that includes <div class="cane-wrap"> <h1 class="mc">christmas tic tac toe</h1> </div> https://i.sstatic.net/R3Yps.png located at the top center of the webpage, with ...