DataTable ParamModel sColumns Null [英] DataTable ParamModel sColumns Null

查看:47
本文介绍了DataTable ParamModel sColumns Null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个带有C#代码的MVC 3网站中使用jQuery库DataTables。



我遇到的问题是JQueryDataTableParamModel param对象返回sColumns的null值。所以我永远不会看到以逗号分隔的列名列表。



当网格的其余部分工作得很好时,为什么它会为空? (分页,排序,过滤=一切正常...)



以下是相关代码,如果您需要了解更多内容,请与我们联系:



查看



<前lang =HTML> < div id = 容器 >
< span class =code-keyword>< div id = demo >
< h2 > 索引< / h2 >
< table id = myDataTable class = 显示 >
< thead >
< tr >
< th > 名字< / th >
< th > 登录名< span class =code-keyword><
/ th >
< th > 模块< span class =code-keyword>< / th >
< / tr >

< tr >
< th > ; < input type = text name = search_name value = 搜索名称 class = search_init / > < / th >
< th > < input 类型 = text name = search_login value = 搜索登录 class = search_init / > < / th >
< th > < input 类型 = text 名称 = search_module value = 搜索模块 class = search_init / > < / th >
< / tr >

< tr >
< th > < < span class =code-leadattribute> / th >
< th > < / th >
< th > < / th >
< / tr >
< / thead >
< tbody >
< / tbody > ;
< / table >
< / div >

< script >
var asInitVals = new Array ();

$( document )。ready( function (){
var oTable = $(' #myDataTable')。dataTable({
bProcessing true
bServerSide true
sAjaxSource' <%= Url.Action(AjaxHandler,DataTables)%>'
});

$( thead input)。keyup(< span class =code-keyword> function (){
/ * 过滤此元素的列(索引)* /
oTable.fnFilter( this .value ,$( thead input)。index( this ));
});



/ *
*支持函数在
*文本框中提供一点用户友好性*页脚
* /

$( thead input)。each( function (i){
asInitVals [i] = .value;
});

$( thead input)。focus( function (){
if this 。 className == search_init){
.className = ;
.value = ;
}
});

$( thead input)。blur( function (i){
if this .value == ){
.className = search_init;
this .value = asInitVals [$( thead input)。 index( this )];
}
});
});
< / script >





控制器



  public  ActionResult AjaxHandler(JQueryDataTableParamModel param)
{
var allUsers = _userService.GetUsers();
IEnumerable< User> filteredUsers = allUsers;

// 列搜索
for int x = 0 ; x < param.iColumns; x ++)
{
if (Convert.ToString(Request [ sSearch _ + x])!= < span class =code-string>)
filteredUsers = FilterResults(x,Convert.ToString(Request [ sSearch _ + x]),filteredUsers);
}

// 主搜索
< span class =code-keyword> if (!String.IsNullOrEmpty(param.sSearch))
filteredUsers = FilterResults(-1,param.sSearch,filteredUsers);

// 这对于列来说很难... ...变得动态...
var sortColumnIndex = Convert.ToInt32(请求[ iSortCol_0\" ]);
Func< User,string> orderingFunction =(c = > sortColumnIndex == 0 ?c.FirstName:sortColumnIndex == 1 ?c.LoginName:c.Module);

var sortDirection =请求[ sSortDir_0]; // asc或desc

if (sortDirection == asc
filteredUsers = filteredUsers。的OrderBy(orderingFunction);
else
filteredUsers = filteredUsers.OrderByDescending(orderingFunction);

var displayedUsers = filteredUsers
.Skip(param.iDisplayStart)
.Take(param.iDisplayLength);

var result = 来自 c in displayedUsers
select new [] {c.FirstName,c。 LoginName,c.Module};

return Json( new
{
sEcho = param.sEcho,
iTotalRecords = allUsers.Count(),
iTotalDisplayRecords = filteredUsers.Count(),
aaData = result
},
JsonRequestBehavior.AllowGet) ;
}

解决方案

document )。ready(< span class =code-keyword> function (){
var oTable =


' #myDataTable')。dataTable({
b处理 true
bServerSide true
sAjaxSource' <%= Url.Action(AjaxHandler,DataTables)%>'
});


thead input)。keyup( function (){
/ * 过滤列(索引)这个元素* /
oTable.fnFilter( this .value,


I am using the jQuery library DataTables in a MVC 3 website with C# code.

The problem I am running into is that the JQueryDataTableParamModel param object comes back with a null value for sColumns. So I never get to see the comma separated list of column names.

Why would that be null when the rest of the grid works great? (paging, sorting, filtering = all work fine...)

Here is the relevant code, let me know if you need to see more:

VIEW

<div id="container">
<div id="demo">
    <h2>Index</h2>
    <table id="myDataTable" class="display">
        <thead>
            <tr>
                <th>First Name</th>
                <th>Login Name</th>
                <th>Module</th>
            </tr>

            <tr>
                <th><input type="text" name="search_name" value="Search Names" class="search_init" /></th>
                <th><input type="text" name="search_login" value="Search Logins" class="search_init" /></th>
                <th><input type="text" name="search_module" value="Search Modules" class="search_init" /></th>
            </tr>

            <tr>
                <th> </th>
                <th> </th>
                <th> </th>
            </tr>
        </thead>
        <tbody> 
        </tbody>
    </table>
</div>

<script>
    var asInitVals = new Array();

    $(document).ready(function () {
        var oTable = $('#myDataTable').dataTable({
            "bProcessing": true,
            "bServerSide": true,
            "sAjaxSource": '<%= Url.Action("AjaxHandler","DataTables") %>'
        });

        $("thead input").keyup(function () {
            /* Filter on the column (the index) of this element */
            oTable.fnFilter(this.value, $("thead input").index(this));
        });



        /*
         * Support functions to provide a little bit of 'user friendlyness' to the textboxes in
         * the footer
         */
        $("thead input").each(function (i) {
            asInitVals[i] = this.value;
        });

        $("thead input").focus(function () {
            if (this.className == "search_init") {
                this.className = "";
                this.value = "";
            }
        });

        $("thead input").blur(function (i) {
            if (this.value == "") {
                this.className = "search_init";
                this.value = asInitVals[$("thead input").index(this)];
            }
        });
    });
</script>



CONTROLLER

public ActionResult AjaxHandler(JQueryDataTableParamModel param)
    {
        var allUsers = _userService.GetUsers();
        IEnumerable<User> filteredUsers= allUsers;

        //column search
        for (int x = 0; x < param.iColumns; x++)
        {
            if (Convert.ToString(Request["sSearch_" + x]) != "")
                filteredUsers = FilterResults(x ,Convert.ToString(Request["sSearch_" + x]), filteredUsers);
        }

        //main search
        if (!String.IsNullOrEmpty(param.sSearch))
            filteredUsers = FilterResults(-1, param.sSearch, filteredUsers);

        //this is hard bound to columns... got to maake dynamic...
        var sortColumnIndex = Convert.ToInt32(Request["iSortCol_0"]);
        Func<User, string> orderingFunction = (c => sortColumnIndex == 0 ? c.FirstName : sortColumnIndex == 1 ? c.LoginName : c.Module);

        var sortDirection = Request["sSortDir_0"]; // asc or desc

        if (sortDirection == "asc")
            filteredUsers = filteredUsers.OrderBy(orderingFunction);
        else
            filteredUsers = filteredUsers.OrderByDescending(orderingFunction);

        var displayedUsers = filteredUsers
                .Skip(param.iDisplayStart)
                .Take(param.iDisplayLength);

        var result = from c in displayedUsers
                     select new[] { c.FirstName, c.LoginName, c.Module };

        return Json(new
        {
            sEcho = param.sEcho,
            iTotalRecords = allUsers.Count(),
            iTotalDisplayRecords = filteredUsers.Count(),
            aaData = result
        },
        JsonRequestBehavior.AllowGet);
    }

解决方案

(document).ready(function () { var oTable =


('#myDataTable').dataTable({ "bProcessing": true, "bServerSide": true, "sAjaxSource": '<%= Url.Action("AjaxHandler","DataTables") %>' });


("thead input").keyup(function () { /* Filter on the column (the index) of this element */ oTable.fnFilter(this.value,


这篇关于DataTable ParamModel sColumns Null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆