简体   繁体   English

将列表项数据从一页转移到另一页

[英]Transfer list item data from one page to another

I am using ionic 2 and am trying to transfer data from one page to another. 我正在使用ionic 2,并试图将数据从一页转移到另一页。 More specifically one list item on the first page(quickSearch) to another page(quickSearchDetail). 更具体地说,第一页上的一个列表项(quickSearch)到另一页上的列表项(quickSearchDetail)。 I have a picture below to demonstrate this. 我下面有一张图片来演示这一点。

When I click on a list it should transfer that data to the next page, however I am having an issue where only the first list item is being transferred irrespective of which item I click (over writing my local storage data). 当我单击一个列表时,它应该将该数据传输到下一页,但是,我遇到一个问题,即无论单击哪个项目,都仅传输第一个列表项(覆盖本地存储数据)。

在此处输入图片说明

quickSearch Template list item quickSearch模板列表项

<ion-item *ngFor="let item of items" (click)="gotoQuickSearchDetail(item._id)">
  <p id="clusterNameFormValue">{{ item.clusterName }}</p>
  <p id="currentBUNameFormValue">{{ item.currentBUName }}</p>
  <p id="technologyFormValue">{{ item.technology }}</p>
  <p id="customerFormValue">{{ item.Customer }}</p>
  <p id="clusterHeadNumberFormValue">{{ item.clusterHeadNumber }}</p>
</ion-item>

quickSearch.ts quickSearch.ts

gotoQuickSearchDetail(){
  var clusterName: any = clusterName = document.getElementById("clusterNameFormValue").innerHTML;
  var currentBUName: any = currentBUName = document.getElementById("currentBUNameFormValue").innerHTML;
  var technology: any = technology = document.getElementById("technologyFormValue").innerHTML;
  var customer: any = customer = document.getElementById("customerFormValue").innerHTML;
  var clusterHeadNumber: any = clusterHeadNumber = document.getElementById("clusterHeadNumberFormValue").innerHTML;

  localStorage.setItem('clustername', clusterName);
  localStorage.setItem('currentBUName', currentBUName);
  localStorage.setItem('technology', technology);
  localStorage.setItem('customer', customer);
  localStorage.setItem('clusterHeadNumber', clusterHeadNumber);

  this.navCtrl.setRoot(QuickSearchDetail);
}

quickSearchDetail item quickSearchDetail项目

<ion-list>
    <ion-item>
      <p id="clusternameDetail"></p>
      <p id="currentBUNameDetail"></p>
      <p id="technologyDetail"></p>
      <p id="customerDetail"></p>
      <p id="clusterHeadNumberDetail"></p>
   </ion-item>
</ion-list>

quickSearchDetail.ts quickSearchDetail.ts

ionViewDidLoad() {
    document.getElementById('clusternameDetail').innerHTML = localStorage.getItem('clustername');
    document.getElementById('currentBUNameDetail').innerHTML = localStorage.getItem('currentBUName');
    document.getElementById('technologyDetail').innerHTML = localStorage.getItem('technology');
    document.getElementById('customerDetail').innerHTML = localStorage.getItem('customer');
    document.getElementById('clusterHeadNumberDetail').innerHTML = localStorage.getItem('clusterHeadNumber');
}

This is too much to handle :D but, where you are going wrong is using getElementById , and id.. and.. stuff. :D太难处理了,但是,您出问题的地方是使用getElementById和id .. and ..东西。 an ID is unique. ID是唯一的。 And with the *ngFor you are creating more elements with the same ID, this is not allowed. 使用*ngFor可以创建更多具有相同ID的元素,这是不允许的。 But the browser copes, and gives back the first element it finds with that ID, which explains the behaviour you are seeing. 但是浏览器可以应付并返回使用该ID找到的第一个元素,该元素解释了您所看到的行为。

Another point is the weird usage of localStorage . 另一点是localStorage的怪异用法。 This does not seem like a use-case for what you are trying to do. 这似乎不是您要尝试做的用例。 I suggest you look at the angular documentation, and specifically at inter component communication. 我建议您查看角度文档,尤其是组件间的通信。

In short, you probably should create a service, which holds the array list. 简而言之,您可能应该创建一个包含数组列表的服务。 Then create a navigation parameter on your detail page which will hold the id number to an item in the array list of the service. 然后在您的详细信息页面上创建一个导航参数,该参数会将ID号保存在服务数组列表中的某个项目上。 On that page you can obtain this id and access the service to get the right data from the items. 在该页面上,您可以获取此ID并访问服务以从项目中获取正确的数据。

I could write it all for you, but this is not the right site for that, which will bring me back to my previous mentioned suggestion. 我可以为您编写所有内容,但这不是正确的网站,这会让我回到之前提到的建议。 Look at angular documentation, to see how you should create a master-detail structure, because getElementById , innerHtml and localStorage is not the way to go 查看有角度的文档,以了解如何创建主从结构,因为getElementByIdinnerHtmllocalStorage并非innerHtml之路

Try to use this 尝试使用这个

//quickSearch Template list item 
<ion-item *ngFor="let item of items" (click)="gotoQuickSearchDetail(item)">
  <p id="clusterNameFormValue">{{ item.clusterName }}</p>
  <p id="currentBUNameFormValue">{{ item.currentBUName }}</p>
  <p id="technologyFormValue">{{ item.technology }}</p>
  <p id="customerFormValue">{{ item.Customer }}</p>
  <p id="clusterHeadNumberFormValue">{{ item.clusterHeadNumber }}</p>
</ion-item>


//quickSearch.ts
gotoQuickSearchDetail(item: any){
  var clusterName: any = clusterName = item.currentBUName;
  var currentBUName: any = currentBUName = item.currentBUName;
  var technology: any = technology = item.technology;
  var customer: any = customer = item.Customer;
  var clusterHeadNumber: any = clusterHeadNumber = item.clusterHeadNumber;

  localStorage.setItem('clustername', clusterName);
  localStorage.setItem('currentBUName', currentBUName);
  localStorage.setItem('technology', technology);
  localStorage.setItem('customer', customer);
  localStorage.setItem('clusterHeadNumber', clusterHeadNumber);

  this.navCtrl.setRoot(QuickSearchDetail);

You have loop for items. 您有项目循环。 In that case each items contain same id. 在这种情况下,每个项目都包含相同的ID。 For this reason when you try to find a value by element id it always gives you 1st item. 因此,当您尝试按元素ID查找值时,它始终会为您提供第一项。

You just need to use navParams . 您只需要使用navParams You have a very seriously creative solution but the answer is very simple. 您有一个非常认真的创意解决方案,但答案很简单。 For my answer I am going to remove all the ID 's , add them back if you need it for styles. 对于我的答案,我将删除所有ID ,如果样式需要将其添加回去。 Also one thing to note, try not to approach angular with a jquery mindset, because they are 2 different animals. 还要注意的一件事是,尽量不要用jquery的心态来接近angular,因为它们是2种不同的动物。

quickSearch Template list item quickSearch模板列表项

 <ion-item *ngFor="let item of items">
   <p>{{ item.clusterName }}</p>
   <p>{{ item.currentBUName }}</p>
   <p>{{ item.technology }}</p>
   <p>{{ item.Customer }}</p>
   <p>{{ item.clusterHeadNumber }}</p>
</ion-item>

So there is your list wrapper in a *ngFor . 因此,在*ngFor有您的列表包装器。 Your assignment of item is all you need. 您只需分配item That item holds all of the data that you want on the next page. item包含您在下一页上想要的所有数据。 It is your "package" ( if you will ). 这是您的“包裹”(如果愿意)。 Now all you want to do is pass the "package" to the next page. 现在,您要做的就是将“包”传递到下一页。 And we want the passing to start when it is clicked. 而且,我们希望单击时开始传递。

Therefore we pass it on the (click) event. 因此,我们将其传递给(click)事件。

(click)="gotoQuickSearchDetail(item)"

So now we just need to add the method in our Component file, and then with the power of routing in ionic 2 we just send it to the next page. 因此,现在我们只需要在Component文件中添加该方法,然后利用ionic 2中的路由功能,就可以将其发送到下一页。

gotoQuickSearchDetail(item){
    this.navCtrl.push(QuickSearchDetail, item);  //  Notice the 2nd parameter
}

The 2nd parameter passes the item to the page declared in the 1st parameter. 第二个参数将项目传递到第一个参数中声明的页面。 So now you are going to the page QuickSearchDetail and you are passing your "package" item along for the ride. 因此,现在您将转到QuickSearchDetail页面,并将“包裹” item传递给您。

Now when you get to the next page you need to tell that page .. "Hey i have "package" called item and it has some data . 现在,当您进入下一页时,您需要告诉该页面。“嘿,我有一个名为item “包装”,它有一些data

So in your QuickSearchDetail Component you can add this. 因此,您可以在QuickSearchDetail组件中添加它。

import { NavController, NavParams } from 'ionic-angular';

@Component({
  selector: '...',
  templateUrl: '...html'
})
export class QuickSearchDetailPage {

    itemDetails: any;
    constructor(public navCtrl: NavController, public navParams: NavParams){
        this.itemDetails = this.navParams.data;

        // All the data is now held in the itemDetails variable.
        console.log(this.itemDetails);
    }
}

You now have assigned the "package" holding the clicked item 's data to the variable itemDetails which was "passed" (by default ) to the navParams.data . 现在,您已将保存单击item的数据的“程序包”分配给变量itemDetails ,该变量已“默认”传递给navParams.data You can now use the itemDetails in your view. 现在,您可以在视图中使用itemDetails

 {{ itemDetails.PROPERTYHERE }}

BONUS: 奖金:

You can get this stuff for free when you use your terminal to generate a page. 使用终端生成页面时,您可以免费获得这些东西。

>_ ionic generate page QuickSearchDetail

Also the navCtrl has a third property which has options for animation. navCtrl还具有第三个属性,该属性具有动画选项。 Which is fun. 真有趣。

this.navCtrl.push(QuickSearchDetail, item, { 
             animate: true, 
             direction: 'forward', 
             mode: 'ios'});

Read more about the navCtrl here 此处阅读有关navCtrl的更多信息
And read more about the navParams here 这里阅读更多有关navParams的信息

Add all data to your Click function and in .ts 将所有数据添加到您的Click函数和.ts中

  gotoQuickSearchDetail(data:any,datab:any.........){

   navpushhere(SearchPage,{firstPassed:data,secondpassed:datab}); }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM