1

在我的 asp.net MVC 4 应用程序中,我有一个带有两个 Kendo UI 网格的视图。其中一个包含带有属性的产品列表。我希望另一个具有完全相同的列,但为空。

当我在第一个网格上选择一行时,我希望将其从第一个网格中删除并添加到第二个网格中。我还希望能够从第二个中删除行并将它们添加回第一个。我不知道如何做到这一点。这是我的问题的答案,但我希望能够使用 Kendo UI asp.net MVC 包装器来实现它:

Kendo UI 通过控件复制数据

我有一个这样的 ViewModel:

public class SelectProductsViewModel
{
    public IEnumerable<Product> Products { get; set; }
    public IEnumerable<Product> SelectedProducts { get; set; }
}

我的控制器动作如下所示:

public ActionResult SelectProducts()
{
    var viewModel = new SelectProductViewModel
    {
        Products = GetProducts(), // Get Products form the database
        SelectedProducts = new List<Product>()
    }

    return View(viewModel);
}

而且,这是我的观点:

@(Html.Kendo().Grid(Model.Products)
    .Name("productsgrid")
    .Columns(columns =>
    {
        columns.Bound(p => p.ProductName);
        columns.Bound(p => p.Color);
        columns.Bound(p => p.Size);
        columns.Bound(p => p.Price);
    })
    .Pageable()
    .Sortable()
    .Filterable()
    .DataSource(dataSource => dataSource
        .Ajax()
        .Read(read => read.Action("GetProducts", "Product"))
        .Model(model =>
        {
            model.Id("ProductID");
        })
    )
)

@(Html.Kendo().Grid(Model.SelecteProducts)
    .Name("selectedproductsgrid")
    .Columns(columns =>
    {
        columns.Bound(p => p.ProductName);
        columns.Bound(p => p.Color);
        columns.Bound(p => p.Size);
        columns.Bound(p => p.Price);
    })
    .Pageable()
    .Sortable()
    .Filterable()
    .DataSource(dataSource => new DataSource())
)
4

3 回答 3

1
var userListGrid = $("#grid1").data("kendoGrid");
var selectedUserListGrid = $("#grid2").data("kendoGrid");

//Clear data.
$("#grid2").data('kendoGrid').dataSource.data([]);

for (var i = 0; i < userListGrid._data.length; i++) {

    var dataitem = userListGrid._data[i];
    //Some condition before add [IsChecked is a model property]
    if (dataitem.IsChecked == true) {
        selectedUserListGrid.dataSource.add(dataitem);
    }
}
于 2014-02-26T01:43:19.500 回答
0

您可以通过添加两个按钮来做到这一点,一个用于将产品添加到 selectedproductsgrid,另一个用于从中删除项目并将它们命名为 AddProduct、RemoveProduct 并在网格中启用多选,然后添加以下 javascript 代码:

function moveTo(from, to) {
var selecteddataset = to.dataSource._data;
var selected = $.map(from.select(), function (item) {
    var curuid = $(item).data("uid");
    var item = from.dataSource.getByUid(curuid);
    var isFound = false;
    for (var i = 0; i < selecteddataset.length; i++) {
        if (selecteddataset[i].uid == curuid) {
            isFound = true;
            break;
        }
    }
    if (isFound == false) {
        to.dataSource.add(item);
        //To add the selected one to SelectedProducts list in server code.
        var url = '@Url.Action("AddSelectedProduct", "Home")';
        $.post(url, { ProductName: item.ProductName, Color: item.Color, Size: item.Size }, function (result) {
        });
    }
});}

$("#AddProduct").click(function (e) {
    var productsList = $("#productsgrid").data("kendoGrid");
    var productsSelectedList = $("#selectedproductsgrid").data("kendoGrid");
    moveTo(productsList , productsSelectedList );
    e.preventDefault();
});

$("#RemoveProduct").click(function (e) {
    var productsSelectedList = $("#selectedproductsgrid").data("kendoGrid");
    var selected = $.map(productsSelectedList .select(), function (item) {
        var curuid = $(item).data("uid");
        var item = productsSelectedList.dataSource.getByUid(curuid);
        productsSelectedList .dataSource.remove(item);
        //To remove the selected from SelectedProducts datasource in server code
        var url = '@Url.Action("RemoveSelectedProduct", "Home")';
        $.post(url, { ProductName: item.ProductName }, function (result) {
        });

    });

    e.preventDefault();
});

动作功能:

public ActionResult AddSelectedProduct(string ProductName, string Color, string Size)
    {
        //Some functions to add to SelectedProducts list.
        return Content("");
    }

public ActionResult RemoveSelectedProduct(string ProductName)
    {
        //Search by product name in SelectedProducts and remove it.
        return Content("");
    }

并且var curuid = $(item).data("uid");将获取每个选定行的 id

于 2013-09-30T06:37:30.080 回答
0

我可能有同样的情况。我已经用下面的代码解决了:

 var selectedDepartments = [];
    $('#dptSelect').click(function (e) {
        var rows = $("#department").data("kendoGrid").select();

        rows.each(function () {
            var grid = $("#department").data("kendoGrid");
            var dataItem = grid.dataItem(this);
            if (!selectedDepartments.includes(dataItem)) {
                 selectedDepartments.push(dataItem);
            }
            console.log(dataItem);
        })

        var selDpt = new kendo.data.DataSource({
            data: selectedDepartments,
        });

        var selGrid = $("#selected_department").data("kendoGrid");
        selGrid.setDataSource(selDpt);


    });

    $('#dptDeselect').click(function (e) {
        var rows = $("#selected_department").data("kendoGrid").select();

        rows.each(function () {
            var grid = $("#selected_department").data("kendoGrid");
            var dataItem = grid.dataItem(this);

            var idx = selectedDepartments.indexOf(dataItem);
            selectedDepartments.splice(idx, 1);
            console.log(dataItem);
        })

        var selDpt = new kendo.data.DataSource({
            data: selectedDepartments,
             });
        var selGrid = $("#selected_department").data("kendoGrid");
        selGrid.setDataSource(selDpt);
    });
于 2020-03-02T07:32:08.760 回答