简体   繁体   English

如何调整响应表的JavaScript以在移动设备中选择其他列

[英]How do I tweak a responsive table’s JavaScript to select a different column in mobile

Here is my CodePen: 这是我的CodePen:

https://codepen.io/matbathome77/pen/KeEzZG https://codepen.io/matbathome77/pen/KeEzZG

var pos = $(this).index()+2;

It is based on someone else's CodePen. 它基于其他人的CodePen。 How do I, in the mobile version, have it default to the “1 year” column, and show the content in that column. 在移动版本中,如何将其默认设置为“ 1年”列,并在该列中显示内容。

Right now it is defaulted to 3 year and it isn't showing any content under that th until you select the item. 现在默认为3年,并且在您选择该项目之前不会显示该内容。

I tried editing the JS on line 4 to be +1 instead of +2, in hopes that this would change the default to 1 year. 我尝试将第4行的JS编辑为+1而不是+2,希望这会将默认值更改为1年。 It didn't work. 没用

Activate the column on the page load, and also on click. 在页面加载和单击上激活列。 Move the onclick inline function to its own name, then call to it (with the li style updating) when it's clicked on. 将onclick内联函数移动到其自己的名称,然后在单击它时对其进行调用(使用li样式更新)。

from: 从:

$( "ul" ).on( "click", "li", function() {
  var pos = $(this).index()+2;
  $("tr").find('td:not(:eq(0))').hide();
  $('td:nth-child('+pos+')').css('display','table-cell');
  $("tr").find('th:not(:eq(0))').hide();
  $('li').removeClass('active');
  $(this).addClass('active');
});

to

function loadColumn(pos) {
  $("tr").find('td:not(:eq(0))').hide();
  $('td:nth-child('+pos+')').css('display','table-cell');
  $("tr").find('th:not(:eq(0))').hide();
}

loadColumn(2);

$( "ul" ).on( "click", "li", function() {
  var pos = $(this).index() + 1;
  loadColumn(pos);
  $('li').removeClass('active');
  $(this).addClass('active');
});

is what you're looking for. 是您要寻找的。

Here's the codepen: https://codepen.io/anon/pen/KeEXzR 这是codepen: https ://codepen.io/anon/pen/KeEXzR

I figured it out. 我想到了。 I had to create an li and hide it. 我必须创建一个li并将其隐藏。 I had to also tell the javascript to call the third column, not the second. 我还必须告诉javascript调用第三列,而不是第二列。 Thanks all. 谢谢大家

https://codepen.io/matbathome77/pen/KeEzZG https://codepen.io/matbathome77/pen/KeEzZG

<li class="hidden">Name (ticker)<br>Bla bla bla category</li>

and the css 和CSS

li.hidden {display:none;}

finally, the javascript 最后,JavaScript

loadColumn(3); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM