【问题标题】:Set column to show 0 in JqGrid将列设置为在 JqGrid 中显示 0
【发布时间】:2016-07-06 23:01:33
【问题描述】:

我有一个 jqGrid。 我的功能是——

   $("#grid").jqGrid({
      url: "/Log/GetLogs",
    datatype: 'json',
    mtype: 'Get',
    colNames: ['LogID', 'Agency Billing Code', 'License    Number', 'Equipement Number', 'Year', 'Make', 'Model', 'Color', 'Begin Miles', 'End Miles'],
    colModel: [
        { key: true, hidden: true, name: 'ID', index: 'ID' },
                            { key: false, name: 'Year', index: 'Year', editable: false },
        { key: false, name: 'Make', index: 'Make', editable: false },
        { key: false, name: 'Model', index: 'Model', editable: false },
        { key: false, name: 'Color', index: 'Color', editable: false },
                { key: false, name: 'Miles', index: 'Miles', editable: true, cellvalue: "" },
    ],
      pager: jQuery('#pager'),
    rowNum: 10,
    rowList: [10, 20, 30, 40],
    height: '100%',
    viewrecords: true,
    caption: 'Log List',
    emptyrecords: 'No Records',
    jsonReader: {
        root: "rows",
        page: "page",
        total: "total",
        records: "records",
        repeatitems: false,
        id: "0"
    },
    autowidth: true,
    multiselect: false

});

当网格加载时,数据库返回“Miles”的值,但我不想显示。我只想显示 0,当我编辑英里值时,它应该映射到我的对象中的英里。 请让我知道我该如何实现? 谢谢..

【问题讨论】:

  • @Oleg 可以帮助您解决这个问题
  • 您能否提供一个用于填充网格的输入数据示例?我的意思是url: "/Log/GetLogs"返回的一项数据?你真的使用repeatitems: false格式的数据吗?一般来说,使用colModel 中的jsonmap 属性可以很容易地解决您的问题。如果你真的使用repeatitems: false 格式的数据,那么列中的jsonmap: function () { return 0; } 会解决你的问题,但是jsonReader 的属性id: "0" 似乎很奇怪。您使用哪个版本的 jqGrid 以及来自哪个分支的 jqGrid?

标签: jqgrid


【解决方案1】:

要完成您需要的内容,您需要为英里列设置格式化程序和取消格式化程序。您尚未指定如何编辑网格行(内联、表单编辑、自定义...等),但我为您创建了内联编辑作为示例。

如果你想玩的话,这里是jsfiddle 中的完整解决方案。编辑只需单击该行,英里将在编辑时显示其原始值,但在非编辑模式下显示 0。有关格式化工作原理的更多详细信息,请参阅 Here

var mileformatter= function (cellval, options, rowObject) {
   return "<span data-val='"+cellval+"'>0</span>";
    }

    var mileUnFormat= function (cellvalue, options, cell) {
          return  $('span', cell).attr('data-val');
    }
            "use strict";
            var mydata = [
                    {ID:"1", Year: "1933", Make: "Nissan",Model:"Model1", Color: "White",Miles:1222},
                    {ID:"2", Year: "2008", Make: "Toyota",Model:"Model2" , Color: "Gray",Miles:3000},
                ];
            $("#list").jqGrid({
      data: mydata,
    datatype: "local",
    mtype: 'Get',
    colModel: [
        { key: true, hidden: true, name: 'ID', index: 'ID' },
         { key: false, name: 'Year', index: 'Year', editable: false },
        { key: false, name: 'Make', index: 'Make', editable: false },
        { key: false, name: 'Model', index: 'Model', editable: false },
        { key: false, name: 'Color', index: 'Color', editable: false },
       { key: false, formatter:mileformatter,unformat:mileUnFormat, name: 'Miles', index: 'Miles', editable: true, cellvalue: "" },
    ],
      pager: jQuery('#pager'),
    rowNum: 10,
    rowList: [10, 20, 30, 40],
    height: '100%',
    viewrecords: true,
    caption: 'Log List',
    emptyrecords: 'No Records',
    jsonReader: {
        root: "rows",
        page: "page",
        total: "total",
        records: "records",
        repeatitems: false,
        id: "0"
    },
    autowidth: true,
    multiselect: false,
    onSelectRow: function (id) {

             jQuery('#list').editRow(id, true);
             }


});

这是您正在寻找的内容的编辑答案和一个新的jsfiddle 链接请注意,我删除了 unformatter 并添加了 beforeSaveRow 函数。

var onEdit=false;

var mileformatter= function (cellval, options, rowObject) {
if(onEdit==true)
{
return cellval;
onEdit=false;
}
   return 0;
    }

            "use strict";
            var mydata = [
                    {ID:"1", Year: "1933", Make: "Nissan",Model:"Model1", Color: "White",Miles:1222},
                    {ID:"2", Year: "2008", Make: "Toyota",Model:"Model2" , Color: "Gray",Miles:3000},
                ];
            $("#list").jqGrid({
      data: mydata,
    datatype: "local",
    mtype: 'Get',
    colModel: [
        { key: true, hidden: true, name: 'ID', index: 'ID' },
         { key: false, name: 'Year', index: 'Year', editable: false },
        { key: false, name: 'Make', index: 'Make', editable: false },
        { key: false, name: 'Model', index: 'Model', editable: false },
        { key: false, name: 'Color', index: 'Color', editable: false },
       { key: false, formatter:mileformatter, name: 'Miles', index: 'Miles', editable: true, cellvalue: "" },
    ],
      pager: jQuery('#pager'),
    rowNum: 10,
    rowList: [10, 20, 30, 40],
    height: '100%',
    viewrecords: true,
    caption: 'Log List',
    emptyrecords: 'No Records',
    jsonReader: {
        root: "rows",
        page: "page",
        total: "total",
        records: "records",
        repeatitems: false,
        id: "0"
    },
    autowidth: true,
    multiselect: false,
    onSelectRow: function (id) {

             jQuery('#list').editRow(id, 
          {
                "keys": true,          
                oneditfunc: function () {

                },

                "successfunc": function () {
               alert('successfunc');
                },
                "url": null,
                "extraparam": {},
                "aftersavefunc": function () {
               alert('aftersavefunc');
                },
                "errorfunc": null,
                "afterrestorefunc": null,
                "restoreAfterError": true,
                "beforeSaveRow": function (options, rowid) {
                onEdit=true;
              jQuery("#list").saveRow(id, false);

                   return false;

                }
                });

       }


});

您可以如下自定义SaveRow,并放置您自己的post url。

 saveparameters = {
        "successfunc" : null,
        "url" : "yoururl",
            "extraparam" : {},
        "aftersavefunc" : null,
        "errorfunc": null,
        "afterrestorefunc" : null,
        "restoreAfterError" : true,
        "mtype" : "POST"
    }

    jQuery("#grid_id").jqGrid('saveRow',rowid,  saveparameters);

【讨论】:

  • 谢谢...这行得通。加载网格时,但是当我单击以编辑值时。它显示数据库的值。我不想显示那个。有没有办法做到这一点。当我点击单元格时,它仍然会显示 0 而不是数据库值?
  • 这不是你说的“当我编辑里程值时,它应该映射到我的对象中的里程”?你能告诉我更多吗?您什么时候想使用数据库值?让它更清楚,然后我会更新我的答案。
  • 在屏幕上我不想要数据库值。当用户输入以英里为单位的值时,我将保存,它应该与数据库中的 Miles 列映射。
  • 所以无论如何,网格总是以英里为单位显示 0?还是应该在最初加载时显示 0 但是当用户编辑英里单元格时,它应该将其保存到 db 并且应该显示英里的编辑值?
  • 所以当网格首先加载时,它会显示 0 ,然后当用户输入值时,它应该保留输入的值,当用户点击保存时,这个值应该保存在数据库中的英里列中。当前网格的列在加载数据时显示为0,当我单击编辑它时,它显示数据库值。我不想在单击进行编辑时显示该值。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-23
  • 1970-01-01
  • 2020-09-16
  • 2012-12-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多