JS-添加和删除标签,显示展开&收起

需求:

点击添加标签,在下方添加标签;

超过一行显示“更多”,点击“更多”展开,并显示“收起”;

展开/收起时可以删除标签,如果小于一行,隐藏“展开收起”;

 

解决方法:

一开始用的固定一行height的高度,css来控制height:auto 和固定值切换,overflow-y:hidden;后来发现不满足需求;

后来就改成通过offset().top的值来区分第一行和其他行(添加other-row作为标识控制)了,总觉得应该还会有更简单的方法;

 

关键代码:

<script>
var selectedArray = selectTag();//选中后传回来的数据
                    $(".j-content-taglist").find("li").remove();//清空原先的标签
                    $(".j-content-taglist").next("a").remove();//清除原有的“展开/收起”
                    selectedArray.forEach(function(a, i) {
                        $(".j-content-taglist").append('<li class="mcs-courseTagList__item">' +
                            '<span class="mcs-courseTagList__text" data-id="'+a.id+'">'+a.ename+'</span>' + 
                            '<a href="javascript:;" class="mcs-courseTagList__close  j-close"><i class="ico-close"></i></a></li>');
                    })
                    $('.j-selected-employee').show(function(){
                        $(".j-content-taglist li").each(function(){
                            var top = $(this).position().top;
                            if(top>0){
                                $(this).addClass('other-row');//非第一行标注other-row
                            }
                        })
                        if($('.other-row').length!==0){
                            $('.j-selected-employee').append('<a href="javascript:;" class="more-btn j-more"><span>更多</span> <i class="arrow arrow-down"></i></a>');
                        }
                        $('.j-content-taglist .other-row').hide();
                    });
</script>
<script>
     //展开
$('body').on('click','.j-more',function () {
    $(this).parent().find('.other-row').show();
    $(this).after('<a href="javascript:;" class="more-btn j-fold"><span>收起</span> <i class="arrow arrow-up"></i></a>');
    $(this).remove();
    
});
//收起
$('body').on('click','.j-fold',function () {
    $(this).parent().find('.other-row').hide();
    $(this).after('<a href="javascript:;" class="more-btn j-more"><span>更多</span> <i class="arrow arrow-down"></i></a>');
    $(this).remove();
})
</script>
 1 <script>
 2      
 3 //删除
 4 $('body').on('click','.j-close',function () {
 5     $(this).parent().remove();
 6     if($('.j-more').length!==0){
 7         $('.j-content-taglist').height(30);
 8         $('.other-row').removeClass('other-row').show();
 9         $(".j-content-taglist li").each(function(){
10             var top = $(this).position().top;
11             if(top>0){
12                 $(this).addClass('other-row');
13             }
14         })
15         if($('.other-row').length===0){
16             $('.j-more').remove();
17         }
18         $('.other-row').hide();
19         $('.j-content-taglist').height('auto');
20     }
21     else if($('.j-fold').length!==0){
22         $('.other-row').removeClass('other-row');
23         $(".j-content-taglist li").each(function(){
24             var top = $(this).position().top;
25             if(top>0){
26                 $(this).addClass('other-row');
27             }
28         })
29         if($('.other-row').length===0){
30             $('.j-fold').remove();
31         }
32     }
33     
34 })
35 </script>

 

 如果有更好简单的方法,欢迎留言!

转载于:https://www.cnblogs.com/monozxy/p/11277066.html

在Element UI的表格组件`el-table-column`中,列展开收起功能通常用于处理复杂数据或者当列内容过多时提高可读性。这个功能通常通过设置`expand`属性来实现,该属性可以是一个函数或一个对象。 1. **设置展开/收起标志(对象)**:你可以为每一列定义一个对象,其中包含`field`(字段名)和`expand`(是否展开)两个属性。例如: ```javascript <el-table-column field="title" :expand="{'field': 'detail', 'expanded': false}" :render="renderDetails" ></el-table-column> ``` 这里,如果`detail`字段存在且为对象,点击标题时会展开展示详情内容。 2. **自定义展开/收起逻辑(函数)**:如果你需要根据数据动态决定是否展开,可以使用一个返回布尔值的函数: ```javascript <el-table-column field="title" :expand="isExpand(row)" :render="renderDetails" ></el-table-column> ``` `isExpand(row)` 函数接受当前行数据作为参数,根据数据内容判断是否展开。 3. **展开/收起方法**:如果你需要提供一个手动控制展开收起的接口,可以在列上添加`header-event`,监听点击事件并调用相应的方法。 ```html <el-table-column field="title" header-event="handleExpand" :render="renderDetails" ></el-table-column> ``` 在Vue实例中定义`handleExpand`方法,处理展开收起操作。 相关问题: 1. el-table-column如何实现列的自动展开? 2. 如何在用户交互时动态控制列的展开状态? 3. el-table-column的`header-event`用于什么?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值