如何将我选择的 GridView 行放入 Javascript 变量中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10124809/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 23:58:05  来源:igfitidea点击:

How do I get my selected GridView rows into a Javascript variable?

javascriptasp.net-mvcgridviewrazordevexpress

提问by MrSlippyFist

I use ASP.NET C# MVC3 with razor templates and I want to know the following:

我将 ASP.NET C# MVC3 与剃刀模板一起使用,我想知道以下内容:

I have a DevExpress GridViewin which I can select rows.
When selected, I want to pass them into a Javascript variable. How do I do this?
And what if I have selected multiple rows at the same time, can I get them in an array or something?

我有一个 DevExpress GridView,我可以在其中选择行。
选中后,我想将它们传递给 Javascript 变量。我该怎么做呢?
如果我同时选择了多行,我可以将它们放入数组或其他东西中吗?

Edit: Thank you for the comment, I now have the following 'JQuery' function:

编辑:感谢您的评论,我现在有以下“JQuery”功能:

$(function () { // Verwijder functie
    $('#select').click(function () {
        var Delete = confirm("Weet u zeker dat u de geselecteerde records wilt verwijderen?");
        if (Delete) {
            //Verwijder Funtie
            var test = ;
            alert(test);
        } else {
            //Niks
        }
    });
});

I need to get the Id of the selected rows into the variable 'test', I tried it with GetSelectedFieldValuesCallback and GetSelectedFieldValues. But this is not working as I'm expecting. If someone can provide some example I would really appreciate that.

我需要将所选行的 Id 放入变量“test”中,我使用 GetSelectedFieldValuesCallback 和 GetSelectedFieldValues 进行了尝试。但这并不像我期望的那样工作。如果有人可以提供一些示例,我将不胜感激。

回答by Draxler

There is a surprising lack of examples for one of the most basic operations of a grid. Especially if you want to send rows back to the server for further processing.

对于网格最基本的操作之一,令人惊讶的是缺乏示例。特别是如果您想将行发送回服务器进行进一步处理。

The problem with grid.GetSelectedFieldValues() is that it generates a postback. Meaning you would need a second postback to actually send data back to the server.

grid.GetSelectedFieldValues() 的问题在于它会生成回发。这意味着您需要第二次回发才能将数据实际发送回服务器。

The most elegant solution I was able to achieve is to use grid.GetSelectedKeysOnPage(). This will return selected key fields defined through settings.KeyFieldName = "Id";

我能够实现的最优雅的解决方案是使用 grid.GetSelectedKeysOnPage()。这将返回通过 settings.KeyFieldName = "Id"; 定义的选定关键字段。

the view which will display your grid.

将显示您的网格的视图。

<script type="text/javascript">
    $(function () {
        $('#btn1').click(function () {
            simpleGrid.PerformCallback();
        });
    });
    function OnBeginCallback(s, e) {
        var selectedValues = s.GetSelectedKeysOnPage();
        e.customArgs["Id"] = "";
        for (var i = 0; i < selectedValues.length; i++) {
            e.customArgs["Id"] += selectedValues[i] + ',';
        }
    }
</script>
@Html.Partial("ProductsPartial", Model.Data)
<div id="btn1">
    btn
</div>

It is important that you create your grid in a separate partial view (don't know why, but that is what it says on the devexpress page.

在单独的局部视图中创建网格很重要(不知道为什么,但它在 devexpress 页面上是这么说的。

The "ProductsPartial" partial view:

“ProductsPartial”部分视图:

@Html.DevExpress().GridView(
        settings =>
        {
            settings.Name = "simpleGrid";
            settings.KeyFieldName = "Id";
            settings.CallbackRouteValues = new { Controller = "yourController", Action = "Postback" };
            settings.SettingsText.Title = "simpleGridWithPostback";
            settings.Settings.ShowTitlePanel = true;
            settings.Settings.ShowStatusBar = GridViewStatusBarMode.Visible;
            settings.SettingsPager.Mode = GridViewPagerMode.ShowAllRecords;
            settings.SettingsPager.AllButton.Text = "All";
            settings.SettingsPager.NextPageButton.Text = "Next &gt;";
            settings.SettingsPager.PrevPageButton.Text = "&lt; Prev";
            settings.Width = new System.Web.UI.WebControls.Unit(200);
            settings.Columns.Add("Id");
            settings.Columns.Add("Name");
            settings.CommandColumn.Visible = true;
            settings.CommandColumn.ShowSelectCheckbox = true;
            settings.ClientSideEvents.BeginCallback = "OnBeginCallback";
        }).Bind(Model).GetHtml()

And finally the controller in which you can process the data

最后是您可以处理数据的控制器

public ActionResult Postback()
{
    String data = Request["Id"];
}

This way you can process all the data you want server side

这样你就可以在服务器端处理你想要的所有数据

回答by MrSlippyFist

I have found the solution. The following function provides the data I want:

我找到了解决办法。以下函数提供了我想要的数据:

$(function () {
    $('#select').click(function () {
        Index.GetSelectedFieldValues("Id", OnGetSelectedFieldValues);
    });
});

function OnGetSelectedFieldValues(result) {
    for (var i = 0; i < result.length; i++){
            alert(result[i]);
        }
}

For people who had/have the same problem.

对于有/有同样问题的人。

Change "Index"in

“索引”更改为

Index.GetSelectedFieldValues("Id", OnGetSelectedFieldValues); 

to whatever you named your gridview, and change "Id" to the column you want to get.

将您命名为 gridview 的任何内容,并将“Id”更改为您想要获取的列。

You can also get multiple data from a single row, to do this you need to add another for-loop in the function OnGetSelectedFieldValues(result)as following:

您还可以从一行中获取多个数据,为此您需要添加另一个 for 循环,function OnGetSelectedFieldValues(result)如下所示:

function OnGetSelectedFieldValues(result) {
    for (var i = 0; i < result.length; i++)
        for (var j = 0; j < result[i].length; j++) 
            alert(result[i][j]);
        }
}

You wil also need to change the getter as following

您还需要更改吸气剂如下

Index.GetSelectedFieldValues("Id;YOUROTHERCOLUMN", OnGetSelectedFieldValues);

Index.GetSelectedFieldValues("Id;YOUROTHERCOLUMN", OnGetSelectedFieldValues);

I hope this will help in the future for other users.

我希望这会在未来对其他用户有所帮助。