Is there a way I can retrieve a set of data from a JSON file by simply clicking on the next or

I am currently working on building a Hybrid mobile app using Angular and Ionic frameworks. Below is the sample data that I have obtained:

$scope.data = [{
        "PID": 108,
        "Name": "Demo",
        "TID": 20,
        "date": "2016/00/29"
     }, {
        "PID": 98,
        "Name": "Sports Demo1",
        "TID": 20,
        "date": "2016/06/02"
     }, {
        "PID": 98,
        "Name": "Sports Demo2",
        "TID": 20,
        "date": "2016/06/02"
     }, {
        "PID": 98,
        "Name": "Sports Demo3",
        "TID": 20,
        "date": "2016/06/02"
     }, {
        "PID": 98,
        "Name": "Sports Demo4",
        "TID": 20,
        "date": "2016/06/02"
     }]

Code for the first page HTML :

<ion-content class="" padding="true">   
        <ul ng-repeat="dataSch in data">
            <li class="item" style="border-width: 0px;">
                <div class="item" style="border-width: 0px; padding : 1px;">{{dataSch.Name}}</div>  
                <div class="item" style="border-width: 0px; padding : 1px;">{{dataSch.date}}</div> 
            </li>  
        </ul> 
</ion-content>

Code for the second page HTML :

<ion-content class="" padding="true">  
        <div>Detail 1</div>  
        <div>Detail 2</div>  
</ion-content>
<ion-footer-bar align-title="left" class="bar-assertive">  
    <button ng-click="next()">Next</button> 
    <button ng-click="previous()">Previous</button>  
</ion-footer-bar>

In the first page, by clicking on an "li" element based on "PID", I can navigate to the second page with all the details. However, on the second page, there are options in the footer to move to next or previous data from there itself. But how can I display the next or previous data on the second page?

For example, if I clicked on the second element on the first page and got the data Name as "Sports Demo1" on the second page, clicking on Next button should show "Sports Demo2" and clicking on Previous should show "Demo".

Answer №1

HTML : 
<a ng-click="showDetails(only_selected_data);">View Details</a>
<div>
   <h4 >{{info.Name}}</h4>
   <h4 >{{info.Name1}}</h4>
   <h4 >{{info.Date}}</h4> 
</div>
<ion-footer-bar class="bar-assertive" >
   <button ng-disabled="disable_btn1" ng-click="previousElement();"> Previous </button>
   <button ng-disabled="disable_btn2" ng-click="nextElement();"> Next</button> 
</ion-footer-bar> 



JS
    $scope.showDetails = function(data){  
            var fullLength = $scope.DataArray.length - 1;  
            $scope.data = data; 
            var index= $scope.DataArray.indexOf(data); 
            $scope.index = index; 
            if(fullLength == $scope.index){ 
                $scope.disable_btn2 = true;
            }else if($scope.index == 0){ 
                $scope.disable_btn1 = true;
            }else{
                $scope.disable_btn1 = false;
                $scope.disable_btn2 = false;
            }
            var info = $scope.DataArray[$scope.index];
            $scope.info = info; 
        }

    $scope.showDetails1 = function(arrayindex){  
            var fullLength = $scope.DataArray.length - 1; 
            if(fullLength == arrayindex){ 
                $scope.disable_btn2 = true;
            }else if(arrayindex == 0){ 
                $scope.disable_btn1 = true;
            } 
            $scope.data = $scope.data; 
            var info = $scope.DataArray[arrayindex];  
            $scope.info = info;
        }

    $scope.previousElement = function(){ 
                $scope.showDetails1($scope.index - 1);
                $scope.index--; 
        }
        $scope.nextElement = function(){ 
                $scope.showDetails1($scope.index + 1);
                $scope.index++; 
        } 

Answer №2

When you reach the second page, make sure to use the next/previous function with the PID of the next/previous element in order to navigate properly:

<ion-footer-bar align-title="left" class="bar-assertive">  
    <button ng-click="next(97)">Next</button> 
    <button ng-click="previous(99)">Previous</button>  
</ion-footer-bar>

In these next/previous functions, remember to include the redirecting logic to take users to the new page.

PS: Instead of having separate next and previous functions, consider using just one function (goToPage(PID)).

Answer №3

To keep track of the current page, you can create an index property in the $scope object. Implementing next and prev functions will involve adjusting this index accordingly:

$scope.gotoPage = function(index) {
   $scope.index = index;
   ...
};
$scope.next = function() {
   $scope.gotoPage($scope.index+1);
};
$scope.prev = function() {
   $scope.gotoPage($scope.index-1);
};

If page1 and page2 share the same controller, the above code should suffice. However, if they use different controllers, you may need to employ a service for seamless functionality.

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

Is there a glitch in jQuery that causes other items in the UI to disappear when deleting a single item from local storage, even though they

After clicking delete on a record, it is successfully deleted from Localstorage. However, upon page reload, none of the "drop downs" show the other records in Localstorage even though they still exist. How can I address this issue? Seeking guidance on res ...

Having trouble getting Vue.js to cooperate with a brand new Laravel 5.8 setup?

I set up a fresh Laravel project using laravel new sample. Then I executed npm install followed by npm run dev. Next, I modified the welcome.blade.php file as shown below: <!DOCTYPE html> <html> <head> <script type="text/j ...

An error occurred with the datepicker: Unable to connect to 'bsValue' as it is not recognized as a property of 'input'

Despite importing DatepickerModule.forRoot() in my Angular unit test, I am encountering the following error: Error: Template parse errors: Can't bind to 'bsConfig' since it isn't a known property of 'input'. (" ...

What is the reason for the square brackets in my json data?

My current project involves exploring the usage of JSON in conjunction with jQuery and MVC2. My goal is to generate JSON data for an AJAX post request to my controller. I have created an array using the following function: function getArguments() { var ar ...

How can you modify the default empty table message in a datatable using DTOptions?

I've come across several questions similar to this lately. Here is an example, but all the answers seem to involve using jquery. I'm trying to find a way to achieve this using DTOptions instead. This is how my code looks like at the moment : ...

What is the best way to delete and add elements in a looped array using Javascript

i have a form similar to this one. https://i.sstatic.net/V7ivb.png how can I replace this form each time the country changes, by removing it and then appending it again from a looping array. If there is only one set of data, display an "Add Form" Button; ...

Avoiding non-router links from remaining active while using routerLinkActive in Angular

One component in the list item of the navigation bar caught my attention: <div [routerLink]="link" routerLinkActive="bg-blue-100" class="flex w-[80%] mx-auto p-3 rounded-md font-bold text-xl justify-between items-center gr ...

Show flexibility in the grandchildren of a row layout

My goal is to achieve a layout similar to the image below using the flex "system", but I am facing a challenge with some generated directives that are inserted between my layout div and the flex containers: https://i.sstatic.net/nq4Ve.png <div id="i-c ...

Converting a JSON file to a .tde file with the help of the Tableau Extract API in Java

Being a beginner in Tableau and Java, I am faced with the task of converting a JSON file fetched from Hive into a Tableau extract for input on Tableau Server. After researching online, I found the Tableau Extract API as an option to convert various formats ...

Utilize jQuery to showcase elements in a dropdown menu

Hey everyone, I'm working on an ASP.NET MVC4 project and I'm using a jQuery script on the edit page. However, I am encountering an issue with displaying elements on the page. Here is the initial HTML markup of my dropdown before any changes: & ...

Creating an Ionic v4 alert box that redirects users to different pages

I am facing an issue with my ion-alert component where I have set up a message with two options, "To Myself" and "To Someone", that should act like buttons and route to different pages in the application. However, using (click) events or [routerLink] on th ...

Creating nested routes in react-router is a useful technique for managing complex applications. By

I have a parent container with nested routes: function Home() { render ( <Router> <Switch> <Route exact path="/website" component={Website} /> <Route path="/dashboard" compo ...

Switch the background color alternately from red to green every second

Need help with a webpage that changes the background color every second using JavaScript. The issue lies in figuring out how to correctly change the variable within the function. Here's an example of the code: <!DOCTYPE html> <html> ...

Uncovering the characteristics of a GeoJSON data layer within Google Maps V3

Is there a way to access the properties of the data layer itself when loading a geoJSON file into a Google Map? I understand how to access the individual properties like posts_here, but I'm interested in obtaining the properties for the layer as a wh ...

Ways to verify if an item is enabled

To ensure a button in my angular application is enabled, I am using Protractor for testing. Here is the test script: it('submit should not be enabled',function() { var price = by.name('price'), oldCategory = by.name ...

``Is it possible to adjust the style of an HTML element based on the style of another element?

Within my web application, I am dynamically changing the style of an HTML element using JavaScript. Below is the code snippet: function layout() { if(document.getElementById('sh1').getAttribute('src') == "abc.png") { docum ...

Using Axios to retrieve data from a MySQL database is a common practice in web development. By integrating Vue

I have developed another Vue.js admin page specifically for "writer" where I can display post data fetched from a MySQL database. The admin page called "admin" is functioning properly and responding with all the necessary data. The following code snippet ...

What is the process for including the posting date in the JSON format while using the Instagram

Struggling to incorporate the date into my JSON Instagram feed has been a challenge for me. For reference, here is a demonstration on JSFiddle: http://jsfiddle.net/galnova/p74jy3sk/ The following code snippet includes the commented-out section related to ...

What is the best way to simulate global variables that are declared in a separate file?

dataConfiguration.js var userData = { URIs: { APIURI: "C" }, EncryptedToken: "D" }; configSetup.js config.set({ basePath: '', files:['dataConfiguration.js' ], ..... UserComponentDetails: .....some i ...

Having difficulties grasping the concept of how to communicate effectively with my MySQL database

Apologies in advance for asking a question that may have been answered elsewhere, but I've been struggling for hours to transfer the information into my own program. Despite my attempts, I always encounter the same obstacles. So, I decided it would be ...