获取datagrid选中行的数据,显示到window窗口中

(在上次随笔代码的基础上)

1.datagrid代码:

<div style="width:100%;height:100%;">
    <table class="easyui-datagrid" id="datagridUser" style="width: 100%; height: 95%;" data-options="singleSelect:true,url:‘@Url.Action("GetList")‘,method:‘get‘,toolbar:toolbar" pagination="true" sortname="Id" sortorder="asc">
        <thead>
            <tr>
                <th style="width:10%" data-options="field:‘Id‘" sortable="true">编号</th>
                <th style="width:15%" data-options="field:‘Name‘" sortable="true">登录名</th>
                <th style="width:15%" data-options="field:‘RealName‘" sortable="true">姓名</th>
                <th style="width:15%" data-options="field:‘Sex‘" sortable="true">性别</th>
                <th style="width:15%" data-options="field:‘Email‘">邮箱</th>
                <th style="width:15%" data-options="field:‘Phone‘">电话</th>
                <th style="width:15%" data-options="field:‘Address‘">地址</th>
            </tr>
        </thead>
    </table>
</div>

2.JS代码

<script type="text/javascript">
    var toolbar =  [{
        text: ‘删除用户‘,
        iconCls: ‘icon-clear‘,
        handler: function () {
            var row = $("#datagridUser").datagrid("getSelected");
            if (row != null) {               
                var userId = row.Id;
                //执行删除操作
                if (confirm("确认删除吗?"))
                {
                    $.ajax({
                        type: "POST",
                        url: "/UserManager/DeleteUser",
                        data: "id=" + userId,
                        success: function (result) {
                            if (result == "true") {
                                $("#datagridUser").datagrid("reload");
                            }
                        }
                    });
                }                        
            }
            else {
                alert("请选择要删除的项!");
            }
        }
    }, {
        text: ‘查看详情‘,
        iconCls: ‘icon-search‘,
        handler: function () {
            var row = $("#datagridUser").datagrid("getSelected");
            if (row != null) {
                //绑定窗体数据
                var userId = row.Id;
                //执行查询操作
                $.ajax({
                    type: "get",
                    url: "/UserManager/GetUser",
                    data: "id=" + userId,
                    success: function (jsonUserInfo) {
                        var json = JSON.parse(jsonUserInfo);
                        $("#textInfoName").val(json.Name);
                        $("#textInfoRealName").val(json.RealName);
                        $("#textInfoSex").val(json.Sex);
                        $("#textInfoEmail").val(json.Email);
                        $("#textInfoPhone").val(json.Phone);
                        $("#textInfoAddress").val(json.Address);                      
                      
                        //打开窗口,居中显示
                        $(‘#windowUserInfo‘).window({
                            top: ($(window).height() - 300) * 0.5,
                            left: ($(window).width() - 500) * 0.5,
                        }).window(‘open‘);
                    }
                });
            }
            else {
                alert("请选择用户!");
            }
        }
    }];
</script>

注:var row = $("#datagridUser").datagrid("getSelected")获取选中行;var userId = row.Id 获取该行数据的ID; url: "/UserManager/GetUser" ajax调用action获取数据;我的action传回的是json字符串;$("#textInfoName").val(json.Name)  绑定显示数据。

3.control

[HttpPost]
        public ActionResult DeleteUser(int id)
        {           
            var user = userService.Get(x => x.Id == id);   //获取要删除的User
            if (userService.Delete(user))
            {
                return Content("true");
            }
            else
            {
                return Content("false");
            }
        }

    [HttpGet]
        public string GetUser(int id)
        {
            var user = userService.Get(x => x.Id == id);   //获取要删除的User           
            var json = JsonConvert.SerializeObject(user);
            return json;
        }

注:代码只有部分,备忘方便以后查看

时间: 2024-08-28 08:33:58

获取datagrid选中行的数据,显示到window窗口中的相关文章

wpf dataGrid 选中行 失去焦点时 的背景颜色的更改

关于 wpf dataGrid 选中行 失去焦点时 的背景颜色的更改.很简单的方式,在datagrid的resource中更改InactiveSelectionHighlightBrushKey属性的值即可. 关键代码如下: <DataGrid.Resources> <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="Yellow

easy ui datagrid 获取选中行的数据

取得选中行数据: var row = $('#tt').datagrid('getSelected'); if (row){ alert('Item ID:'+row.itemid+" Price:"+row.listprice); } 取得所有选中行的 itemid: var ids = []; var rows = $('#tt').datagrid('getSelections'); for(var i=0; i<rows.length; i++){ ids.push(ro

bootstrapTable:获取选中行的数据

必须要有checkbox:true和singleSelect:true,然后就可以通过var row=$("#mytab").bootstrapTable('getSelections');获取行数据了,使用时因为是单行选中所以需要row[0].列名方式取值 如下加粗加大的代码 function edit(){ // 获取当前行 var row=$("#mytab").bootstrapTable('getSelections'); alert(row[0].pro

C# 获取DataGridView选中行的值

int index = DataGridView1.CurrentRow.Index;    //取得选中行的索引 txt_ProductId.Text = DataGridView1.Rows[index].Cells["Id"].Value;   //获取单元格列名为‘Id’的值

Silverlight获取DataGrid选中的行数据

注意触发的事件为:CurrentCellChanged后台代码: /// <summary> /// 获取datagrid当前选中的单元格数据 /// 如果绑定的数据源为实体的话默认选中的当前记录为一条实体 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void dgca

eazyUI 修改datagrid选中行默认背景色

<style> .datagrid-row-selected { background: #D1EEEE; color: #fff; } </style> 在html代码中,加入这段CSS样式,重新设置选中行背景色即可

Winform如何获取ListView选中行数据

1.设计窗体 一个ListView    listView1 一个textBox    txbSelected 2.listView1填充数据 private void Form1_Load(object sender, EventArgs e) { // listView1填充数据 ListViewItem list = listView1.Items.Add("1"); list.SubItems.Add("依依"); ListViewItem list1 =

Table获取checkbox选中行数据

//检测勾选值 function checkEnter() { var Ivalue = ""; $("#dataTable tr").each(function () { var chk = $(this).find('td').eq(0).find('#subcheck'); if (chk.attr("checked") == "checked") { var IsEter = $(this).find('td').eq

easyUI这样获取Json的内嵌数据 显示json下的json

先给出返回的json数据. { "total": "2", "rows": [ { "id": "1", "name": "张富银", "xiuhao": "2014009012", "exttend": { "sid": "1", "tid": &