4

I am developing a web page using twitter-bootstrap 3 and some tables are not fitting my page's margins. They overflow. So I want to create a structure which works like Excel spreadsheet.

The most left two columns (blue region) will be fixed and the remainin columns will be shown as possible. Overflowing columns will be displayed if user scrolls the table's scroll bar to right. You can see the picture how I want it to be.

enter image description here Putting the table into a div and rule the div overflow-x:scroll is not a solution for me. Because The first two columns must be visible all the time.

There is a solution here I have tried this. It works only when I shrink the window into smaller width value.

zkanoca
  • 9,664
  • 9
  • 50
  • 94
  • *Is it possible to create a scrolling table?* Yes. – Karl-André Gagnon May 09 '14 at 13:56
  • @Bobby5193 **Fixed columns**. And for Karl-, that's a very selective quote that doesn't capture the actual request. Even though it is the title, there's more to it :) – CodingIntrigue May 09 '14 at 13:57
  • @RGraham the fixed columns go in another table, the overflowing ones go in the table with overflow-x:scroll; I don't see how this is confusing – Bobby5193 May 09 '14 at 13:59
  • fixed width of columns and when you will scroll you can change the first left column width, when it reach 0 go to the second and so on – TheGr8_Nik May 09 '14 at 14:01
  • @Bobby5193 Well I couldn't get it to work. And I don't *think* my CSS is that bad - http://jsfiddle.net/XGvSY/ – CodingIntrigue May 09 '14 at 14:04
  • @RGraham and zkanoca that's the answer to the actual question. in the entire post, there is only 1 question, 1 description of a problem and 1 request (*"how i want it to be"*). Yet we see no effort on solving the issue or any attempt to (im sure you've tried something, and you should show it). So, yeah, it is possible to achieve what you want. You may try something and then I can tell you what you are doing wrong. – Karl-André Gagnon May 09 '14 at 14:04
  • put it in a div http://jsfiddle.net/bobby5193/XGvSY/2/ – Bobby5193 May 09 '14 at 14:05
  • @Karl-AndréGagnon OP has shown a good illustrative example which explains the *problem* he has. Nothing wrong with asking a hypothetical question as long as it's backed up. I'm not sure showing you markup for a table would help - you've seen it a million times before. – CodingIntrigue May 09 '14 at 14:08
  • 1
    take a look at my answer [here](http://stackoverflow.com/questions/14977864/fixed-header-table-with-horizontal-scrollbar-and-vertical-scrollbar-on/) – avrahamcool May 09 '14 at 14:10
  • 2
    @RGraham Well that's just my opinion, but SO is a help center. Yes the OP posted clearly what he want, I agree. But this question is etheir *"show me a tool that can do it"* which is a *off topic* question in the close vote or *"I need to, give me a code"* (i'm exaggerating). I know it's possible, i've already done it somewhere (replaced the table with div's), but it was a long job and got paid for that. That's why ill not give the code away, but i'd gladly help to solve why current code is not working. But yeah, that's my opinion, other people can answer if they want! – Karl-André Gagnon May 09 '14 at 14:16
  • P.S.: I do not wish to start an arguments here, you may disagree and i understand. But anyway, have a great day! – Karl-André Gagnon May 09 '14 at 14:16
  • Sorry OOT, but I have issue with fixed column, and here's [my question](http://stackoverflow.com/q/23560059/1297435) can someone in here to help me, cause if my issue can be fixed, the problem OP can be resolved too.. – rails_id May 09 '14 at 15:07
  • 2
    @Karl-AndréGagnon - while I totally get what you are saying, looking at it from the "help center" view that you mentioned, simply not commenting would have been just as helpful as the initial comment that you provided. – talemyn May 09 '14 at 15:32
  • @talemyn It was a sarcastic way to say *"Your question is incomplete to my eye."* Which mean the question is too general. – Karl-André Gagnon May 09 '14 at 15:40
  • 2
    @Karl-AndréGagnon - oh, I totally got that, but surely you can understand that someone coming to this site for help with a problem, might get a little frustrated to receive sarcasm, instead of something constructive. I know we all get sick of having to remind people to tell us what they've tried and provide code samples and all that, but adding a sarcastic response to an incomplete/poor question, doesn't make the question any better either. – talemyn May 09 '14 at 15:47
  • @talemyn actually, that's opinion based. My mother always told me that *instead of showing what someone did wrong, you should make them realize what they did wrong.* My comment wasn't mean at all and put the finger on what was wrong in this question. Now, may we stop polluting the comment section since it is really not helpful! I would gladly appreciate if someone could flag my first comment (since I can't) and ask moderator to delete the entire conversation? – Karl-André Gagnon May 09 '14 at 15:58
  • 2
    All the responders and commenters, I respect all your work. You can easily ask what I have tried so far. I have googled, tried to divide the table, put it into some kind of divs, boxes. Nothing could help. I never want you to do my work. I never like it. But I was really stucked. So I tried to illustrate what I want. And now, I decided to reduce the font-size according to column-count. Enless thanks for your efforts. – zkanoca May 09 '14 at 16:10

6 Answers6

3

Seems to me that you could use a jquery dataTable along with the FixedColumns plugin - it does exactly what you want by default. Just include jquery.dataTables.js and jsfixedColumns.min.js (along with the css files) from the CDN and initialize your table :

var table = $('#example').DataTable( {
  sScrollX: "100%",
  sScrollXInner: "150%",
  bScrollCollapse: true        
});
new $.fn.dataTable.FixedColumns(table);

see demo -> http://jsfiddle.net/n2x8W/

dataTables supports bootstrap and you can style the table as you want. In the example I have just included the not-so-modern default CSS. Notice, the initialization exposes dataTables basic features like pagination, filtering etc. They can easily be plugged off if you not need / want them.

Also notice that I am using version 1.9.4, since this to my experience is more stable than 1.10.x.

davidkonrad
  • 83,997
  • 17
  • 205
  • 265
  • That looks great! I am going to try it if it does not conflict bootstrap's jquery – zkanoca May 09 '14 at 14:39
  • Your answer is the closest to my need. But I have given up. I reduce the font-size in according to column count, using `%...`
    – zkanoca May 09 '14 at 16:00
  • @zkanoca, Sry to hear that. If you post a sample of your table in the question, and tell which version of bootstrap you are using, maybe I can give it a try? Guess it is responsive issues that is your concern? – davidkonrad May 09 '14 at 20:24
2

You could try this:
Wrap your table in a div,
Absolute position your first 2 columns, give them some width (Lets say Apx and Bpx),
Use margin-left:(A+B)px and overflow-x:scroll to the wrapper div

Fiddle: http://jsfiddle.net/ycYdL/

CdB
  • 4,738
  • 7
  • 46
  • 69
  • fixed columns' heights are variant. Their data come from DB. What if a row consists of two or more lines? – zkanoca May 09 '14 at 14:46
  • 1
    You are right @zkanoca... Can't find a pure CSS workaround, so I added a piece of jQuery code (saw in a comment above you are using it), maybe it fit your needs... http://jsfiddle.net/ycYdL/1/ – CdB May 09 '14 at 15:07
1

Unfortunately not. You will need a table for the fixed part, and a table for the scrolling part.

To avoid the problem of rows no longer being the same height, consider actually duplicating the "fixed" columns from the original table and overlaying them on your scrollable area. This will look visually identical (provided you set a solid background colour on the fixed area), but will ensure the table works as intended.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
0

there are certain tools which can help you with that problem.

I'd give datatables a go, they offer an options that does exactly that, maybe with a few variations.

have a look here: http://datatables.net/examples/basic_init/scroll_y.html

Ted
  • 3,985
  • 1
  • 20
  • 33
0

You could place the table into a div that would fit to the size of your screen and then overflow auto that div

tylerlindell
  • 1,545
  • 2
  • 17
  • 20
  • I have thought this. But I want the first two columns to be visible when the table is scrolled. – zkanoca May 09 '14 at 14:02
  • I have not tried it but maybe you could only put the cells you want to scroll inside of the div... it's a big stretch and probably not work but maybe using divs and displaying them as table cells and the container as a table might work. – tylerlindell May 09 '14 at 18:32
0

This can be done, but it's not exactly straightforward . . .

You basically are going to need to create two tables:

  1. A thin table on the left side, that is made up of the row headers (i.e., the stuff in blue).

  2. A second table, in a div, immediately to the right of the first table, that will contain the data fields for the rows. The div would then need to have the overflow-x: scroll; style attached to it.

I've not done this with horizontal scrolling before, but I have done it with vertical (top row stays put, the rest scroll), and the concept should work the same way here.

A couple of things to be aware of:

  • the CSS to enforce matching row heights is going to be VERY important here . . . getting that to match up is much more complex with a horizontal scroll, than a vertical one
  • if you have any accessibility requirements (e.g., screen reader support), you will may have to include a hidden "header column" in the second table, that is a duplicate of the static one in the first table, so that the screen reader will associate the row headers with each data cell.
talemyn
  • 7,822
  • 4
  • 31
  • 52