【问题标题】:Isotope: Dynamic JSON loaded from Google Spreadsheet同位素:从 Google 电子表格加载的动态 JSON
【发布时间】:2014-12-21 06:50:20
【问题描述】:

我想做的是加载并使用来自 Google 电子表格 JSON 的数据来填充同位素列表,但是一旦我添加了 JSON,动画和排序功能就会停止运行(我已经验证了用于加载它的 JSON/Javascript有效,但我完全不知道如何解决这个问题。在研究中,我确实找到了A CODE,它允许同位素从 JSON 文件加载元素并且仍然起作用,但是我仍在通过 Javascript 和我不知道如何将我的 Google 电子表格 JSON 集成到该解决方案中。我知道这可能很简单,但任何帮助我的人都会深表感谢。谢谢!

编辑:这是带有我的代码的JSFIDDLE

    function importFST(json) {
    for (i = 0; i < json.feed.entry.length; i++) {
        entry = json.feed.entry[i];
        $('.isotope').append('<div class="element-item' + + entry.gsx$alpha.$t + '"><a href="http://' + entry.gsx$url.$t + '.domain.com/" title="' + entry.gsx$skill.$t + '">' + entry.gsx$name.$t + '</a> | "' + entry.gsx$living.$t + '"' + ' | <em>' + entry.gsx$mutation.$t + '</em></div>');
    }
}

$( function() {
  // init Isotope
  var $container = $('.isotope').isotope({
    itemSelector: '.element-item',
    layoutMode: 'fitRows',
    getSortData: {
      name: '.name',
      alliance: '.alliance',
      number: '.number parseInt',
      category: '[data-category]'
    }
  });

  // filter functions
  var filterFns = {
    // show if name ends with -a
    a: function() {
      var name = $(this).find('.name').text();
      return name.match( /a$/ );
    }
  };

  // bind filter button click
  $('#filters').on( 'click', 'button', function() {
    var filterValue = $( this ).attr('data-filter');
    // use filterFn if matches value
    filterValue = filterFns[ filterValue ] || filterValue;
    $container.isotope({ filter: filterValue });
  });

  // bind sort button click
  $('#sorts').on( 'click', 'button', function() {
    var sortByValue = $(this).attr('data-sort-by');
    $container.isotope({ sortBy: sortByValue });
  });

  // change is-checked class on buttons
  $('.button-group').each( function( i, buttonGroup ) {
    var $buttonGroup = $( buttonGroup );
    $buttonGroup.on( 'click', 'button', function() {
      $buttonGroup.find('.is-checked').removeClass('is-checked');
      $( this ).addClass('is-checked');
    });
  });

});

【问题讨论】:

  • Isotope 看起来确实很有趣 :-) 我从未使用过,但我查看了文档 (isotope.metafizzy.co/methods.html),我会尝试在循环的 JSON 响应中附加 Isotype 样式的元素: $container.isotope() .append( elem ) .isotope( '附加', elem ) .isotope('layout');而不是使用 container.appendChild(element);风格。好吧,就像我说的我从未使用过同位素,但我会试试这个。无论如何祝你好运:-)
  • 发布的代码来自您链接到的同位素示例,而不是您的代码。你的代码是什么?
  • @Macsupport 这是一个 JSFiddle:jsfiddle.net/changmin/eq32fo5r

标签: javascript jquery json jquery-isotope


【解决方案1】:

您的问题是 'element-item' 和 entry.gsx$alpha.$t 之间缺少空格。这导致您的同位素项目具有 element-itemaaa、element-itembbb 等类。您需要添加一个空格以确保 itemSelector: '.element-item'

这是一个有效的jsFiddle

这是我的代码(与你的稍有不同,附加了使用同位素):

  function importFST(json) {
   // init  
  for (i = 0; i < json.feed.entry.length; i++) {
    entry = json.feed.entry[i];
    var $newElems = $('<div class="element-item' + ' ' + entry.gsx$alpha.$t + '"><a href="http://' + entry.gsx$url.$t + '.domain.com/" title="' + entry.gsx$skill.$t + '">' + entry.gsx$name.$t + '</a> | ' + entry.gsx$living.$t + ' | <em>' + entry.gsx$mutation.$t + '</em></div>');

        var $container = $('.isotope').isotope({
    itemSelector: '.element-item',
    layout: 'fitRows',
    getSortData: {
        name: '.name',
        alliance: '.alliance',
        number: '.number parseInt',
        category: '[data-category]'
    }
});
// filter functions
var filterFns = {
    // show if name ends with -a
    a: function () {
        var name = $(this).find('.name').text();
        return name.match(/a$/);
    }
};

// bind filter button click
$('#filters').on('click', 'button', function () {
    var filterValue = $(this).attr('data-filter');
    // use filterFn if matches value
    filterValue = filterFns[filterValue] || filterValue;
    $container.isotope({
        filter: filterValue
    });
});

// bind sort button click
$('#sorts').on('click', 'button', function () {
    var sortByValue = $(this).attr('data-sort-by');
    $container.isotope({
        sortBy: sortByValue
    });
});

// change is-checked class on buttons
$('.button-group').each(function (i, buttonGroup) {
    var $buttonGroup = $(buttonGroup);
    $buttonGroup.on('click', 'button', function () {
        $buttonGroup.find('.is-checked').removeClass('is-checked');
        $(this).addClass('is-checked');
    });
});

   $('.isotope').append($newElems).isotope( 'appended', $newElems );

}
}

【讨论】:

  • 非常感谢!一旦我有足够的声誉,我会回来投票。这真的帮助了我。 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-28
  • 2019-08-26
  • 1970-01-01
  • 2015-09-03
  • 2020-05-21
相关资源
最近更新 更多