【问题标题】:flexigrid get selected row columns valuesflexigrid 获取选定的行列值
【发布时间】:2013-07-20 21:50:35
【问题描述】:

我是 flexigrid 的新手。谁能告诉我如何获取所选行的每列值。?

如何获取每个列的名称(reportName 和 reportDescription)?因为我将//所有数据推入数组,如下所述。

我正在使用下面的代码来获取选定的行。但它返回null。你能帮助我吗?

//Column. <br/>


colModel: [
  { display: 'WidgetID', name: 'WidgetID', width: 50, sortable: true, align: 'left', hide: true },
  { display: 'Widget Name', name: 'WidgetName', width: 170, sortable: true, align: 'left' },
  { display: 'IsClientReport', name: 'IsClientReport', width: 50, sortable: false, align: 'left', hide: true },
  { display: 'ClientReportID', name: 'ClientReportID', width: 50, sortable: false, align: 'left', hide: true },
  { display: 'ReportType', name: 'ReportType', width: 280, sortable: true, align: 'left' }
],

$('#grid01').click(function(event){ 
  $('.trSelected', this).each( function(){ 
      console.log( ' rowId: ' + $(this).attr('id').substr(3) + ' IsClientReport: ' + $('td[abbr="IsClientReport"] >div', this).html() + ' sign: ' + $('td[abbr="WidgetID"] >div', this).html() + ' ReportType: ' + $('td[abbr="ReportType"] >div', this).html() ); 
  }); 
});

谢谢, 庞库马尔潘迪安

【问题讨论】:

    标签: flexigrid


    【解决方案1】:

    不确定您是否已经弄清楚了,但我将把这个答案留在这里,以防遇到同样情况的其他人像我一样偶然发现您的问题。

    在列上设置 'sortable: false' 会从 Flexigrid 生成的 'td' 中删除 'abbr' 属性。这意味着您不能使用推荐的解决方案来获取选定的行。

    我自己修改了 flexigrid.js 文件来解决这个问题。

    Flexigrid 以前仅在列具有“名称”且具有“可排序:真”时才添加“缩写”属性。我删除了 'sortable: true' 的条件。

    反过来,这也意味着列将始终是可排序的。 为了防止这种情况,我添加了一个 'sortable' 属性,只有当列是 'sortable: true' 时才会设置它

    之后,我必须检查所有使用“abbr”作为排序条件的情况,并将其替换为“可排序”检查。

    就是这样。

    uploaded the file to mediafire 如果您只想下载并使用这个代替。非特定位置的更改太多,我无法在此处显示我对代码的更改。如果需要,我可以提供差异或更多解释。请注意,'sortable: true' 仍然适用于我的修复。

    【讨论】:

      【解决方案2】:

      我遇到了同样的问题,用下面的代码解决了

       jQuery('#schoolist .trSelected').each( function(){
                      alert(jQuery('[abbr="name"]',this).text());
      
                  });
      

      只需将其添加到函数中,并将 id #schoolist 和 abbr name 替换为您需要的列的名称即可。

      【讨论】:

        【解决方案3】:

        在此处查看 FlexiGrid 常见问题解答: http://code.google.com/p/flexigrid/wiki/FAQ#Get_selected_row

        【讨论】:

        • 我正在使用下面的代码来获取选定的行。但它返回null。你能帮助我吗? $('#grid01').click(function(event){ $('.trSelected', this).each( function(){ console.log(' rowId:' + $(this).attr('id' ).substr(3) + ' name: ' + $('td[abbr="name"] >div', this).html() + ' sign: ' + $('td[abbr="sign"] >div', this).html() + ' 状态: ' + $('td[abbr="status"] >div', this).html() ); }); });
        • 我也遇到了同样的问题,有人解决了吗?
        【解决方案4】:

        你可以试试:

        $('#grid01').click(function(event){
            $('.trSelected', this).each( function(){
                console.log(
                    '  rowId: '  + $(this).attr('id').substr(3) +
                    '  name: '   + $('td[abbr="name"] >div', this).html() +
                    '  sign: '   + $('td[abbr="sign"] >div', this).html() +
                    '  status: ' + $('td[abbr="status"] >div', this).html() 
                );
            });
        });
        

        将 flexgrid 与 CI 结合使用,然后在您的自定义按钮事件中添加以下代码

        function test(com, grid) {  
            if (com == 'Export') {
                var data = ($('.bDiv', grid).html());
                $('.bDiv tbody tr', grid).each(function () {
                    console.log('  rowId: ' + $(this).attr('id').substr(3) + '  name: ' + $('td[abbr="name"] >div', this).html() + '  coupon_no: ' + $('td[abbr="coupon_no"] >div', this).html() + '  status: ' + $('td[abbr="status"] >div', this).html());
                });
            }   
        }
        

        PHP CI 代码:

        $buttons[] = array('Export','excel','test');
        

        检查屏幕:

        【讨论】:

          【解决方案5】:

          将以下函数添加到 flexigrid.js 源将返回一个选定行的数组。

          $.fn.selectedRows = function (p) {
              var arReturn = [];
              var arRow = [];
              var selector = $(this.selector + ' .trSelected');
              $(selector).each(function (i, row) {
                  arRow = [];
                  $.each(row.cells, function (c, cell) {
                      var col = cell.abbr;
                      var val = cell.innerText;
                      var idx = cell.cellIndex;
          
                      arRow.push(
                          {
                              Column: col,
                              Value: val,
                              CellIndex: idx
                          }
                          );
                  });
                  arReturn.push(arRow);
          
              });
              return arReturn;
          };
          

          用法:

          var rows = $('#datagrid').selectedRows();
          

          按列名查找值

          function getColValueByName(cols, colName) {
             var retVal = '';
             var param = $.grep(cols, function (e) {
              var found = e.Column == colName;
              if (found != null && found != undefined & found) {
                  retVal = e.Value;
              }
          });
             return retVal;
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2011-07-04
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多