【问题标题】:How to dynamically add row to the jquery Datatable and refine the columns to show?如何动态地将行添加到 jquery Datatable 并细化要显示的列?
【发布时间】:2015-09-15 04:16:26
【问题描述】:

我想在 pageload 上的数据表中动态添加行。

我想将来自数据表的 ajax 请求的数据分成两行(如果条件为真,第二行动态添加到该行)。

这个例子应该更清楚。

<table>
<thead>
<tr>
   <td>Name</td>
   <td>City</td>
   <td>Work</td>
   <td>Address</td>
   <td>Pin</td>
   <td style="display:none">Mobile</td>
   <td style="display:none">Email</td>
   <td style="display:none">Profession</td>
</tr>
</thead>
</table>

不显示带有样式属性的 td 而是我想添加另一行来显示这三个字段。

我的问题:

1) 如果满足条件,我应该使用哪个回调来追加一行 (条件是profession == 'IT')然后才在满足条件的那一行下方添加一行。

2) 我如何隐藏整个 4 列数据(我只隐藏标题而不是他的数据。它会添加到表体本身)。

目前我正在使用“aocolumns”来隐藏列,但它不起作用。

它正在添加一个额外的标题,而 tbody 内的数据没有加载。

"aoColumns": [
               {
                 "sName": "Mobile",
                 "sClass": "hidden",
                 "bSortable": false
                },
                 {
                 "sName": "Email",
                 "sClass": "hidden",
                 "bSortable": false
                 },
                   {
                   "sName": "Profession",
                   "sClass": "hidden",
                   "bSortable": false
                   },                                
                 ],

我的隐藏类很简单

.hidden{
display:none;
}

注意:这是服务器端表。

提前谢谢你

编辑:

服务器端代码

     public ActionResult SummaryAjax(JQueryDataTableParamModel param)
            {
     int totalrecords = 0;
                string username = Convert.ToString(Session["userName"]);
                string month = this.Request.QueryString["month"];
                string year = this.Request.QueryString["year"];
                DateTime currentDate = Convert.ToDateTime("2015-09-01");
                if (!string.IsNullOrEmpty(month) && !string.IsNullOrEmpty(year))
                {
                    currentDate = Convert.ToDateTime(year + "-" + month + "-01");
                }
                var objparcelData = db.GetWIPForUserProc(username.ToLower(), currentDate).ToList();

totalrecords = objparcelData.Count();
                if (param.iDisplayLength != -1)
                    objparcelData = (objparcelData.Skip(param.iDisplayStart).Take(param.iDisplayLength)).ToList();
                var resultdata = (from p in objparcelData
                                  select new GetWIPForUserProc_Result
                                  {
                                      Job = p.Job,
                                      JobDescription = p.JobDescription,
                                      Customer = p.Customer,
                                      PreviousContractValue = p.PreviousContractValue,
                                      ContractValue = p.ContractValue,
                                      EstimatedFinalCost = p.EstimatedFinalCost,
                                      EstimatedGrossMargin = p.EstimatedGrossMargin,
                                      CostToDate = p.CostToDate,
                                      PercentComplete = p.PercentComplete,
                                      MarginToDate = p.MarginToDate,
                                      RequisToDate = p.RequisToDate,
                                      ExcessOfCostEarnings = p.ExcessOfCostEarnings,
                                      MarginPercent = p.MarginPercent,
                                      ChangeContractValue = p.ChangeContractValue,
                                      ChangeEstimatedFinalCost = p.ChangeEstimatedFinalCost,
                                      Backlog = p.Backlog,
                                      Add1 = p.Add1,
                                      ApprovCC = p.ApprovCC,
                                      BillAdd1 = p.BillAdd1,
                                      BZCode = p.BZCode,
                                      isManualEntry = p.isManualEntry

                                  }).ToList();

                var result = from p in resultdata
                             select new[] {  Convert.ToString(p.Job), 
                                             Convert.ToString(p.JobDescription),
                                             Convert.ToString(p.Customer),
                                             Convert.ToString(p.PreviousContractValue),
                                             Convert.ToString(p.ContractValue),
                                             Convert.ToString(p.EstimatedFinalCost),
                                             Convert.ToString(p.EstimatedGrossMargin),
                                             Convert.ToString(p.CostToDate),
                                             Convert.ToString(p.PercentComplete),
                                             Convert.ToString(p.MarginToDate),
                                             Convert.ToString(p.RequisToDate),
                                             Convert.ToString(p.ExcessOfCostEarnings),
                                             Convert.ToString(p.MarginPercent),
                                             Convert.ToString(p.ChangeContractValue),
                                             Convert.ToString(p.ChangeEstimatedFinalCost),
                                             Convert.ToString(p.Backlog),
                                             Convert.ToString(p.Add1),
                                             Convert.ToString(p.ApprovCC),
                                             Convert.ToString(p.BillAdd1),
                                             Convert.ToString(p.BZCode),
                                             Convert.ToString(p.isManualEntry)

                                                                        };
}
 return Json(new
                {
                    sEcho = param.sEcho,
                    iTotalRecords = totalrecords,
                    iTotalDisplayRecords = totalrecords,
                    aaData = result,

                },
                JsonRequestBehavior.AllowGet);

【问题讨论】:

    标签: javascript jquery html datatable


    【解决方案1】:

    我先从 2) 开始。数据表中的列是按索引,而不是按名称。您只列出了三列,因此只有前三列将分配一个类——在本例中为“隐藏”类。要隐藏 PIN、Mobile、Email 和 Profession 列,您必须详细说明所有列,如下所示(注意,数字仅表示名称与 id 不对应):

    "aoColumns": [
    { "sName": "0Name", "bSortable": false, "sClass": "" },
    { "sName": "1City", "bSortable": false, "sClass": "" },
    { "sName": "2Work", "bSortable": false, "sClass": "" },
    { "sName": "3Address", "bSortable": true, "sClass": "" },
    
    { "sName": "4Pin", "bSortable": true, "sClass": "hidden" },
    { "sName": "5Mobile", "bSortable": true, "sClass": "hidden" },
    { "sName": "6Email", "bSortable": false, "sClass": "hidden" },
    { "sName": "7Profession", "bSortable": false, "sClass": "hidden" }
    ]
    

    就 1) 而言——您能详细说明一下吗?为什么服务端不根据条件追加行?

    【讨论】:

    • 我不知道我们可以从服务器端追加一行吗?我正在使用 asp.net MVC
    • 您注意到这是一个服务器端表。因此,您的服务器端代码应检查条件,然后在返回 Json 之前向结果集中添加一行。您必须为我发布服务器端代码才能帮助您附加该行,但它应该相当简单。
    • 请注意字段不同,但结果应该相同。只需将任意四个字段附加到新行即可。
    猜你喜欢
    • 2021-01-22
    • 1970-01-01
    • 2012-04-15
    • 2015-02-28
    • 2021-07-01
    • 2015-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多