2016-04-25 63 views
0

我打電話給一些API來填充陣列。最終的輸出是一樣的東西下面Laravel Blade - 顯示陣列內容

Array 
(
    [0] => Array 
    (
     [leadData] => Array 
     (
      [LeadID] => 1232806 
      [DateIdentified] => 21/04/2016 
      [Client] => Prospect 1 
      [LeadName] => Test 
      [Owner] => Some Owner 
      [Value] => 2160.00 
      [Status] => 70% 
     ) 
     [clientData] => Array 
     (
      [BusinessStructure] => 
      [IsProspect] => No 
     ) 
     [quoteData] => Array 
     (
      [QuoteID] => Q0020 
      [ProjectName] => Test 
      [Amount] => 1800.00 
      [AmountTax] => 360.00 
      [AmountIncludingTax] => 2160.00 
      [EstimatedCost] => 450.00 
      [EstimatedCostTax] => 90.00 
      [EstimatedCostIncludingTax] => 540.00 
     ) 
     [customData] => Array 
     (
      [0] => Array 
      (
       [Lead Type] => New 
      ) 
      [1] => Array 
      (
       [Month] => June 
      ) 
     ) 
    ) 
    [1] => Array 
    (
     [leadData] => Array 
     (
      [LeadID] => 1230279 
      [DateIdentified] => 19/04/2016 
      [Client] => Bank1 
      [LeadName] => test 3 
      [Owner] => Some Owner 
      [Value] => 36000.00 
      [Status] => 50% 
     ) 
     [clientData] => Array 
     (
      [BusinessStructure] => 
      [IsProspect] => No 
     ) 
     [quoteData] => Array 
     (
      [QuoteID] => Q0016 
      [ProjectName] => test 3 
      [Amount] => 30000.00 
      [AmountTax] => 6000.00 
      [AmountIncludingTax] => 36000.00 
      [EstimatedCost] => 0.00 
      [EstimatedCostTax] => 0.00 
      [EstimatedCostIncludingTax] => 0.00 
     ) 
    ) 
) 

有一點要注意的是,有時信息不是全部是存在的,只是因爲它是不可用的。所以你可以看到數組0有一些customData,而數組1沒有。其他元素甚至可能沒有quoteData部分等。

所以我的刀片模板現在有這個信息。我基本上想顯示所有的數據,但如果它不可用,只顯示一個空單元格。所以,我有以下標題

<table class="table table-bordered table-hover additionalMargin alignment"> 
    <tr class="col-md-12 noPadding"> 
     <thead> 
     <tr> 
      <th>Lead ID</th> 
      <th>Client Name</th> 
      <th>Is Prospect</th> 
      <th>Business Structure</th> 
      <th>Quote ID</th> 
      <th>Project Name</th> 
      <th>Amount</th> 
      <th>Amount Tax</th> 
      <th>Amount inc Tax</th> 
      <th>Cost</th> 
      <th>Cost Tax</th> 
      <th>Cost inc Tax</th> 
      <th>Type</th> 
      <th>Month</th> 
     </tr> 
     </thead> 
    </tr> 
    <tbody> 

    </tbody> 
</table> 

然而,TBODY內,我在做什麼,似乎很凌亂,我似乎在正確的地方,以獲得數據的唯一方法是,如果我如檢查一切

@if(is_array($leadArray)) 
     @foreach($leadArray as $array) 
      <tr> 
       <td> 
        @if(!empty($array['leadData'])) 
         {{ $array['leadData']['LeadID'] }} 
        @endif 
       </td> 
       <td> 
        @if(!empty($array['leadData'])) 
         {{ $array['leadData']['Client'] }} 
        @endif 
       </td> 
       <td> 
        @if(!empty($array['clientData'])) 
         {{ $array['clientData']['IsProspect'] }} 
        @endif 
       </td> 
       <td> 
        @if(!empty($array['clientData'])) 
         {{ $array['clientData']['BusinessStructure'] }} 
        @endif 
       </td> 
       <td> 
        @if(!empty($array['quoteData'])) 
         {{ $array['quoteData']['QuoteID'] }} 
        @endif 
       </td> 
       <td> 
        @if(!empty($array['quoteData'])) 
         {{ $array['quoteData']['ProjectName'] }} 
        @endif 
       </td> 
       <td> 
        @if(!empty($array['quoteData'])) 
         {{ $array['quoteData']['Amount'] }} 
        @endif 
       </td> 
       <td> 
        @if(!empty($array['quoteData'])) 
         {{ $array['quoteData']['AmountTax'] }} 
        @endif 
       </td> 
       <td> 
        @if(!empty($array['quoteData'])) 
         {{ $array['quoteData']['AmountIncludingTax'] }} 
        @endif 
       </td> 
       <td> 
        @if(!empty($array['quoteData'])) 
         {{ $array['quoteData']['EstimatedCost'] }} 
        @endif 
       </td> 
       <td> 
        @if(!empty($array['quoteData'])) 
         {{ $array['quoteData']['EstimatedCostTax'] }} 
        @endif 
       </td> 
       <td> 
        @if(!empty($array['quoteData'])) 
         {{ $array['quoteData']['EstimatedCostIncludingTax'] }} 
        @endif 
       </td> 
       @if(!empty($array['customData'])) 
        @foreach($array['customData'] as $data) 
          <td> 
           @if(!empty($data['Lead Type'])) 
            {{ $data['Lead Type'] }} 
           @endif 
          </td> 
          <td> 
           @if(!empty($data['Month'])) 
            {{ $data['Month'] }} 
           @endif 
          </td> 
        @endforeach 
        @else 
         <td></td> 
         <td></td> 
       @endif 
      </tr> 
     @endforeach 
    @endif 

有沒有更好的方法來做到這一點?或者這是我唯一的選擇?

感謝

+0

你可以做'{{@ $陣列[「leadData」] ['客戶端']}}'在數據丟失時抑制錯誤。或者以一種用'null'填充任何缺失數據的方式預處理數組。 – ceejayoz

+0

@ceejayoz,有趣。它記錄在任何地方嗎?如果不是,它究竟是如何工作的? –

+2

@AlexeyMezenin'@'是PHP的錯誤抑制運算符。有關詳細信息,請參閱http://php.net/manual/en/language.operators.errorcontrol.php。它應該很少使用,但你的看起來像一個潛在的適當的用例。 – ceejayoz

回答

3

刀片有一個功能,您可以使用or表示「回聲這一點,如果它存在,或者這個如果不」。所以你可以做

<td>{{ $array['leadData']['LeadID'] or '' }}</td> 

而這基本上導致你想要什麼。更乾淨,不是? :)

文檔:https://laravel.com/docs/5.2/blade#displaying-data - 幾段落。

+1

我忘了這個。尼斯。 – ceejayoz

+0

這實際上很有趣 - 它似乎也檢查是否它的空,這意味着我不需要這樣做 –

+0

@Joel Hinz,它也很有趣,絕對是upvote。在文檔中他們說它正在檢查'isset()'。但它檢查「is_null()」和「空」嗎? –

1

你也可以通過遍歷數組的列名做到這一點更優雅和更短的(使用or ''喬爾決定):

{{ $dataColumns['leadData'] = ['LeadID', 'Client'] }} 
{{ $dataColumns['clientData'] = ['IsProspect', 'BusinessStructure'] }} 
{{ #dataColumns['quoteData'] = ['QuoteID', 'ProjectName', 'Amount', 'AmountTax', 'AmountIncludingTax', 'EstimatedCost', 'EstimatedCostTax', 'EstimatedCostIncludingTax'] }} 

@if(is_array($leadArray)) 
    @foreach($leadArray as $array) 
     <tr> 
      @foreach($dataColumns as $dataColumn) 
       @foreach($dataColumn as $column) 
        <td>{{ $array[$dataColumn][$column] or '' }}</td> 
       @endforeach 
      @endforeach 
      @if(!empty($array['customData'])) 
       @foreach($array['customData'] as $data) 
        <td>{{ $data['Lead Type'] or ''}}</td> 
        <td>{{ $data['Month'] or '' }}</td> 
       @endforeach 
      @else 
        <td></td> 
        <td></td> 
      @endif 
     </tr> 
    @endforeach 
@endif 
+0

謝謝,我給了這個嘗試,它表示,HTML eminities期望參數1是一個字符串,數組給出 –

+0

並在什麼行?另外,嘗試使用'{! !!}''而不是'{{}}' –

+0

很難說,因爲它在helpers.php行中說ErrorException錯誤469: –