2013-11-01 107 views
0

我有一个jTable,我需要选择所有行,这样我可以稍后将信息提供给AJAX并将数据发送到控制器进行处理。jQuery jTable选择行

我的问题是,我无法让它工作。这是迄今为止的JS代码。

$('#saveButton').click(function(){ 
     $('#UserTable').jtable("selectRows"); // I think here is the problem 
     var $selectedRows = $('#smsUserTable').jtable('selectedRows'); 
     $selectedRows.each(function() { 
      var record = $(this).data('record'); 
      var userId = record.userId; 
      console.log(userId); 
     }); 
     }); 

我得到Uncaught TypeError:无法始终调用未定义错误的方法'addClass'。

也许我正在使用selectRows方法错误。先谢谢你。

+3

我不认为这是代码的问题,因为你不调用'addClass'任何地方。当您尝试在本机DOM对象上使用jQuery函数时,通常会出现此错误。 –

回答

1

也许这是解决方案?

$('#UserTable .jtable-data-row').each(function() { 
      $(this).addClass('jtable-row-selected'); 
      $(this).addClass('ui-state-highlight'); 
     }); 

这将选择表中的所有可见行。

1

您可以使用下面的代码来获得JSON字符串的所有表recods数据:

/* Select all Records */ 
$('#your_table .jtable-data-row').each(function() { 
    $(this).addClass('jtable-row-selected'); 
    $(this).addClass('ui-state-highlight'); 
}); 
/* Read each record and add it to json */ 
var $selectedRows = $('#your_table').jtable('selectedRows'),records = []; 
$selectedRows.each(function() { 
    var record = $(this).data('record'); 
    records.push(record); 
}); 
var josn_data = JSON.stringify(records);