可能であれば、「if」ステートメントで 2 つの条件を組み合わせようとしています。
最初に画面の幅を確認してから、テーブルの行または最初の行に 2 つ以上の td があるかどうかを確認します。ある場合は、何らかの機能を実行します。
これは、幅の最初の部分として持っているものですが、満たす必要がある他の条件をどのように追加しますか?
var width = parseInt($(window).width());
if (width<=610) {
//Need to add the other condition that is a table has more than 2 td's in a row
}
これが私が実行しようとしている実際のコードです。2列以上の場合の実行については、私のコメントを参照してください。
var width = parseInt($(window).width());
if (width<=610) {
//Need to figure out how execute this if there are more than 2 columns in the table???
$('.content table').replaceWith(function() {
var $th = $(this).find('th'); // get headers
var th = $th.map(function() {
return $(this).text();
}).get(); // and their values
$th.closest('tr').remove(); // then remove them
var $d = $('<div>', { 'class': 'responsive-table' });
$('tr', this).each(function(i, el) {
var $div = $('<div>', {'class': 'box grey-bg'}).appendTo($d);
$('td', this).each(function(j, el) {
var n = j + 1;
var $row = $('<div>', {
//'class': 'row-' + n
'class': 'row'
});
$row.append(
$('<span>', {
//'class': 'label-' + n,
'class': 'label',
text: th[j]
}), ' : ', $('<span>', {
//'class': 'data-' + n,
'class': 'data',
text: $(this).text()
})).appendTo($div);
});
});
return $d;
});
};