Javascript 如何在单击时删除多个播放列表项目?
我有一个api,当我单击mehr(更多)按钮时,它会从Jasonfile加载10个项目。如果再次单击,它将加载10个以上,以此类推。。。在JPlayer中,它们仅显示如何一次删除1项:Javascript 如何在单击时删除多个播放列表项目?,javascript,jquery,jplayer,Javascript,Jquery,Jplayer,我有一个api,当我单击mehr(更多)按钮时,它会从Jasonfile加载10个项目。如果再次单击,它将加载10个以上,以此类推。。。在JPlayer中,它们仅显示如何一次删除1项: myPlaylist.remove(); // Removes all items myPlaylist.remove(0); // Removes the 1st item myPlaylist.remove(1); // Removes the 2nd item myPlaylist.remo
myPlaylist.remove(); // Removes all items
myPlaylist.remove(0); // Removes the 1st item
myPlaylist.remove(1); // Removes the 2nd item
myPlaylist.remove(-1); // Removes the last item
当我单击weniger(更少)按钮时,是否可以一次删除10个项目?我试过这个:
$(".pc-less-btn.btn-{{ pk }}").click(function() {
myPlaylist.remove(10); // To Removes 10 items but doesn't works
});
选项1:设置新播放列表
$(".pc-less-btn.btn-{{ pk }}").click(function() {
const removeFromIndex = 10
// remove from end
const newPlaylist = myPlaylist.playlist.slice(myPlaylist.playlist.length - removeFromIndex)
// remove from start
// const newPlaylist = myPlaylist.playlist.slice(removeFromIndex)
myPlaylist.setPlaylist(newPlaylist)
});
选项2:逐个移除(不推荐)
$(“.pc减去btn.btn-{{pk}”)。单击(函数(){
for(设i=0;i<10;i++){
myPlaylist.remove(i);//播放列表的索引可能会更改
}
});
这是一个棘手的解决方案。我想删除播放列表中的所有项目,所以我正在寻找解决方案,所以我发现这个技巧到目前为止对我有效。我已经在版本2.9.2中进行了测试
假设您的playlist对象是player
,那么在任何jQuery单击事件中,您都可以像下面这样获得所选列表索引
$("#selector_id").on("click", function (e) {
var object = $(this);
var ids = []; // Fetch you ids in your way, either checkbox or anything or manual enter
// Then get the playlist from the player object.
var playlist = player.playlist;
// Check if playlist has data.
if (playlist && playlist.length) {
var new_playlist = [];
$.each(playlist, function (index, item) {
// If index is not in your ids then get the playlist item.
if (!ids.includes(index)) {
new_playlist.push(item);
}
});
// Now you have new playlist after removing selected items.
// Check if we have anything in new playlist.
if (new_playlist.length) {
// If we have data in new playlist then init playlist.
player._initPlaylist(new_playlist);
// Then call the refresh function with clearMedia event.
player._refresh(function () {
$(player.cssSelector.jPlayer).jPlayer("clearMedia");
});
// You can call update Control if you have Controls enabled.
}
}
});
如果您查看文档,看起来无法删除一系列播放列表项。你可以在点击中做一个简单的循环,然后使用thatHi@medet,谢谢你的快速回答。但它删除了10并再次添加了10。Init不应该添加,使用removeFromIndex值,可能您删除的比添加的多?当我使用remove from end时会这样做。但是当我使用remove from start时,它不会添加。
$("#selector_id").on("click", function (e) {
var object = $(this);
var ids = []; // Fetch you ids in your way, either checkbox or anything or manual enter
// Then get the playlist from the player object.
var playlist = player.playlist;
// Check if playlist has data.
if (playlist && playlist.length) {
var new_playlist = [];
$.each(playlist, function (index, item) {
// If index is not in your ids then get the playlist item.
if (!ids.includes(index)) {
new_playlist.push(item);
}
});
// Now you have new playlist after removing selected items.
// Check if we have anything in new playlist.
if (new_playlist.length) {
// If we have data in new playlist then init playlist.
player._initPlaylist(new_playlist);
// Then call the refresh function with clearMedia event.
player._refresh(function () {
$(player.cssSelector.jPlayer).jPlayer("clearMedia");
});
// You can call update Control if you have Controls enabled.
}
}
});