简体   繁体   English

如何将行动态添加到 jquery 数据表并细化要显示的列?

[英]How to dynamically add row to the jquery Datatable and refine the columns to show?

I want to add row dynamically in the datatable on pageload .我想添加一行动态在页面加载的数据表。

i want to split the data coming from the ajax request of datatable into two rows(second row added dynamically to the row if the condition is true).我想将来自数据表的 ajax 请求的数据拆分为两行(如果条件为真,则将第二行动态添加到行中)。

here is the example which should make it more clear.这是一个应该让它更清楚的例子。

<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>

The td with style attribute are not to be shown instead i want to add another row to show these three field.不显示带有样式属性的td ,而是我想添加另一行来显示这三个字段。

My Question:我的问题:

1) which callback should i use to append a row in case a condition is met (Condition is profession == 'IT' ) only then add a row below that row whose condition is met. 1)如果满足条件(条件是职业=='IT' ),我应该使用哪个回调来附加一行,然后才在满足条件的行下方添加一行。

2) How i can hide the whole 4 column data (i am hiding only header but not he data. it will add to the table body itself). 2)我如何隐藏整个 4 列数据(我只隐藏标题而不隐藏他的数据。它会添加到表体本身)。

Currently i am using "aocolumns" to hide column but it is not working.目前我正在使用“aocolumns”来隐藏列,但它不起作用。

it is adding an extra header and data inside the tbody is not loading.它正在添加一个额外的标题,并且 tbody 中的数据未加载。

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

My hidden class is simple我的隐藏课很简单

.hidden{
display:none;
}

Note: This is server side table.注意:这是服务器端表。

Thank you in advance先感谢您

EDIT:编辑:

server Side Code服务器端代码

     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);

I'll start with 2) first.我将首先从 2) 开始。 The columns in datatables are by index, not by name.数据表中的列是按索引,而不是按名称。 You have only three columns listed, so only the first three columns will have a class assigned to them -- in this case, the "hidden" class.您只列出了三列,因此只有前三列会分配给它们一个类——在本例中为“隐藏”类。 To hide the PIN, Mobile, Email and Profession columns, you must detail all the columns, as the following shows (note, the numbers are only to show that the names don't correspond with the ids):要隐藏 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" }
]

As far as 1) goes -- can you elaborate a little?就 1) 而言——你能详细说明一下吗? Why doesn't the server side append the row based on the condition?为什么服务器端不根据条件追加行?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM