如何使用log = [{id:“Logs List Details1”},{id:“Logs List Details2”} ... etc}]以angular

时间:2017-04-17 12:59:15

标签: angular typescript

How to declare variable with  log=[{id: "Logs List Details1"},{id: "Logs List Details2"},....etc}] as dynamically. If i am giving statically like this and push my dynamic data to it, its taking after page limit over. But if i pass only one static value for log object and appending my data to it, its taking only one value. NO pagination happening in this condition. means my dynamic data is not appending here directly.Here My page limit per page is 3.

********here is my html code************

    <div class=" list-servers">
      <div class="col-md-12">
        <span>Default Log</span>
        <span class="flt-right"><a href="#"><img src="images/plus.png">==</a>
        </span>
      </div>
    </div>

    <div class="container">
      <div class="row" id="row-main">
      <div class="row">

       </div>
       <div class="col-md-9" id="content">

         <div class="content-area">
           <ng2-smart-table *ngIf="log" [settings]="settings" [source]="log" 
                  [defaultSettings]="defaultSettings" >
           </ng2-smart-table>
         </div>
        </div>
      </div>
    </div>

**************And here is my .ts file***************
***********I am using Ng2-smart-table to display my log details.***********

    import { Component, OnInit, OnChanges } from '@angular/core';
    import any = jasmine.any;
    import {GetDefaultLogService} from '../get-default-log.service';
    import {DefaultLogItem} from '../default-log-item';
    import { OauthTokenService } from '../oauth-token.service';


    @Component({
     templateUrl: './get-default-log.component.html',
     styleUrls: ['./get-default-log.component.css']
    })
    export class GetDefaultLogComponent implements OnInit{

     defaultLogItemValue: string;
     servername = sessionStorage.getItem("servername");
     logType = sessionStorage.getItem("logType");
     hidediv: boolean = true;

     settings = {
     columns: {
      id: {
        title: 'Logs List Details'
      }
     }
     };

    protected defaultSettings: Object  = {
      mode: 'inline', // inline|external|click-to-edit
      hideHeader: false,
      hideSubHeader: true,
      attr: {
        id: '',
      },
      noDataMessage: 'No data found',
      columns: {},
      pager: {
        display: true,
        perPage: 3
      }
    };

      defaultLogItems: DefaultLogItem[];

      // log:Array<any>=[{id: ""}];
      //log:any=[];
      //I want json in this format dynamically
      log=[
       {id: "Logs List Details1"},
       {id: "Logs List Details2"},
       {id: "Logs List Details3"},
       {id: "Logs List Details4"},
       {id: "Logs List Details5"}
      ];


  constructor(private service: GetDefaultLogService, private oauthTokenService: 
    OauthTokenService) {
        this.service.getLogItemList(this.oauthTokenService.getHeaders(), 
        sessionStorage.getItem("server"), 12345, 
        sessionStorage.getItem("startDate"), sessionStorage.getItem("endDate"), 
        sessionStorage.getItem("logType")).subscribe(lst =>{
          this.defaultLogItems = lst;
          for(var v in lst[0].logList) {
            this.log.push({id: lst[0].logList[v]});
          }

        });
    }

      ngOnInit() {
        console.log("this.defaultLogItems"+this.log);
      }
    }

********所以请用这个建议。我正在分页,但我不想要静态数据。我想要页面限制每页= 10。

如何使用log = [{id:“Logs List Details1”,id:“Logs List Details2”.... etc}]动态声明变量。如果我像这样静态地给出并将我的动态数据推送到它,那么它会超过页面限制。但是,如果我只为日志对象传递一个静态值并将我的数据附加到它,则它只获取一个值。在这种情况下没有发生分页。表示我的动态数据没有直接附加在这里

1 个答案:

答案 0 :(得分:0)

表示您想以下列格式创建数组:

log=[ 
    {id: "Logs List Details1"}, 
    {id: "Logs List Details2"}, 
    {id: "Logs List Details3"}, 
    {id: "Logs List Details4"}, 
    {id: "Logs List Details5"}

];

试试这个

var log= [];
for (let i = 1; i < 100; i++) {
  let lg= {
   id:i
};
log.push(lg);
}