如何获得隐藏的列mRender()函数在jQuery的数据表阿贾克斯数据源值 [英] How to get an hidden column value in mRender() function from ajax data source in jQuery datatable

查看:646
本文介绍了如何获得隐藏的列mRender()函数在jQuery的数据表阿贾克斯数据源值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在asp.net mvc5一个AJAX源创建一个jQuery数据表。
我想补充的编辑一个额外的列,并删除这是不是在我的模型类或AJAX数据source.For编辑和删除功能,我需要的,我没有在我的数据表显示的ID列的值。

I am trying to create a jQuery datatable from a ajax source in asp.net mvc5. I want to add an extra column for edit and delete which is not in my model class or in ajax data-source.For the edit and delete functionality I need the value of the Id column which I have not shown in my datatable.

下面是我的模型类:

public class Country
{
    public int Id { get; set; }
    [Required(ErrorMessage = "Country Code Name Must not be empty")]
    public String Code { get; set; }
    [Required(ErrorMessage = "Country Name Must not be empty")]
    public String Name { get; set; }
    [Required(ErrorMessage = "Template Name Must not be empty")]
    public String Template { get; set; }
    [Required(ErrorMessage = "SPViews Name Must not be empty")]
    public String SPViews { get; set; }
}

下面是我的控制器:

    public ActionResult GetAll(JQueryDataTableParamModel param)
    {
        var countryList = _repository.GetAll().ToList();
        var filteredCountry = (from e in countryList
                               where (param.sSearch == null || e.Name.ToLower().Contains(param.sSearch.ToLower()))
                               select e).ToList();
        var result = from country in filteredCountry.Skip(param.iDisplayStart).Take(param.iDisplayLength)
                     select new[] { country.Id,country.Code, country.Name, country.Template, country.SPViews };

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

这是我的HTML表格:<​​/ P>

here is my html table:

<table id="countryListTable" class="table table-condensed">
<thead>
    <tr>
        <th>Id</th>
        <th>Code</th>
        <th>Name</th>
        <th>Template</th>
        <th>SPViews</th>
        <th>&nbsp;</th>
    </tr>
</thead>
<tbody>
</tbody>
</table>

和最后,这里是我的jQuery code:

and lastly here is my jQuery code:

     var countryTable = $("#countryListTable").dataTable({
            "bServerSide": true,
            "bProcessing": true,
            "sAjaxSource": "/Country/GetAll",
            "aoColumns": [
                null,
                null,
                null,
                null,
                {     // fifth column (Edit link)
                    "mData": "Id",
                    "bSearchable": false,
                    "bSortable": false,
                    "mRender": function (nRow, aData) {
                        //need to get the Id column value
                        return '<a class="glyphicon glyphicon-pencil" href="/Country/Edit/">Edit</a><a class="glyphicon glyphicon-remove" href="/Country/Delete/">Delete</a>';
                    }
                }
            ]
        });

任何帮助将是AP preciated。
问候:)

Any help would be appreciated. Regards :)

推荐答案

@ mg1075感谢您的回复。 fnRender 功能似乎德precated 。我没有尝试的解决方案,但我固定它使用的 mRender 功能的另一种方式。
因此,这里是我的解决方案:

@mg1075 thank you for your reply. fnRender function seems to be deprecated. I did not try your solution, but I fixed it another way using mRender function. So here is my solution:

      countryTable = $("#countryListTable").dataTable({
            "bServerSide": true,
            "bProcessing": true,
            "sAjaxSource": "/Country/GetAll",
            "aoColumns": [
                 { "bVisible": false },
                  null,
                  null,
                  null,
                  null,
                  {
                      mData: 0,//The Id column
                      "bSearchable": false,
                      "bSortable": false,
                      mRender: function (data, type, row) {

                          return '<a class="glyphicon glyphicon-pencil" onclick="editCountry(\'/Country/Edit/' + data + '\');return false;">Edit</a><a class="glyphicon glyphicon-remove" onclick="deleteCountry(\'/Country/Delete/' + data + '\');return false;">Delete</a>';
                      }
                  }],

        });

我觉得这两种方式应该是完美

I think the both approach should be perfect

这篇关于如何获得隐藏的列mRender()函数在jQuery的数据表阿贾克斯数据源值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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