Jquery easyui从零单排之datagrid单行数据删除

        这里主要学习datagrid单行数据删除功能的实现。


一、前台代码:


    
    Start from zero
    
    
    
    
    


    
LoginID
UserName Sex Department Age 删除

1.首先在datagrid里添加删除列:

 
                    删除
                
function formatOper1(val, row, index)
        {
            return '删除';
        }

2.然后在添加函数:

 function deletedate(index)
        {
            $("#test").val("deletes");
            $('#dg').datagrid('selectRow', index); // 关键在这里  
            var row = $('#dg').datagrid('getSelected');
            if (row) {

                if (confirm("确认要删除?")) {
                    $('#dg').datagrid('reload', {
                        test: $('#test').val(),
                        loginid: row.LoginID
                    });
                }
            }
        }

        因为是删除选中的单行,关键是这里$('#dg').datagrid('selectRow', index),test是操作标识,loginid是向处理页面传值的id,处理程序根据id来进行删除的sql操作。

在这里添加了按钮提醒if (confirm("确认要删除?")),是否确定要删除.


二、处理代码Handler.ashx:

<%@ WebHandler Language="C#" Class="Handler" %>

using System;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using Newtonsoft.Json;
using System.Text;

public class Handler : IHttpHandler {

    public void ProcessRequest(HttpContext context)
    {
        string test = context.Request.QueryString["test"];//前台传的标示值    
        string username = context.Request.QueryString["username"];//前台传的标示值    
        string loginid = context.Request.QueryString["loginid"];//前台传的标示值   

        string sort = !string.IsNullOrEmpty(context.Request.QueryString["sort"])?context.Request.QueryString["sort"]:"LoginID"; 
        string order = !string.IsNullOrEmpty(context.Request.QueryString["order"])?context.Request.QueryString["order"]:"asc";
        if (string.IsNullOrEmpty(test))
        {
            Query(context,sort,order);
        }
        else if (test == "search")
        {
            SearchData(context, username, loginid);
        }
        else if (test == "deletes")
        {
            DeleteData(context,loginid);
        }
    }
    public void Query(HttpContext context,string sort,string order)
    {
        context.Response.ContentType = "text/plain";
        SqlHelp sqla = new SqlHelp();
        string stra = "select * from tTestTable order by "+sort+" "+order;
        
        DataTable dta = sqla.GetDataTable(stra);
        sqla.SqlClose();

        string json = JsonConvert.SerializeObject(dta);

        context.Response.Write(json);

    }
    public void SearchData(HttpContext context, string username, string loginid)
    {
        context.Response.ContentType = "text/plain";

        SqlHelp sqlb = new SqlHelp();
        string strb = "select * from tTestTable where 1=1";
        if (!string.IsNullOrEmpty(username))
        {
            strb += " and " + "UserName" + " like '%" + username + "%'";
        }
        if (!string.IsNullOrEmpty(loginid))
        {
            strb += " and " + "LoginID" + " like '%" + loginid + "%'";
        }
        strb += " order by LoginID desc";
        DataTable dtb = sqlb.GetDataTable(strb);
        sqlb.SqlClose();

        string jsonb = JsonConvert.SerializeObject(dtb);

        context.Response.Write(jsonb);

    }
    public void DeleteData(HttpContext context, string loginid)
    {
        context.Response.ContentType = "text/plain";
        SqlHelp sqlc = new SqlHelp();
        string strc = "delete from tTestTable where LoginID='"+loginid+"'";
        bool cc = sqlc.ExecuteNonQuery(strc);
        sqlc.SqlClose();
        if (cc)
        {
            SqlHelp sqld = new SqlHelp();
            string strd = "select * from tTestTable";
            DataTable dtd = sqld.GetDataTable(strd);
            sqld.SqlClose();
            string jsond = JsonConvert.SerializeObject(dtd);
            context.Response.Write(jsond);
        }
        
    }
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}

        因为是删除操作,那这里获取到的test == "deletes",所以执行删除函数DeleteData(HttpContext context, string loginid),执行sql返回Json数据。


三、运行结果截图:





你可能感兴趣的:(Jquery,easyui从零单排)