EasyUI的使用经验——aa

摘要:
恢复1。EasyUI的使用最初需要在两小时内完成。我花了一天时间,而且要走很长的路!让我们来看看今天的收成吧!主要用于添加、删除、修改和查询EasyUI。首先,调用文件然后添加主页使用Jquery加载EasyUI函数ShowAgain的数据{$.datagrid;ViewCode在服务器端编写程序:publicActionResultGetPageList{stringName=Request[“Name”];intid1=string.IsNullOrEmpty?True:u。用户ID==id1),u=>u。UserId、行、页、outtotal);returnJson;}ViewCode备注:GetPageList;EasyUI需要几个参数total,并且返回的数据格式为{total:“”,行:〔{},{}}〕},因此returnJson应该以这种方式编写;它发起的请求是接受上述格式的请求。
恢复

1,EasyUI的使用本来要求两个小时完成,我用了一天,路漫漫啊!

下面来看下今天的收获吧!  

主要是对EasyUI的增删改查

(1)首先是调用文件

   

 <link href="~/Content/easyUI/icon.css" rel="stylesheet" />
    <link href="~/Content/easyUI/default/easyui.css" rel="stylesheet" />
    <script src="~/Scripts/jquery-1.7.1.min.js"></script>
    <script src="~/Scripts/jquery.easyui.min.js"></script>
    <script src="~/Scripts/easyui-lang-zh_CN.js"></script>
    <script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>

(2)然后是添加主页  

 <div>
        <table id="roleList"></table>
    </div>

  使用Jquery加载EasyUI的数据

EasyUI的使用经验——aa第1张EasyUI的使用经验——aa第2张
function ShowAgain(Param) {
        $("#roleList").datagrid({
            url: '@Url.Action("GetPageList","EasyUI2")',
            title: '角色列表',
             700,
            height: 400,
            fitColumns: true,
            idField: 'UserId',
            loadMsg: '正在加载角色的信息...',
            pagination: true,
            singleSelect: false,
            pageSize: 10,
            pageNumber: 1,
            pageList: [10, 20, 30],
            queryParams: Param,
            columns: [[
                    { field: 'ck', checkbox: true, align: 'left',  50 },
                    { field: 'UserId', title: '编号',  80 },
                    { field: 'UserName', title: '用户名',  120 },
                    { field: 'UserAge', title: '年龄',  120 },
                    { field: 'UserGender', title: '性别',  120 }
            ]],
            toolbar: [{
                id: 'toolbarAdd',
                text: '添加',
                iconCls: 'icon-add',
                handler: AddShow
            }, {
                id: 'toolbarEdit',
                text: '修改',
                iconCls: 'icon-edit',
                handler: EditShow
            },
            {
                id: 'toolbarEdit',
                text: '删除',
                iconCls: 'icon-remove',
                handler: DeleteShow
            }]
        });
View Code

编写服务端的程序:

EasyUI的使用经验——aa第3张EasyUI的使用经验——aa第4张
public ActionResult GetPageList(int rows,int page)
        {
            string Name=Request["Name"];
            int id1 = string.IsNullOrEmpty(Request["Id"]) ? 0 : Int32.Parse(Request["Id"]);
            int total;
            var result = bll.GetPageList<int>(u=>(true)&&
                (string.IsNullOrEmpty(Name) ? true:u.UserName.Contains(Name))
                &&
                (id1==0 ? true : u.UserId == id1)
                ,u=>u.UserId,rows,page,out total);
            return Json(new {total,rows=result},JsonRequestBehavior.AllowGet);
        }
View Code

注意:
GetPageList(int rows,int page);EasyUI的需要几个参数,total, 其返回的数据是要求这样的格式{total:'',rows:[{},{}]} 

所以要这样写return Json(new {total,rows=result},JsonRequestBehavior.AllowGet);

其发起的请求是要以上面的格式接受请求。

Id,和后面的u=>(true)&&
                (string.IsNullOrEmpty(Name) ? true:u.UserName.Contains(Name))
                &&
                (id1==0 ? true : u.UserId == id1)是用来查询用的。后面会介绍。

二,增

(1)书写html语句

EasyUI的使用经验——aa第5张EasyUI的使用经验——aa第6张
   <div id="AddList">
        
        @using (Ajax.BeginForm("Add", "EasyUI2", new AjaxOptions() { 
        OnSuccess="AddAfter"
        }))
        {
            <table border="1">
                <tr>
                    <td>姓名:</td>
                    <td>@Html.TextBoxFor(r => r.UserName)</td>
                </tr>
                <tr>
                    <td>性别:</td>
                    <td>@Html.TextBoxFor(r=>r.UserGender)</td>
                </tr>
                <tr>
                    <td>年龄:</td>
                    <td>@Html.TextBoxFor(r => r.UserAge)</td>
                </tr>
            </table>
        }
    </div>
View Code

JQuery在一开始是要先隐藏数据。
$("#AddList").hide();

 当点击时变成显示,并调用函数

EasyUI的使用经验——aa第7张EasyUI的使用经验——aa第8张
function AddShow() {
        $("#AddList").show();
        $("#AddList").dialog({
            title: "添加用户",
            collapsible: true,
            minimizable: true,
            maximizable: true,
            resizable: true,
            buttons: [{
                text: "添加",
                iconCls: "icon-add",
                handler: function () {
                    $("#AddList form").submit();
                }
            }, {
                text: "取消",
                iconCls: "icon-cancel",
                handler: function () {
                    $("#AddList").dialog("close");
                }
            }]
        });

    }
View Code

(2)书写服务器端程序

EasyUI的使用经验——aa第9张EasyUI的使用经验——aa第10张
        public ActionResult Add(UserInfo model)
        {
         
            bool boo= bll.Add(model);
            return Content(boo.ToString());
        }
View Code

(3)添加完成是更新

EasyUI的使用经验——aa第11张EasyUI的使用经验——aa第12张
function AddAfter(msg)
    {
        if (msg == "True") {
            $('#roleList').datagrid('reload');
            $("#AddList").dialog("close");
        }
        else {
            $("#AddList").dialog("close");
            alert("添加失败!");
        }
    }
View Code

三,删除

(1)编写Jquery

function DeleteShow() {
        var items = ''; 
        var id = '';
        items = $('#roleList').datagrid('getSelections');
        $.each(items, function (i, item) {
            id += item['UserId'] + ',';
        });
        $.messager.confirm('提示', '确定要删除吗?', function(msg) {
            id = id.substring(0, id.length - 1);
            $.post("EasyUI2/Delete?a="+Date().toString(), { idList: id}, function (msg) {
                if (msg == "True") {
                    location.reload();
                    id = '';
                } else {
                    alert("删除失败!");
                }
            })  
        })
    }

(1)首先调用getSelections方法获取所有的选中项

(2)根据获取的数据组合成数组并发送到服务器

(3)编写服务器代码

        public ActionResult Delete(string idList)
        {
            bool boo=false;
            //idList
            List<int> list = new List<int>();
            string[] ids = idList.Split(',');
           
            foreach (var id in ids)
            {
                list.Add(int.Parse(id));
            }
            boo = bll.DelByIds(list.ToArray());
           return Content(boo.ToString());
        }

 服务器的底层是用OA写的。DelByIds是这么写的

 public bool DelByIds(int[] id)
        {
            int a = 0;

            for (int i = 0; i < id.Length; i++)
            {
              bool boo= DelById(id[i]);
              if (boo) a++;
            }
            if (a != id.Length) return false ;
            return true;
        }


四,改

html

 <!--修改-->
    <div id="Edit">
        <iframe src="#"></iframe>
    </div>

jquery
注意:一开始时也要隐藏,点击时显示

  function EditShow() {
        $("#Edit").show();
        var items = $('#roleList').datagrid('getSelections');//获取选择的项目,当大于1时提示用户
        if (items.length != 1) {
            $.messager.alert('警告', "请选择一条要修改的记录");
            return;
        }
        var id = items[0]["UserId"];

        //修改src属性,指向修改页面
        $('#Edit iframe').attr("src", '@Url.Action("Edit","EasyUI2")?id1=' + id);
        $("#Edit").dialog({
            title: "修改用户",
            collapsible: true,
            minimizable: true,
            maximizable: true,
            resizable: true,

            buttons: [{
                text: "修改",
                iconCls: "icon-edit",
                handler: function () {
                    $('#Edit iframe')[0].contentWindow.EditSubmit();
                }
            }, {
                text: "取消",
                iconCls: "icon-cancel",
                handler: function () {
                    $("#Edit").dialog("close");
                }
            }]
        });
    }

这段程序的关键是前面的调用获取选择的项目,

还有就是后面的调用点击修改时 $('#Edit iframe')[0].contentWindow.EditSubmit();调用Edit页面中的Jquery函数EditSubmit();

(2)Edit页面

EasyUI的使用经验——aa第13张EasyUI的使用经验——aa第14张
@model Model.UserInfo
@{
    ViewBag.Title = "Edit";
}
<script src="~/Scripts/jquery-1.7.1.min.js"></script>
<script src="~/Scripts/jquery.easyui.min.js"></script>
<script src="~/Scripts/jquery.unobtrusive-ajax.js"></script>
<link href="~/Content/easyUI/default/tabs.css" rel="stylesheet" />
<link href="~/Content/easyUI/default/easyui.css" rel="stylesheet" />
<link href="~/Content/easyUI/icon.css" rel="stylesheet" />
<script>
    function EditSubmit() {
        $('form').submit();
    }
    function EditAfter(msg) {
        window.parent.EditAfter(msg);
    }
</script>
<h2>Edit</h2>
@using (Ajax.BeginForm("Edit", "EasyUI2", new AjaxOptions()
{
    OnSuccess = "EditAfter"
}))
{
    <table border="1">
        <tr>
            <td>编号:</td>
            <td>@Html.TextBoxFor(u => u.UserId)</td>
        </tr>
        <tr>
            <td>姓名:</td>
            <td >@Html.TextBoxFor(u=>u.UserName)</td>
        </tr>
        <tr>
            <td>性别:</td>
            <td>@Html.TextBoxFor(u => u.UserGender)</td>
        </tr>
        <tr>
            <td>年龄:</td>
            <td>@Html.TextBoxFor(u => u.UserAge)</td>
        </tr>
    </table>
}
View Code

(3)服务器代码

 public ActionResult Edit()
        {
            int id= String.IsNullOrEmpty(Request["id1"])? 0:Int32.Parse(Request["id1"]);
            var result = bll.GetById(id);
            return View(result);
        }
        [HttpPost]
        public ActionResult Edit(UserInfo model)
        {
            bool boo = false;
            if (model != null)
            {
                boo = bll.Edit(model);
            }
            return Content(boo.ToString()); 
        }

第一个Edit是一开始点击修改时加载数据。
第二个是当数据修改完成时,提交修改数据。

五,查

(1)html

<!--搜索-->
    <div id="Select">
        <table border="1">
            <tr>
               <td>用户名:</td><td><input id="Name" type="text" /></td>
                <td>编号:</td><td><input id="Id" type="text" /></td>
                <td><input type="button" onclick="Select()" value="搜索" /></td>
            </tr>
        </table>
        
    </div>

使用最简单的table方式加载数据。
(2)Jquery

获取数据并传入”一“(最上面的程序),的函数中

 function Select() {
        
            //获取查询数据
            var Name1 = $("#Name").val();
            var Id1 = $("#Id").val();
            //向服务器请求
            ShowAgain({
                'Id': Id1,
                'Name': Name1
            });
            
    }

服务器:

public ActionResult GetPageList(int rows,int page)
        {
            string Name=Request["Name"];
            int id1 = string.IsNullOrEmpty(Request["Id"]) ? 0 : Int32.Parse(Request["Id"]);
            int total;
            var result = bll.GetPageList<int>(u=>(true)&&
                (string.IsNullOrEmpty(Name) ? true:u.UserName.Contains(Name))
                &&
                (id1==0 ? true : u.UserId == id1)
                ,u=>u.UserId,rows,page,out total);
            return Json(new {total,rows=result},JsonRequestBehavior.AllowGet);
        }

获取数据并根据数据查询
(4)完成之后

function SelectAfter() {
        $('#roleList').datagrid('reload');
    }


完成的属于你自己的EasyUI


 

免责声明:文章转载自《EasyUI的使用经验——aa》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇【Android】使用属性动画碰到的困惑及讲解layer.open获取弹出层的input框的值下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Jquery获取列表中的值和input单选、多选框控制选中与取消

一、Jquery获取列表中的值   1、jsp页面代码    <tbody> <c:forEach var="model" items="${listRefEntity }" varStatus="status"> <tr&...

bootstrap 3 之 表格(table的各种样式)

1、table-striped:斑马线表格 2、table-bordered:带边框的表格 3、table-hover:鼠标悬停高亮的表格 4、table-condensed:紧凑型表格(单元格的内距由8px调至5px。) 5、table-responsive:响应式表格(当你的浏览器可视区域小于768px时,表格底部会出现水平滚动条。当你的浏览器...

Pandas dataframe数据写入文件和数据库

 Pandas是Python下一个开源数据分析的库,它提供的数据结构DataFrame极大的简化了数据分析过程中一些繁琐操作,DataFrame是一张多维的表,大家可以把它想象成一张Excel表单或者Sql表。之前这篇文章已经介绍了从各种数据源将原始数据载入到dataframe中,这篇文件介绍怎么将处理好的dataframe中的数据写入到文件和数据库中。 ...

CSS 固定table 表头和列

<html><head>    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">    <title>固定表头和列</title>    <style>        .FixedTitleRow ...

C#生成简单验证码

我们平时无论是网站登录还是注册,都会频繁的遇到各式各样的验证码 ,其实生成验证码对于C#来说非常简单。 下面就是我学习生成验证码的简单实例。 封装的辅助类代码,如下: 1 using System; 2 using System.Collections.Generic; 3 using System.Drawing; 4 using System...

一道DOM操作题

创建一个表格,表头为姓名,性别,年龄,班级 1. 点击单元格内的内容,弹窗输入值,修改单元格内原有数据; 2. 设置加粗,标红按钮,在修改完单元格内容后显示按钮并能够对修改的内容进行样式改变; 3. 设置添加按钮,点击添加一行新的单元格(4个); 代码如下: 1 <!doctype html> 2 <html> 3 <h...