Tips for rearranging the order of x-editable events?


I'm currently developing a web application using angularJs and xeditables. Within this application, I have multiple xeditable text elements following each other, with validation triggering upon pressing enter or clicking outside the text field:

<div ng-repeat="answer in item.answers track by $index">    
    <input type="radio" name="{{item.label.text}}">&nbsp;<span editable-text="answer.text" onshow="onShow()" onhide="onClose()" buttons="no" blur="submit" onbeforesave="validateEditableText($data)">{{answer.text || "Edit this text"}}</span>
</div>


The functions onShow() and onClose() are defined as follows:

$scope.onShow = function() {
  alert('onShow');
  if($scope.hideMenu == true)
    $scope.hideMenu = false;
};

$scope.onClose = function() {
      alert('onClose');
      if($scope.hideMenu == false)
        $scope.hideMenu = true;
    };


These functions simply toggle a boolean value. This boolean is utilized to prevent event propagation (which may seem unusual, but is necessary for my requirements).
Below is my function for blocking event propagation, although understanding it fully is not essential for this explanation:

$scope.blocEventPropagation = function(event) {
  if($scope.selectedItem != null){
    if($scope.hideMenu && !$scope.selectedItem.dropDownOpen)
      event.stopPropagation();
    }
};

My current issue arises when I click on one xeditable text field and then directly click on another. The events do not occur in the sequence I desire. The current order is: onShow() for the first xeditable text when clicked, onShow() for the second text when clicked directly after without closing the first, and finally onClose() for the first text. My ideal sequence would be: onShow() for the first text when clicked, onClose() for the first text when clicking on a second text, and onShow() for the second text.

I have extensively reviewed the xeditable documentation but have not found a solution. I attempted using a timeout to delay the event, but this was unsuccessful.

Any ideas on how to alter the event order, limit function calls, or suggest an alternative solution would be greatly appreciated.

Answer №1

To ensure the menu remains hidden, each element must contribute independently:

  <div ng-repeat="answer in item.answers track by $index">    
        <input type="radio" name="{{item.label.text}}">&nbsp;<span editable-text="answer.text" onshow="{{$scope.hideMenu[$index] = true;}}" onhide="{{$scope.hideMenu[$index] = false;}}" buttons="no" blur="submit" onbeforesave="validateEditableText($data)">{{answer.text || "Edit this text"}}</span>
  </div>

To collectively hide the menu only when an element is displayed:

<div id="menu" ng-hide="{{$scope.hideMenu.indexOf(true) >= 0}}" />

There might be some syntax errors, but the intention is to make it clear.

Answer №2

After much searching, I finally stumbled upon a solution :

$scope.count = 0;

$scope.onShow = function() {
  $scope.count ++;
  if($scope.hideMenu == true)
    $scope.hideMenu = false;
};

$scope.onClose = function() {
  $scope.count --;
  if($scope.hideMenu == false && $scope.count == 0)
    $scope.hideMenu = true;
};

Admittedly, this solution may not be the most elegant, but given my current level of knowledge, I believe it is the best option for this specific scenario. I have attempted to find a more efficient solution, but have been unsuccessful.
It is worth noting that the previous answer works well if we are using onShow() and onClose() within a ng-repeat with the $index. However, in my case, I need to use these functions in different locations within my application, making it impossible to utilize an index.

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

Displaying a Bootstrap button and an input box next to each other

Looking for advice on aligning or arranging a button and input box side by side in Bootstrap without grouping. I've searched online for examples, but they all involve grouping of elements. jsfiddle: https://jsfiddle.net/erama035/p9dagmL3/3/ <div ...

Saving the AJAX response object in a global variable - Important fields are not being retrieved

Currently, I am developing an asynchronous webpage in Grails wherein I invoke a controller and display the response as a D3.js network. To ensure further usability, I saved the object as a global variable. Despite the successful execution of the function u ...

How can union types be used correctly in a generic functional component when type 'U' is not assignable to type 'T'?

I've been researching this issue online and have found a few similar cases, but the concept of Generic convolution is causing confusion in each example. I have tried various solutions, with the most promising one being using Omit which I thought would ...

Using jQuery's ajax function to send data with a variable name of data field

I am trying to figure out how to dynamically add a variable to the name of the data field I want to send information to through ajax. Below is an example of the code I'm working on: var qty = $('#qty_'+value).val(); $.ajax({ url: &apo ...

Occasionally, there are instances when node.js combined with express.js and hogan.js may result in a blank page being

Most of the time, every feature in this app functions properly. However, there are instances when a blank page is displayed instead of the homepage (or any other page). Interestingly, making a minor adjustment to the affected view, such as adding a space o ...

What could be the reason for TypeScript being unable to recognize my function?

In my code, I have a Listener set up within an onInit method: google.maps.event.addListener(this.map, 'click', function(event) { console.log(event.latLng); var lt = event.latLng.lat; var ln = event.latLng.lng; co ...

Tips for blocking submissions when a user tries to input a hyperlink

I have encountered a problem in my journey of learning JS and unfortunately, I couldn't resolve it by myself. Lately, spam has been flooding through the form on my website and all my attempts with jQuery and JS to fix it have failed. As a last resort ...

Creating dynamic tooltips with AngularJS

UPDATE: It appears that there may be an issue with the scope not updating when the email field is invalid. Is there a way to change that behavior? This is my first time posting on stackoverflow so I hope I'm doing it correctly. I am new to AngularJS ...

I am experiencing some difficulty with the GetServerDate using the JSON protocol in JQuery; it's

I am facing an issue while trying to execute a basic ajax call to fetch data from a file on a server. Even though I can access the file via the web browser and have diligently followed tutorials related to this topic, I have hit a dead end. Here is the sn ...

Updating ES6 syntax for superset in array: a step-by-step guide

I am currently working with ES6 React code that generates an array of MiniIcons on a webpage. const MiniIcons = ({miniicons}) => ( <div id="application"> {miniicons.map(miniicon => ( <MiniIcon key={miniicon.id} id={miniicon.id} ...

TestingCompilerFactory is not available as a provider

Currently troubleshooting my test file to identify the issue that is hindering a successful test run: import { async, ComponentFixture, TestBed } from '@angular/core/testing'; import { Component, Directive, Input, OnInit } from '@angula ...

In the Swiper function of JavaScript within a Django template, the occurrence of duplicate elements (products) is being generated

Experimenting with displaying products in a Django template, I decided to utilize the Swiper js class. This allowed me to showcase the products within a div, complete with navigation buttons for scrolling horizontally. However, when testing this setup wit ...

Utilizing PHP to create an interactive website

As a novice PHP developer, I took to Google in search of tutorials on creating dynamic PHP websites. What I found was that many tutorials used the $_GET variable to manipulate URLs and make them appear like this: example.com/?page=home example.com/?page= ...

Leveraging external Javascript files in Ionic framework version 3 and above

Hey there, I'm currently working on integrating the Mapwize SDK, an external Javascript library, with the latest version of Ionic. I've encountered the common challenge of getting Javascript to function smoothly with Typescript. Although I'm ...

What steps can I take to stop Google Maps from resetting after a geocode search?

As a beginner working with the Google Maps Javascript API v.3, I have written some code to initialize a map, perform an address lookup using the geocoder, re-center the map based on the obtained lat long coordinates, and place a marker. However, I am facin ...

What is the best way to showcase a user's input in a webpage using vanilla javascript

Struggling with creating functionalities for a simple calculator using vanilla.js. Able to display numbers on click but facing issues with multiple clicks and deletion of values. Trying to use addeventlistener but encountering a Type Error "addeventliste ...

Disabling form with customized component in AngularJS

I’ve been trying to find a solution, but I’m stuck on this. I created a custom directive called manyToOneSelect that fetches items from the server, displays them to the user, and allows the user to select one item. It’s working fine, but I can’t fi ...

Iterate over the array and show the elements only when a click event occurs

I am trying to create a loop through an array (array) and display the elements one by one only after clicking a button (bt). However, when I run this code, it only shows the last element of the array (i.e. honda). Can someone please help me fix this issu ...

The code begins executing even before the function has finished its execution

I believe that is what's happening, but I'm not entirely sure. There's code in a .js file that invokes a function from another .js file. This function performs an ajax call to a php page which generates and returns a list of radio buttons wi ...

Custom AngularJS directive for ensuring the selection of a required HTML element

Today brings another question as I continue working on my web application. I've encountered an issue with the 'required' attribute not being widely supported in major browsers. The attribute only works if the first option value is empty, whi ...