2017-06-01 32 views
0

上下文:表中有一組月份(例如,5月,6月,7月),在這些月份之下將是給定月份的所有讀數。具有動態值的可摺疊數據行

代碼:

<tbody data-bind="visible: !MeterReadingHistory_IsBusy(), foreach: HeaderLines()" style="display: none"> 
         <tr data-toggle="collapse" data-target=".order1"> 
          <td> 
           <!-- ko if: meterReadings.length > 0--> 
           <span class="glyphicon glyphicon-chevron-down"></span> 
           <span class="reading-history-data" data-bind="html: monthName"></span> 
           <!-- /ko --> 
           <!-- ko if: meterReadings.length == 0--> 
           <span class="reading-history-data" data-bind="html: monthName"></span> 
           <!-- /ko --> 
          </td> 
          <td> 
           <span class="reading-history-data" data-bind="html: latestReadingDate"></span> 
          </td> 
          <td> 
           <span class="reading-history-data" data-bind="html: latestReadingType"></span> 
          </td> 
          <td> 
           <span class="reading-history-data" data-bind="html: latestReadingElectric"></span> 
          </td> 
          <td> 
           <span class="reading-history-data" data-bind="html: latestReadingGas"></span> 
          </td> 
         </tr> 


         <!-- ko if: meterReadings.length > 0 --> 
         <tr class="collapse order1" > 
          <td colspan="5"> 

           <table class="table mb-none desktop-only"> 
            <thead> 
             <tr> 
              <th>Day</th> 
              <th>Reading Source</th> 
              <th>Electricity</th> 
              <th>Gas</th> 
             </tr> 
            </thead> 
            <tbody> 
             <!-- ko foreach: meterReadings --> 
             <tr> 
              <td data-bind="text: readingDateParsed"></td> 
              <td data-bind="html: readingType"></td> 
              <!-- ko foreach: readings --> 
              <td data-bind="html: reading"></td> 
              <!-- /ko --> 
              <!-- ko if: readings.length == 0 --> 
              <td></td> 
              <td></td> 
              <!-- /ko --> 
              <!-- ko if: readings.length == 1 --> 
              <td></td> 
              <!-- /ko --> 
             </tr> 
             <!-- /ko --> 
            </tbody> 
           </table> 
          </td> 
         </tr> 
         <!-- /ko --> 
        </tbody> 

的問題:當我在任何行的點擊,它擴展每個月行公開所有數據的時候,其實我只是想顯示的子行的已被點擊的實際月份。

主要的問題是這個表是動態的,我們不知道將會生成多少個標題行,因此通過data-bind屬性很難將每行分配給特定的數據源。

所以......我怎麼才能讓這段代碼只顯示我點擊過的那一行數據,例如5月,並顯示這個給定月份的所有讀數,同時所有其他標題行保持關閉狀態?

謝謝!

+0

您可以使用'$ index()'在循環中創建「唯一」目標。 (由'foreach'呈現的每個元素都有自己的索引。)但是:knockout並不真正喜歡修改DOM的其他代碼(例如引導框架)。嘗試谷歌「自定義引導綁定」,以確保東西繼續工作。 – user3297291

回答

0
$([data-toggle="collapse"]).on('click',function(){ 
    var thisCollapse = $(this).attr('data-target'); 
    $(thisCollapse).slideToggle('slow'); 
}) 
+0

似乎沒有做任何事情:( – Jacooobley

+0

雖然這段代碼可能回答這個問題,但提供關於如何和/或爲何解決問題的額外上下文會提高答案的長期價值。 – Badacadabra