javascriptjquerylistdynatable

Querying a stylized list with dynatable


Although I have been searching high and low to find the answer to this question, I have not been able to find it. Should there be a well-hidden corner of the internet that I have not searched that does contain the answer to this question, please let me know.

Anyway, on the Dynatable website there is an example to query a table for a certain value. If your input html is indeed a table, this seems to work, but if you're using a stylized listthis produces the following error in my case:

Uncaught TypeError: Cannot read property 'length' of null

Which traces back to line 1582 of the dynatable.jquery.js file:

    // 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;
    }
  }

The following it my select element:

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

And the dynatable conversion of my list:

            // 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();
        });

I have not changed much from the examples of the website. The most I did was merge the list example with the query example. Again, if I apply a similar method to a table, it seems to work, but for my list it doesn't. What is going wrong?


Solution

  • The docs weren't very clear here, but I solved the same problem for myself earlier today. Here is your code in working order:

    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();
    });
    

    Cheers