backbone.js 如何为backgridjs元素添加导航?

lhcgjxsq  于 2022-11-10  发布在  其他
关注(0)|答案(1)|浏览(131)

我对backgrid和backgrid很陌生,想知道如何为backgrid网格添加导航锚?到目前为止,我创建了许多网格,并将它们的 meta信息存储在导航集合中。导航和网格元素被呈现,但我不知道如何将导航锚点连接到网格表的任何内容。
示例代码中,我使用了grid.render().cid,但它不起作用:

races = { };  
  docs.forEach(function(doc) {
    if (races[doc.s_y] === undefined) {
      races[doc.s_y] = {};
    }
    if (races[doc.s_y][doc.t] === undefined) {
      races[doc.s_y][doc.t] = new Races();
      races[doc.s_y][doc.t].add(doc);
    } else {
      races[doc.s_y][doc.t].add(doc);
    }

  });
  console.log("Found number of docs", docs.length);

  var NavigationCollection = Backbone.Collection.extend({
    model : Race
  });
  var navCollection = new NavigationCollection();
  var NavView = Backbone.View.extend({  
    el : $('ul'), 

    initialize: function(){
      _.bindAll(this, 'render'); 
      // this.render();  
    },  

    render : function() {
      this.collection.each(function (race) {
        this.$el.append("<li><a href=#" + race.get("link") + ">" + race.get("track") + " " + race.get("year") + "</a></li>");
      }, this);
      return this; // remember this for chaining
    }

  });

  var grids = {};
  for (var key in races) {
    if (races.hasOwnProperty(key)) {
      for (var track in races[key]) {
        var grid = new Backgrid.Grid({
          columns: columns,
          collection: races[key][track],
          emptyText: "No Data, Loading"
        });
        if (grids[key] === undefined) {
          grids[key] = {};
        }
        grids[key][track] = grid;
        console.log(key + " -> " + races[key][track]);
        grid.render().sort("p", "ascending");
        console.log(grid.render());
        navCollection.add({ year: key, track: track, link: grid.render().cid });
        $("#race-results").append(grid.render().el);
      }
    }
  }
  var navView = new NavView({
    collection: navCollection
  });
  $("#navigation").append(navView.render().el);
v2g6jxz6

v2g6jxz61#

刚发现'id'可以在网格定义中定义。我在那里使用'track'并在导航元素中将其保存为'link'。也许有更好的方法来实现这一点?

相关问题