Identifying the hashKey and selected option in a dropdown menu

My attempt to set the selected option for the select menu is not working because the data in the ng-model that I am sending has a different $$hashKey compared to the data in the select menu, and the $$hashKey holds the values.

<select class="form-control" ng-model="selManga" ng-options="manga.seri for manga in mangalar"> 
<option value="">Select Manga</option> 
</select>
<select ng-change="selPage = 0" ng-model="selChapter" ng-options="selManga.randomword.indexOf(chapter) as chapter.klasor for chapter in selManga.randomword"> 
<option value="">Chapter</option> 
</select>
<select ng-model="selPage" ng-options="selManga.randomword[selChapter].yol.indexOf(page) as selManga.randomword[selChapter].yol.indexOf(page) + 1 for page in selManga.randomword[selChapter].yol">
</select>

After researching, people suggest using track by to solve this issue, but I have to use as. Is there another way to work around it?

The selected value for the first select menu works fine, but the second one does not. Here is the Plunker example: http://plnkr.co/edit/3V8JSF2AU01ZZNPfLECd?p=info

.controller('nbgCtrl',function  ($scope, MMG, $stateParams) {
var milo = $stateParams.serix;
var musti = $stateParams.klasor;
MMG.adlar.success(function(loHemen) {
    var i, miloMangaInArray;
    for (i=0; i<loHemen.length; i++) {
        if (loHemen[i].seri===milo) {
            miloMangaInArray = loHemen[i];
            break;
        }
    };
    var a;
    for (a=0; a<miloMangaInArray.randomword.length; a++) {
        if(miloMangaInArray.randomword[a].klasor===musti) {
            break;

        }
    }
$scope.mangalar = loHemen; //JSON Data
$scope.selManga = $scope.mangalar[i]; // The ng-model for the first select menu is working.
$scope.selChapter = $scope.mangalar[i].randomword[a]; // The ng-model for the second select menu is not working due to mismatch with JSON data.
});

$scope.next = function (manga, chapter, page) {
    var nextPage = page + 1;
    if (angular.isDefined(manga.randomword[chapter].yol[nextPage])) {
        $scope.selPage = nextPage;
    } else if (angular.isDefined(manga.randomword[chapter + 1])) {
        $scope.selChapter = chapter + 1;
        $scope.selPage = 0;
    }};

})

https://i.stack.imgur.com/YvvkV.png

Answer №1

Hey there, check out this awesome js fiddle I put together for the solution http://jsfiddle.net/abc123xyz/5/

In this scenario, I utilized the indexOf method to retrieve the index of the page in the array for the last select element only.

However, please note that this may not be the most efficient solution as it will need to apply indexOf every time the digest loop runs. There are several alternative solutions that could be considered:

1- Extracting the id of the page from the name of the image itself

2- Mapping the pages array to a list of objects with the following format:

[{"index":1,"img":"00.jpg"},{"index":2,"img":"01.jpg"},{"index":3,"img":"02.jpg"}]

You can achieve the second option with the following code snippet:

pages.map(function(d,i){return {"index":i,"img":d};});

coder123

Answer №2

I strongly recommend adopting the AngularJS approach to managing models and bindings. Rather than manually tracking various indexes in your view code, utilize the power of ng-select to automatically assign references to sections of your model using ng-model. By making some minor adjustments to both the HTML and controller, you can streamline your code and achieve successful results.

To begin, establish a shared $scope.model = {…} object within the $scope. Then update the HTML as follows:

<select ng-model="model.selManga" ng-options="manga.seri for manga in mangalar">
  <option value="">Select Manga</option>
</select>

<select ng-model="model.selChapter" ng-options="chapter.klasor for chapter in model.selManga.randomword" ng-change="model.selPage = model.selChapter.yol[0]">
  <option value="">Chapter</option>
</select>

<select ng-model="model.selPage" ng-options="page as model.selChapter.yol.indexOf(page) + 1 for page in model.selChapter.yol">
</select>
<img class="picture" ng-src="http://baskimerkeziankara.com/{{model.selPage}}" ng-click="next(model.selPage)">

Next, adjust the controller accordingly:

.controller('nbgCtrl', function($scope, MMG, $stateParams) {
    var model = {
      selManga: undefined,
      selChapter: undefined,
      selPage: undefined
    };
    $scope.model = model;

    MMG.adlar.success(function _init(loHemen) {
      for (var i = 0; i < loHemen.length; i++) {
        if (loHemen[i].seri === $stateParams.serix) {
          model.selManga = loHemen[i];
          break;
        }
      }

      for (var a = 0; a < model.selManga.randomword.length; a++) {
        if (model.selManga.randomword[a].klasor === $stateParams.klasor) {
          model.selChapter = model.selManga.randomword[a];
          break;
        }
      }

      model.selPage = model.selChapter.yol[0];

      $scope.mangalar = loHemen;
    });

    $scope.next = function _next(page) {
      var pageIndex = model.selChapter.yol.indexOf(page);

      if (angular.isDefined(model.selChapter.yol[pageIndex + 1])) {
        model.selPage = model.selChapter.yol[pageIndex + 1];
      } else {
        var chapterIndex = model.selManga.randomword.indexOf(model.selChapter);

        if (angular.isDefined(model.selManga.randomword[chapterIndex])) {
          pageIndex = 0;

          model.selChapter = model.selManga.randomword[chapterIndex + 1];
          model.selPage = model.selChapter.yol[pageIndex];
        }
      }
      console.log('manga', model.selManga.seri, 
                  'chapter', model.selChapter.klasor,
                  'selPage', pageIndex + 1);
    };
  })

An example demonstrating this implementation is available on Plunker at: http://plnkr.co/edit/2aqCUAFUwwXuGQHpuooj

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

A guide on incorporating an event to multiple elements in vue.js

I am trying to implement a double-click event on multiple elements in Vue3. My goal is to create a table of contents data and add a double-click event to the checkboxes and favorite icons without using @dblclick="". I find it more convenient to assign a c ...

Live search bar feature with jQuery更新

I am currently working on creating a dynamic search bar that updates a list of items from the database based on the input value. Below is the code I have developed for this search bar: $(document).ready(function(){ $('#search').keyup(function ...

What could be causing my AngularJs routing and animation to bypass the second redirection?

Recently started working with Angular and experimenting with routing and animations to manage my page transitions. I followed a helpful guide that helped me set everything up. I encountered a few issues: When trying to link back to the landing page (home ...

Within the materia-ui table, I am facing an issue where clicking the button to expand a row results in all rows expanding. I am seeking a solution to ensure only the selected row expands

When a row is clicked, all rows in the data table expand to show inner data for each row. The issue is that clicking the expand button expands all rows rather than just the selected row. Each time I try to expand one specific row, it ends up expanding mul ...

Having trouble executing the typescript build task: Command 'C:Program' is not valid as an internal or external command

I'm currently working on converting typescript code to JavaScript and have been following the steps outlined in the documentation. To automate the compilation of .ts files, I set up a watch task triggered by pressing Ctrl+Shift+B. However, upon runni ...

In Protractor and Jasmine, the expected pattern should be an array of objects

I have a scenario where the expected data in an array of objects needs to be updated frequently due to changes in the system under test. Is there a way to set expectations on a pattern for this type of object array rather than relying on specific values? ...

Leveraging the power of HTML5 alongside Angularjs and Node/Express within the MEAN.js boilerplate framework

I've decided to kickstart my app using the mean.js () boilerplate because of its built-in authentication/authorization features. However, I've hit a roadblock when it comes to incorporating HTML5 into my project. In my Angular app, I've en ...

What are some javascript libraries that can be used to develop a mobile image gallery for both Android and iPhone

I currently have the touch gallery system in place, but unfortunately it isn't functioning properly on Android devices. ...

Encountered an Angular SSR error stating "ReferenceError: Swiper is not defined"

When attempting to implement SSR (Server-Side Rendering) in a new project, everything runs smoothly and without issue. However, encountering an error arises when trying to integrate SSR into an existing project. ...

Tips for effectively implementing a custom theme alongside a component library that utilizes Material UI without the need to apply it to every single component

What is the correct way to utilize a custom theme with a component lib that utilizes Material UI without wrapping every single component? import { createMuiTheme } from '@material-ui/core/styles'; const theme = createMuiTheme({ palette: { ...

Tracking user sessions using cookies, not relying on JavaScript like Google does

Currently, I am working in an environment that utilizes PHP on the server side and Javascript on the client side. In order to monitor user sessions, I regularly send a JS PUT request to the server every 5 seconds. This allows me to gather data such as the ...

Implementing a JQuery modal with backend coding

I have encountered a problem in my ASP.NET code-behind where I am trying to incorporate a modal popup. Despite my efforts, I have not been able to successfully implement it. Do you have any suggestions on how I should proceed with this process? <scrip ...

Navigate to a specified div using JavaScript

I'm having an issue with my navigation bar not scrolling to the designated div. Despite looking at other examples, I can't seem to find a solution <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"> ...

Looking to retrieve the value of a selected checkbox within a horizontally laid out HTML table

Trying to extract values from a table with a horizontal header when checkboxes are selected. The goal is to retrieve the values of the selected column for all rows. Code snippet provided below. <script src="https://ajax.googleapis.com/ajax/libs/jquer ...

Retrieve relevant information from JSON upon scanning the barcode

Dealing with a barcode scanner app specifically designed for warehouse use. Upon scanning an item, the UPC number is successfully displayed. However, I now require the display of additional data associated with that UPC, which needs to be retrieved from a ...

Struggling to understand the javascript snippet "requiring the passport file and passing in passport as a parameter."

I am still learning the ropes of javascript and currently working on a basic login restful api using the passport middleware. I understand that when I use require('xxxxx'); I am importing a module for use. While researching online, I came across ...

I am experiencing difficulty accessing Laravel and AngularJS on my local host from another computer within my network

I currently have a project running smoothly on my main machine using the command php -S localhost:8080 -t public to start a local server. Everything is functioning perfectly in this setup. However, I am now attempting to access the project from another c ...

Searching with regex to find keys in an object structured like JSON

Here is a similar object in JSON-like format: { id: "123", name: "John Doe", age: 30, city: "Seattle", email: "johndoe@example.com", phone: "123-456-7890", address: "123 Main St", zipcode: "98101", country: "USA" } I am looking to identify all the keys i ...

Updating all images in a JQuery thumbnail gallery

I've been experimenting with jQuery and fancy box to create a special effect on my website. I wanted to display a large image with thumbnails below it, where clicking on a thumbnail would update the main image (similar to the RACE Twelve image example ...

Using breeze.EntityQuery.from('Agencies') will give you the final entity that was returned in the overall results

Greetings, I am new to breeze and apologize in advance if my issue turns out to be a rookie mistake on my part. I am currently working with Angular, specifically John Papa's hot towel base. I have created a repository that retrieves a list of agencie ...