data-attribute to list <li>

Hey,

I'd like to add a html5 data-attribute to the elements of certain lists (gallery type).
Does anyone have an idea how to do this without overriding the list view?

It's a default elgg_list_entities_from_metadata query.
 

  • The easier would perhaps be using `item_view` parameter in list options, wrap your item with a div that has data-attribute, then just use $.parent().

    // custom/item.php
    $entity = elgg_extract('entity', $vars);
    $view = elgg_view_list_item($entity);
    echo elgg_format_element('div', [
       'data-guid' => $entity->guid,
    ], $view);
    
    
    // custom/list.php​
    elgg_require_js('custom/list');
    echo elgg_list_entities([
        'types' => 'object',
        'item_view' => 'custom/item',
    ]);
    
    // custom/list.js
    define(function(require) {
       var $ = require('jquery');
       $('li > [data-guid]').each(function() {
          console.log($(this).data('guid'));
       });
    });
    
  • Thanks, I was going to try the item_view parameter, but I didn't thought  I could use a custom list like that. Thanks!