list.js icon indicating copy to clipboard operation
list.js copied to clipboard

How do I pass a custom style into 'item'?

Open TomLewis opened this issue 6 years ago • 5 comments

I need to be able to set custom class names based on specific rows to highlight them in my list. I cant pass a variable into the 'item' it only allows me to set inside but I need to SET the class name per item in my list.

        <script>
            var options = {
              valueNames: [ 
                'name', 
                'rank',
                { 
                  attr: 'src', 
                  name: 'image' 
                },
                { 
                  attr: 'href', 
                  name: 'link' 
                },
              ],
              
              item: '<li class="list-group-item list-group-item-action p-1 small"><img class="image img-fluid" src="" /><a class="link name" href=""></a> - <span class="rank"></span></li>'

            };
            
            var values = [];
            var userList = new List('users', options, values);
</script>

TomLewis avatar Dec 13 '18 13:12 TomLewis

Interesting feature. I think this could be solved by #634 if I rewrote it a bit. We'll see. I'll keep this issue open.

javve avatar Dec 18 '18 21:12 javve

@javve Has this feature ever made it? I can not find any documentation. I would also really like to have this feature. And #634 is still open... @FrozenBeard did you manage to achieve this?

image

As the image shows, I would like to change the color of the badges based on it's status. I would like to do this by changing the color class associated to the badge. Using Tailwind CSS to style.

MartijnHarmenzon avatar Mar 14 '20 10:03 MartijnHarmenzon

Yeah, I moved to a different library ☹️

TomLewis avatar Mar 15 '20 13:03 TomLewis

@FrozenBeard I see. Out of curiosity, where did you move to?

I managed to solve it for now by adding the color classes afterwards by using the following code.

    notificationList.items.forEach(function (element) {
      const status = element.values().status;
      let bg_color;

      switch (status) {
        case 'High':
          bg_color = 'bg-red-700';
          break;
        case 'Middle':
        bg_color = 'bg-orange-500';
          break;
        case 'Low':
        bg_color = 'bg-green-600';
          break;
        default:
        bg_color = 'bg-gray-400';
          break;
      }

      element.elm.cells[1].children[0].classList.add(bg_color);
    });

MartijnHarmenzon avatar Mar 15 '20 15:03 MartijnHarmenzon

You can solve this using an item function that generates arbitrary HTML:

item: function(v) {
  return `<whatever class="name ${v.myclass}"></whatever>`;
}

pmarks-net avatar Dec 31 '23 15:12 pmarks-net