简体   繁体   English

如何在传递给视图以作为有序列表进行迭代的 ViewModel 集合中获取 jQuery 可选对象的值?

[英]How can I get the value of a jQuery selectable object in a ViewModel collection I passed to my view to be iterated through as an ordered list?

Convoluted question, I know.复杂的问题,我知道。 I'm working on a small MVC application for the purposes of my own learning and I'm stuck at this point after much SO scouring.为了我自己的学习目的,我正在开发一个小型 MVC 应用程序,经过大量的搜索后,我被困在了这一点上。 My View is such (note that ItemList is a List):我的视图是这样的(注意 ItemList 是一个列表):

@model PackItOut.ViewModels.IndexViewModel
<ol id="item-list-20">
                @foreach (var i in Model.ItemList)
                {
                    <li class="ui-widget-content">
                        @switch (i.Category)
                        {
                            case "Food":
                                <i class="fa fa-cutlery" aria-hidden="true"></i>
                                break;
                            case "Shelter":
                                <i class="fa fa-tree" aria-hidden="true"></i>
                                break;
                            case "First Aid":
                                <i class="fa fa-medkit" aria-hidden="true"></i>
                                break;
                            case "Clothing":
                                <i class="fa fa-child" aria-hidden="true"></i>
                                break;
                            case "Fluid":
                                <i class="fa fa-tint" aria-hidden="true"></i>
                                break;
                            case "Weather":
                                <i class="fa fa-bolt" aria-hidden="true"></i>
                                break;
                            default:
                                break;
                        }
                        @i.Name
                    </li>
                }
            </ol>
<button class="btn btn-danger change-item" id="remove-item-btn"><i class="fa fa-trash" aria-hidden="true"></i> Remove Item</button>

The Javascript is such: Javascript 是这样的:

$(document).ready(function() {
var theItemList = $("ol[id*='item-list-']");
$(theItemList).selectable({
    selected: function(event, ui) {
        $(ui.selected).addClass("ui-selected").siblings().removeClass("ui-selected");
    },
    stop: function () {
        var hasSelection = $(".ui-selected");
        if (hasSelection != null)
            $(".change-item").css('visibility', 'visible');
        else
            $(".change-item").css('visibility', 'hidden');
    }
});
$("#remove-item-btn").on("click", function (event) {
    var targetItem = document.getElementsByClassName("ui-selected");
    if (targetItem == null)
        alert('Please select an item to remove.');
});
});

Ideally, in the .on("click") I'd like to pass in the object represented by the targeted item so I can remove the item from the ViewModel collection but I'm having trouble getting the actual object and not just the HTML.理想情况下,在 .on("click") 中,我想传入由目标项目表示的对象,以便我可以从 ViewModel 集合中删除该项目,但我无法获取实际对象而不仅仅是 HTML . Any suggestions on not only this issue but any other issues you see in this snippet are always welcome.任何关于不仅这个问题而且你在这个片段中看到的任何其他问题的任何建议总是受欢迎的。 Thanks in advance.提前致谢。

I'm going to look into my crystal ball and predict the future.我要看着我的水晶球并预测未来。 Once you complete the removal functionality, you're going to want to be able to add to the list as well, right?完成删除功能后,您也希望能够添加到列表中,对吗? Good thing there's a NuGet package called BeginCollectionItem and related blog post by Steve Sanderson dedicated to this exact scenario - adding and removing items from a list in MVC.好消息是有一个名为 BeginCollectionItem 的NuGet包和 Steve Sanderson 的相关博客文章,专门针对这种情况 - 在 MVC 的列表中添加和删除项目。

I'm not going to write out a full solution because that's been done, but I recently implemented this myself using this package.我不打算写出完整的解决方案,因为已经完成了,但我最近使用这个包自己实现了这个。 There are several answers on SO involving this package that should help to get you started.关于这个包的 SO 有几个答案,应该可以帮助您入门。 This one is especially good.这个特别好。

In response to your specific question, however, you could do something like is mentioned in the linked answer to simply remove a div associated with a list item:但是,为了回答您的具体问题,您可以执行链接答案中提到的类似操作,以简单地删除与列表项关联的div

HTML/Razor: HTML/剃刀:

@model PackItOut.ViewModels.IndexViewModel
<div id="editorRows">
    <ol id="item-list-20">
        @foreach (var i in Model.ItemList)
        {
            <div class="editorRow">
                <li class="ui-widget-content">
                    @switch (i.Category)
                    {
                        case "Food":
                            <i class="fa fa-cutlery" aria-hidden="true"></i>
                            break;
                        case "Shelter":
                            <i class="fa fa-tree" aria-hidden="true"></i>
                            break;
                        case "First Aid":
                            <i class="fa fa-medkit" aria-hidden="true"></i>
                            break;
                        case "Clothing":
                            <i class="fa fa-child" aria-hidden="true"></i>
                            break;
                        case "Fluid":
                            <i class="fa fa-tint" aria-hidden="true"></i>
                            break;
                        case "Weather":
                            <i class="fa fa-bolt" aria-hidden="true"></i>
                            break;
                        default:
                            break;
                    }
                    @i.Name
                </li>
                <button class="btn btn-danger deleteRow"><i class="fa fa-trash" aria-hidden="true"></i> Remove Item</button>
            </div>
        }
    </ol>
</div>

JavaScript: JavaScript:

$('#editorRows').on('click', '.deleteRow', function () {
    $(this).closest('.editorRow').remove();
});

Although you will have to play with your HTML elements because you can't nest a <div> within an <ol> .尽管您将不得不使用 HTML 元素,因为您不能将<div>嵌套在<ol>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何对通过上下文传递给我的 ajax done 函数的值进行类型检查? - How can I get type checking for a value passed into my ajax done function through the context? 如何使用JQuery中的“每个”功能搜索有序列表 - How can i search ordered list using the “each” function in JQuery 如何让我的默认属性数组访问插件中传递的jQuery对象? - How can I give my default array of properties access to the passed jQuery object in my plugin? 有没有办法可以将 mongodb 集合作为有序列表发送到 ejs 文件? - Is there a way I can send a mongodb collection to an ejs file as an ordered list? 如何通过给定的 object map 获得“价值”..? - How can I map through the given object to get “value”..? 如何让JSDoc将我的param标记为jQuery对象? - How can I get JSDoc to mark my param as a jQuery object? 使用可选的 jQuerys UI 时如何获取所选项目的列表? - How can I get a list of selected items when using jQuerys UI selectable? 如何遍历数据集合并返回整个对象? - How can I loop through a collection of data and return the entire object? 如何获取数组中的对象值? - How can I get an object value in my array…? 当集合的值更改时,如何更改视图 - How do I change view when the value of my collection changes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM