在jqxGrid中,我如何区分具有数据的行和长度为0的行?

时间:2015-06-29 21:26:33

标签: jquery jqxgrid

我有一个包含n行的网格。此网格包含一个复选框列,此复选框允许最终用户将行添加或不添加到数据库。我的问题是我的代码添加了一切。可行的数据和空行。如何判断该行是否长度为0?

网格代码

    var data = {};

    var source = {
      localdata: data,
      datatype: "array",
      datafields: [{
        name: 'UOMRelatedUnit_ID',
        type: 'string'
      }, {
        name: 'UOMRelatedUnit_AddItem',
        type: 'bool'
      }, {
        name: 'UOMRelatedUnit_Name',
        type: 'string'
      }, {
        name: 'UOMRelatedUnit_Abbreviation',
        type: 'string'
      }, {
        name: 'UOMRelatedUnit_ConversionOfBaseUnits',
        type: 'number'
      }],
      addrow: function(rowid, rowdata, position, commit) {
        //Server Action
        commit(T);
      },
      updaterow: function(rowid, newdata, commit) {
        //Server Action
        commit(T);
      }
    };
    var dataAdapter = new $.jqx.dataAdapter(source);

    $("#jqxUOMRelatedUnitsDropdownGrid").jqxGrid({
      width: 500,
      height: 200,
      source: dataAdapter,
      editable: T,
      selectionmode: 'singlecell',
      theme: 'energyblue',
      showtoolbar: T,
      rendertoolbar: function(toolbar) {
        var me = this;
        var container = $("<div style='margin: 5px;'></div>");
        toolbar.append(container);
        container.append('<input id="addUoMRelatedUnitsRowButton" type="button" value="Add New Row" />');
        container.append('<input style="margin-left: 5px;" id="addUoMRelatedUnitsMultipleRowsButton" type="button" value="Add Multiple New Rows" />');
        $("#addUoMRelatedUnitsRowButton").jqxButton();
        $("#addUoMRelatedUnitsMultipleRowsButton").jqxButton();

        // create new row.
        $("#addUoMRelatedUnitsRowButton").on('click', function() {
          $("#jqxUOMRelatedUnitsDropdownGrid").jqxGrid('beginupdate');
          var GridObject = ['']
          var commit = $("#jqxUOMRelatedUnitsDropdownGrid").jqxGrid('addrow', null, GridObject);
          $("#jqxUOMRelatedUnitsDropdownGrid").jqxGrid('endupdate');
        });

        // create new rows.
        $("#addUoMRelatedUnitsMultipleRowsButton").on('click', function() {
          $("#jqxUOMRelatedUnitsDropdownGrid").jqxGrid('beginupdate');
          for (var i = 0; i < 5; i++) {
            var GridObject = ['']
            var commit = $("#jqxUOMRelatedUnitsDropdownGrid").jqxGrid('addrow', null, GridObject);
          }
          $("#jqxUOMRelatedUnitsDropdownGrid").jqxGrid('endupdate');
        });
      },
      columns: [{
        text: '',
        editable: F,
        datafield: 'UOMRelatedUnit_ID',
        width: 0
      }, {
        text: 'Add',
        editable: T,
        datafield: 'UOMRelatedUnit_AddItem',
        columntype: 'checkbox',
        width: 40
      }, {
        text: 'Name',
        editable: T,
        datafield: 'UOMRelatedUnit_Name',
        columntype: 'textbox',
        width: 200
      }, {
        text: 'Abbreviation',
        editable: T,
        datafield: 'UOMRelatedUnit_Abbreviation',
        columntype: 'textbox',
        width: 100
      }, {
        text: '# of Base Unit',
        editable: T,
        datafield: 'UOMRelatedUnit_ConversionOfBaseUnits',
        columntype: 'textbox',
        width: 100
      }]
    });

     // select or unselect rows when the checkbox is checked or unchecked.
    $("#jqxUOMRelatedUnitsDropdownGrid").bind('cellendedit', function(event) {
      if (event.args.value) {
        $("#jqxUOMRelatedUnitsDropdownGrid").jqxGrid('selectrow', event.args.rowindex);
      } else {
        $("#jqxUOMRelatedUnitsDropdownGrid").jqxGrid('unselectrow', event.args.rowindex);
      }
    });

    $("#jqxUOMRelatedUnitsDropdownGrid").jqxGrid('beginupdate');
    var GridObject = ['']
    var commit = $("#jqxUOMRelatedUnitsDropdownGrid").jqxGrid('addrow', null, GridObject);
    $("#jqxUOMRelatedUnitsDropdownGrid").jqxGrid('endupdate');

确定空行的当前代码

if(!row.UOMRelatedUnit_AddItem || row.length === 0) {
    _row["Name"] = $("#txtUnitOfMeasureSetName").val();
    _row["Active"] = T;
    _row["UnitOfMeasureTypeID"] = $("input[type='radio'][id='rblUnitOfMeasureType']:checked").val();
    _row["BaseUnitID"] = $("input[type='radio'][id='rblUnitOfMeasureBaseUnit']:checked").val();
    _row["RelatedUnitDisplayOrder"] = RecordCount;
    _row["RelatedUnitName"] = row.UOMRelatedUnit_Name;
    _row["RelatedUnitAbbreviation"] = row.UOMRelatedUnit_Abbreviation;
    _row["RelatedUnitConversionRatio"] = row.UOMRelatedUnit_ConversionOfBaseUnits;
    _row["UnitOfMeasureSetID"] = UnitOfMeasureSetID;
    _UnitOfMeasureRelatedUnitData[index++] = _row;
    RecordCount += 1;
}

编辑1

网格中的空行示例。

Empty Rows

1 个答案:

答案 0 :(得分:0)

找到了答案,我需要做的就是检查AddItem列是否未定义并且确实可以解决问题:

if($.type(row.UOMRelatedUnit_AddItem) !== "undefined")