1

尽管我一直在寻找这个问题的答案,但我一直无法找到它。如果互联网上有一个我没有搜索过的隐藏的角落确实包含这个问题的答案,请告诉我。

无论如何,在Dynatable 网站上,有一个示例可以查询表中的某个值。如果您的输入 html 确实是一个表格,这似乎可行,但如果您使用的是程式化列表,这在我的情况下会产生以下错误:

Uncaught TypeError: Cannot read property 'length' of null

这可以追溯到 dynatable.jquery.js 文件的第 1582 行:

    // Find an object in an array of objects by attributes.
// E.g. find object with {id: 'hi', name: 'there'} in an array of objects
findObjectInArray: function(array, objectAttr) {
  var _this = this,
      foundObject;
  for (var i = 0, len = array.length; i < len; i++) {
    var item = array[i];
    // For each object in array, test to make sure all attributes in objectAttr match
    if (_this.allMatch(item, objectAttr, function(item, key, value) { return item[key] == value; })) {
      foundObject = item;
      break;
    }
  }

下面是我的选择元素:

        <select id="filter-prop" name="prop">
          <option>All</option>
          <option>First Run</option>
          <option>Rejected</option>
        </select>

以及我的列表的可动态转换:

            // Function that renders the list items from our records
        function ulWriter(rowIndex, record, columns, cellWriter) {
          var cssClass = record.prop, li;
          if (rowIndex % 3 === 0) { cssClass += ' first'; }
          li = '<li class="' + cssClass + '"><div class="thumbnail"><div class="thumbnail-image">' + record.thumbnail + '</div><div class="caption">' + record.caption + '</div></div></li>';
          return li;
        }

        // Function that creates our records from the DOM when the page is loaded
        function ulReader(index, li, record) {
          var $li = $(li),
              $caption = $li.find('.caption');
          record.thumbnail = $li.find('.thumbnail-image').html();
          record.caption = $caption.html();
          record.label = $caption.find('h3').text();
          record.description = $caption.find('p').text();
          record.color = $li.data('color');
          record.prop = $li.attr('class');
        }

        var pictable = $('#picturelist').dynatable({
          table: {
            bodyRowSelector: 'li'
          },
          features :{
            recordCount: false,
            sorting: false,
          },
          dataset: {
            perPageDefault: 10,
            perPageOptions: [5, 10, 15, 20]
          },
          writers: {
            _rowWriter: ulWriter
          },
          readers: {
            _rowReader: ulReader
          },
          params: {
            records: 'objects'
          }
        }).data('dynatable');


        $('#filter-prop').change( function() {
            var value = $(this).val();
            if (value === "All") {
                pictable.queries.remove("Prop");
            } else if (value === "First Run") {
                pictable.queries.add("Prop","span4 firstrun rejected");
                pictable.queries.add("Prop","span4 firstrun");
            } else if (value === "Rejected") {
                pictable.queries.add("Prop","span4 firstrun rejected");
                pictable.queries.add("Prop","span4 rejected");
            };
            pictable.process();
        });

我与网站的示例没有太大变化。我做的最多的是将列表示例与查询示例合并。同样,如果我对表格应用类似的方法,它似乎可以工作,但对于我的列表却没有。出了什么问题?

4

1 回答 1

1

这里的文档不是很清楚,但我今天早些时候为自己解决了同样的问题。这是您的工作顺序代码:

var pictable = $('#picturelist')

    .bind('dynatable:init', function(e, dynatable) {
        dynatable.queries.functions['prop'] = function(record, queryValue) {

            if ( record.prop == queryValue )
            {
                return true;
            }
            else
            {
                return false;
            }

        };
    })

    .dynatable({
        table: {
            bodyRowSelector: 'li'
        },
        features :{
            recordCount: false,
            sorting: false
        },
        dataset: {
            perPageDefault: 10,
            perPageOptions: [5, 10, 15, 20]
        },
        writers: {
            _rowWriter: ulWriter
        },
        readers: {
            _rowReader: ulReader
        },
        params: {
            records: 'objects'
        }
}).data('dynatable');


$('#filter-prop').change( function() {
    var value = $(this).val();
    if (value === "All")
    {
        pictable.queries.remove("prop");
    }
    else
    {
        pictable.queries.add("prop", value)
    }

    pictable.process();
});

干杯

于 2014-12-26T18:53:22.687 回答