HTML5技术

用DapperExtensions和反射来实现一个通用搜索 - yt1983(2)

字号+ 作者:H5之家 来源:H5之家 2017-03-09 08:00 我要评论( )

public class Predicate{ 列名 ColumnItem { get ; set ; } 操作符 OperatorItem { get ; set ; } 值 Value { get ; set ; }} 4.然后通过反射Account类的属性加载到前台列名的DropDownList,再增加一个操作符的Drop

public class Predicate { 列名 ColumnItem { get; set; } 操作符 OperatorItem { get; set; } Value { get; set; } }

4.然后通过反射Account类的属性加载到前台列名的DropDownList,再增加一个操作符的DropDownList

var columnItems = new List<SelectListItem>(); //通过反射来获取类的属性 //Type t = Assembly.Load().GetType();
       Type t = typeof(SearchDemo.Models.Account);   
foreach (PropertyInfo item in t.GetProperties()) { string filedName = (item.GetCustomAttributes(typeof(MarkAttribute), false)[0] as MarkAttribute).FiledName; columnItems.Add(new SelectListItem() { Text = filedName, Value = item.Name }); } ViewBag.columnItems = columnItems; var operatorItems = new List<SelectListItem>() { , Value = }, , Value = }, , Value = }, , Value = }, , Value = }, , Value = } }; ViewBag.operatorItems = operatorItems;

 5.前台界面实现代码

<!DOCTYPE html>
<html>
<head>
    <title>DapperExtensions通用搜索</title>
    <script src="../../Scripts/jquery-1.4.4.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        Date.prototype.format = function (format) {
            var o = {
                "M+": this.getMonth() + 1, //month   
                "d+": this.getDate(), //day   
                "h+": this.getHours(), //hour   
                "m+": this.getMinutes(), //minute   
                "s+": this.getSeconds(), //second   
                "q+": Math.floor((this.getMonth() + 3) / 3), //quarter   
                "S": this.getMilliseconds() //millisecond   
            }

            if (/(y+)/.test(format)) {
                format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
            }

            for (var k in o) {
                if (new RegExp("(" + k + ")").test(format)) {
                    format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
                }
            }
            return format;
        }  
    </script>
    <style type="text/css">
        ul
        {
            list-style: none;
            padding: 0px;
            margin: 0px;
            width: 590px;
            height: 20px;
            line-height: 20px;
            border: 1px solid #99CC00;
            border-top: 0px;
            font-size: 12px;
        }
        ul li
        {
            display: block;
            width: 25%;
            float: left;
            text-indent: 2em;
        }
        .th
        {
            background: #F1FADE;
            font-weight: bold;
            border-top: 1px solid #99CC00;
        }
    </style>
    <script type="text/javascript">
        var predicates = [];
        var index = 0;
        $(document).ready(function () {
            $("#btnAdd").click(function () {
                var columnItem = $("#columnItems option:selected");
                var operatorItem = $("#operatorItems option:selected");
                var value = $("#value").val();
                if(value == ""){
                    alert("请输入值");
                    return;
                }
                var predicate = { index: index, columnItem: columnItem.val(), operatorItem: operatorItem.val(), value: value };
                predicates.push(predicate);
                var html = "<ul><li>" + columnItem.text() + "</li><li>" + operatorItem.text() + "</li><li>" + value + "</li><li><a href='javascript:;'>删除</a></li></ul>"
                $("#predicates ul:last").after(html);
                index++;
            })

            $("#btnSearch").click(function () {
                $.ajax({
                    type: "POST",
                    url: "home/search",
                    data: JSON.stringify(predicates),
                    contentType: "application/json",
                    success: function (data) {
                        if (data.Error != null) {
                            alert(data.Error);
                            return;
                        }
                        $("#list .th").nextAll().remove();
                        var html = "";
                        $.each(data, function (index, item) {
                            html += "<ul><li>" + item.AccountId + "</li>";
                            html += "<li>" + item.RealName + "</li>";
                            html += "<li>" + item.Age + "</li>";
                            //转换日期
                            var dateMilliseconds = parseInt(item.CreateTime.replace(/\D/igm, ""));
                            var date = new Date(dateMilliseconds);
                            html += "<li>" + date.format("yyyy-MM-dd hh:mm:ss") + "</li></ul>";
                        });
                        $("#list .th").after(html);
                    }
                });
            })
        })

        function del(obj,index) {
            obj.parentNode.parentNode.remove();
            for (var i = 0; i < predicates.length; i++) {
                if (predicates[i].index == index) {
                    predicates.splice(i, 1);
                }
            }
        }
    </script>
</head>
<body>
    <div>
        列名:@Html.DropDownList("columnItems")&nbsp;&nbsp;操作符:@Html.DropDownList("operatorItems")&nbsp;&nbsp;值:@Html.TextBox("value")&nbsp;&nbsp;
        <input type="button" value="增加" />&nbsp;&nbsp;<input type="button" value="搜索" />
    </div>
    <br />
    <div>
        <ul>
            <li>列名</li>
            <li>操作符</li>
            <li>值</li>
            <li>操作</li>
        </ul>
    </div>
    <br />
    <div>
        <ul>
            <li>账户ID</li>
            <li>姓名</li>
            <li>年龄</li>
            <li>创建时间</li>
        </ul>    
    </div>
</body>
</html>

 6.最后通过DapperExtensions的谓词和反射实现搜索方法

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • FormData+Ajax 实现多文件上传 学习使用FormData对象 - 天青色的西瓜莹

    FormData+Ajax 实现多文件上传 学习使用FormData对象 - 天青色的西瓜

    2017-03-08 18:01

  • css3实现圆角边框渐变 - 就只是小茗

    css3实现圆角边框渐变 - 就只是小茗

    2017-03-08 15:00

  • [前言] 实现一个Android电子书阅读APP - xfangs

    [前言] 实现一个Android电子书阅读APP - xfangs

    2017-03-04 09:03

  • HTML5动画(二):Canvas 实现圆形进度条并显示数字百分比 - 孟然

    HTML5动画(二):Canvas 实现圆形进度条并显示数字百分比 - 孟然

    2017-02-23 14:05

网友点评