簡體   English   中英

同位素中動態元素的排序

[英]Sorting dynamic elements in isotope

我正在使用jQuery的$ .each循環和在Google Sheets(Tabletop.js)中創建的JSON文件構建艦船列表,並嘗試使用Isotope.js對它們進行排序。

jQuery創建列表沒有任何問題。 但是,當我單擊按鈕以通過同位素對元素進行排序時,什么也沒有發生。

當我將元素硬編碼到HTML頁面中時,Isotope可以對所有內容進行排序。

我在互聯網上四處張望,找不到答案。

帶有回叫到另一個函數的循環,該函數從電子表格中獲取我的數據。 #port是HTML的div,一切正常。

這是我正在使用的代碼。

function showInfo(data, tabletop) {

    var shipData = tabletop.sheets('data').all();

    var items=[];

    $.each(shipData, function(i) {

        //Put each ship in the port ^__-
        var ship = "<div class=\"ship\"><div class=\"row\"><p class=\"name font-lg\">" + shipData[i].name + "</p><p class=\"cruise-line font-xs\">" + shipData[i].line +"</p><div class=\"ship-photo col-md-9 col-sm-9\"><img src=\"img/" + shipData[i].img + "\" class=\"img-responsive\"></div><div class=\"ship-info col-md-3 col-sm-3\"><div class=\"row\"><div class=\"header font-xs col-sm-4 col-xs-4\">Launched</div><div class=\"header font-xs col-sm-4 col-xs-4\">Length</div><div class=\"header font-xs col-sm-4 col-xs-4\">Tonnage</div></div><div class=\"row\"><div class=\"year font-lg col-sm-4 col-xs-4\">" + shipData[i].launchYear +"</div><div class=\"length font-lg col-sm-4 col-xs-4\">" + shipData[i].length + "'</div><div class=\"weight font-lg col-sm-4 col-xs-4\">" + shipData[i].tonnage + "</div></div></div><div class=\"ship-info col-md-3 col-sm-3\"><div class=\"row\"><div class=\"header font-xs col-sm-6 col-xs-6\">Max capacity</div><div class=\"header font-xs col-sm-6 col-xs-6\">Crew</div></div><div class=\"row\"><div class=\"max-passenger font-lg col-sm-6 col-xs-6\">" + shipData[i].maxCapacity + "</div><div class=\"crew font-lg col-sm-6 col-xs-6\">" + shipData[i].crewCapacity + "</div></div></div></div>";

            items.push(ship);

      });

    $port.append(items)
}

這是我的同位素分類代碼:

// init Isotope
    $('#port').isotope({
        itemSelector: ".ship",
        layoutMode: 'vertical',
        getSortData: {
            name: '.ship'
        }
    })

// bind sort button click
$('#sorts').on( 'click', 'button', function() {
     var $this = $(this);
     var sortValue = $this.attr('data-sort-value');

     $('#port').isotope({ 
            sortBy: sortValue 
     });

  });

這是我的HTML:

<div class="compare">
    <h3>How the ships compare</h3>
    <div id="sorts" class="button-group">
                <button class="button" data-sort-value="name">name</button>
                <button class="button" data-sort-value="length">length</button>
                <button class="button" data-sort-value="weight">weight</button>
                <button class="button" data-sort-value="passengers">passenger</button>
                <button class="button" data-sort-value="original-order">clear</button>      
            </div>
        </div>

<div id="port"></div>

JSFiddle演示 一個列表是用JS生成的,而另一個列表是用HTML生成的。

要解決的兩個問題。

將#sort更改為此:

<button id="sort"  data-sort-value="name">Click to sort</button>

和您的數據功能:

$.each(data, function (i) {
$('.port').append("<li><div class='name'>" + data[i] + "</div></li>");
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM