Jquery datagrid动态添加列

后端

        /// <summary>
        /// SpecificQjValue(格式:1,2) 改为 1 as SpecificQjValue0,2 as SpecificQjValue1格式
        /// </summary>
        /// <param name="SpecificQjValue"></param>
        /// <returns></returns>
        public string GetQjValue(string SpecificQjValue)
        {
            int i = 0;
            string strQjValue = string.Empty;
            SpecificQjValue.Split(',').ToList().ForEach(x =>
            {
                strQjValue = (string.IsNullOrEmpty(strQjValue)) ? x + " as SpecificQjValue" + i : 
                                     strQjValue + "," + x + " as SpecificQjValue" + i;
                i++;
            });
            return strQjValue;
        }
        ///获取详情
        public JsonModelInfoList<JsonSampleAcceptQjMxInfo> GetSampleAccepQjMxInfo(int Id)
        {
            string sql = string.Empty;
            List<JsonSampleAcceptQjMxInfo> rowQj = DapperHelper.Select<JsonSampleAcceptQjMxInfo>(TargetDB.Target,
                    "select * from SampleAcceptQjMxInfo where SampleAcceptQjId = " + Id, null);
            rowQj.ForEach(x =>
            {
                string strQjValue = GetQjValue(x.SpecificQjValue);
                string sql1 = "select  m.CreateTime,m.BatchNum,m.VolumeNum,m.ResultOk, " + strQjValue +
                      ",(select MakeName from SampleAcceptQjInfo where Id = m.SampleAcceptQjId ) as MakeName," +
                       "(select AdmitName from SampleAcceptQjInfo where Id = m.SampleAcceptQjId ) as AdmitName," +
                       "stuff((select ','+ SpecQjColName from SpecificQjInfo t where SpecificMxId = m.SpecificMxId 
                       order by Id  for  xml path('')), 1, 1, '') as SpecQjColName
                       from SampleAcceptQjMxInfo m where m.Id = " + x.Id;
                sql = (string.IsNullOrEmpty(sql)) ? sql1 : sql + " union all " + sql1;
            });
            
            List<JsonSampleAcceptQjMxInfo> rows = DapperHelper.Select<JsonSampleAcceptQjMxInfo>(TargetDB.Target, sql, null);
            // 总数
            int total = rows.Count;
            List<JsonSampleAcceptQjMxInfo> temp = rows;
            return new JsonModelInfoList<JsonSampleAcceptQjMxInfo> { total = total, rows = rows};
        }

原先查询结果
在这里插入图片描述
以上后端代码运行后最终查询结果:SpecificQjValue可以为多个 不固定 动态的
在这里插入图片描述一般处理程序

  public void GetSampleAccepQjMxInfo()
        {
            int Id = Convert.ToInt32(Context.Request["Id"].Trim());
            //int pageSize = Convert.ToInt32(Context.Request["rows"].ToString());
            //int pageIndex = Convert.ToInt32(Context.Request["page"].ToString());
            JsonModelInfoList<JsonSampleAcceptQjMxInfo> datasource = bll.GetSampleAccepQjMxInfo(Id);
            int i = 0;
            int colCount = 6 + datasource.rows[0].SpecQjColName.Split(',').Length;//固定列+动态列
            string jsonCol = ",\"totalCol\":"+ colCount + ",\"columns\":[{\"field\":\"CreateTime\",\"title\":\"检查日期\"},      {\"field\":\"BatchNum\",\"title\":\"批号\"},{\"field\":\"VolumeNum\",\"title\":\"卷号\"}";
            datasource.rows[0].SpecQjColName.Split(',').ToList().ForEach(x =>
            {
                jsonCol += ",{\"field\":\"SpecificQjValue"+i+"\",\"title\":\"" + x + "\"}";
                i++;
            });
            jsonCol += ",{\"field\":\"ResultOk\",\"title\":\"判定\"},{\"field\":\"MakeName\",\"title\":\"检查员\"},{\"field\":\"AdmitName\",\"title\":\"承认\"}]}";
            string jsonRow = JsonConvert.SerializeObject(datasource);
            string json = jsonRow.Substring(0, jsonRow.Length - 1) + jsonCol;
            Context.Response.ContentType = "json";
            Context.Response.Write(json);
        }

前端获取的Json格式:

{
"total":1,
"rows":[{"Id":6,"QjNo":"1","TicketName":"666666PC","ManageProject":"1"},{"Id":7,"QjNo":"777","TicketName":"77777PC","ManageProject":"7"}],
"totalCol":1,
"columns":[{"field":"1111","title":"1111"},{"field":"2222","title":"2222"}]
}
$(function () {
$.ajax({
   type: "post", 
   url: '',
   data: { Id: $("#hidID").val() },
   dataType: "json", 
   async: false,
   cache: false,
   error: function (x, e) {return true;},
   success: function (data) 
   if (data.total > 0) {
       var successData={
            total:data.total,
            rows:data.rows
      };
    var arrays = [];
    var columnsArray = [];
    for (var i = 0; i < data.totalCol; i++) { arrays.push({ field: '', title: '', width: '' }); }
    columnsArray.push(arrays);//[[]]形式  
     $(data.columns).each(function(index,value){ 
       columnsArray[0][index]['field'] = value.field;
       columnsArray[0][index]['title'] = value.title;
      columnsArray[0][index]['width'] = "100";  
       });
       //赋值      
       $('#mygrid').datagrid({  
          columns:columnsArray,  
           data: successData  
       });  
     }      
   }
 });
 })
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在EasyUI的DataGrid中,我们可以通过定义的formatter函数来实现根据不同的值来禁用某些单元格。具体来说,我们可以在formatter函数中返回一个`<span>`标签,并给它添加一个`disabled`属性。然后,我们可以使用jQuery的`each()`函数来遍历DataGrid的所有行和,并找到我们想要禁用的单元格,并将它的`<span>`标签设置为禁用状态。 下面是一个实现的示例代码: ```javascript // 定义DataGrid的columns var columns = [[ {field:'name',title:'Name',width:100}, {field:'age',title:'Age',width:100,formatter: function(value,row,index){ if (value > 18){ // 如果年龄大于18,则禁用单元格 return '<span disabled="disabled">'+value+'</span>'; } else { return value; } }}, {field:'gender',title:'Gender',width:100,formatter: function(value,row,index){ if (value === 'Female'){ // 如果性别为女性,则禁用单元格 return '<span disabled="disabled">'+value+'</span>'; } else { return value; } }} ]]; // 在DataGrid添加数据后,禁用满足条件的单元格 $('#dg').datagrid('appendRow',{ name:'John', age:20, gender:'Male' }).datagrid('appendRow',{ name:'Jane', age:16, gender:'Female' }).datagrid('getPanel').find('span[disabled="disabled"]').each(function(){ $(this).closest('td').css('opacity', '0.5'); }); ``` 在上面的代码中,我们定义了一个包含三DataGrid,分别是`Name`、`Age`和`Gender`。在`Age`和`Gender`的formatter函数中,我们判断单元格的值是否满足某些条件,如果满足,则返回一个禁用状态的`<span>`标签。 接着,我们向DataGrid添加了两行数据,并且使用`getPanel()`函数获取DataGrid的面板,然后使用`find()`函数查找所有禁用状态的`<span>`标签,并使用`each()`函数遍历它们,并将它们的父元素`<td>`的不透明度设置为0.5,从而禁用它们。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值