简体   繁体   中英

Keep a table header at top of grid when scrolling

I know there are multiple way to write this, I have tried (fixed) and since I am not familiar with front end code I am finding this challenging. I found that placing the table header in a table itself and the table body in a table scrollable -y worked but I also need to have columns line up based on a drop down selection from the user and if I could avoid having to do that it would improve the look but I have yet to find a fix.

         </div>
      <div v-if="globals.showStructure === true" style="border:1px solid red; max-height:500px;overflow-y:scroll;position:top;" id="tableContainer2">
      <!--<div v-if="globals.showStructure === true" style="max-height:500px;overflow-y:auto" id="tableContainer2">-->
        <table class="table table-bordered">
          <ul class="right-click-menu" tabindex="-1"  v-if="globals.viewActiveStructMenu === true" ref="activeStructure">
           <li v-on:click="deleteStructure()">Delete</li>
           <li v-on:click="pendStructure()">Pend</li>
          </ul>
          <ul class="right-click-menu" tabindex="-1"  v-if="globals.viewPendStructMenu === true" ref="pendStructure">
           <li v-on:click="deleteStructure()">Delete<li>
           <li v-on:click="activateStructure()">Activate</li>
          </ul>
          <thead>
            <tr>
            <th style="width: 36px;" v-if="globals.showActiveToPendCheckbox == true" >Pend </th>
            <th style="width: 38px;" v-if="globals.showActivateCheckbox == true" > Activate </th>
            <th style="width: 58px;" v-if="globals.showDeleteCheckbox == true" > Delete </th>
            <th style="width: 48px;" data-toggle="tooltip" title="Super Summary Code">Sup Sum Code</th>
            <th style="width: 80px;" data-toggle="tooltip" title="Super Summary Description"> Sup Sum Code Desc</th>
            <th style="width: 48px;" data-toggle="tooltip" title="Summary Code">Sum Code</th>
            <th style="width: 122px;" data-toggle="tooltip" title="Summary Code Description">Sum Code Desc</th>
            <th style="width: 115px;" data-toggle="tooltip" title="Family Code">Fam Code</th>
            <th style="width: 81px;" data-toggle="tooltip" title="Secondary Code">Sec Code</th>
            <th style="width: 180px;" data-toggle="tooltip" title="Trade Item Description">GTIN Desc</th>
            <th style="width: 82px;" data-toggle="tooltip" title="GTIN">GTIN</th>
            <th style="width: 40px;" data-toggle="tooltip" title="Status">Status</th>
            <th v-if="globals.showEditColumn == true" style="width: 50px;" title='Edit'>Edit</th>
            </tr>
          </thead>
          <tbody>
          <tr  v-on:mousedown="rowStructClick(fcStructureItem.gtin,false)"  v-for="(fcStructureItem, index) in filteredFamilyCodeStructure"
            :key="fcStructureItem.gtin"
            :id="fcStructureItem.gtin"
            :form.fcStructure="form.fcStructure" tabindex="-1" v-on:contextmenu="openStructMenu($event)"
            :class = "alterRowColors(index)">
              <td v-if="globals.showActiveToPendCheckbox == true">
                <input  type="checkbox" v-model="activeToPendSelected" :value="fcStructureItem.gtin" number>
                </input>
              </td>
              <td v-if="globals.showActivateCheckbox == true">
                <input  type="checkbox" v-model="activateSelected" :value="fcStructureItem.gtin" number>
                </input>
              </td>
              <td v-if="globals.showDeleteCheckbox == true">
                <input  type="checkbox" v-model="deleteSelected" :value="fcStructureItem.gtin" number>
                </input>
              </td>
            <td>
              <span >{{ fcStructureItem.superSummaryCd }}</span>
            </td>
            <td>
              <span >{{ fcStructureItem.superSummaryDesc }}</span>
            </td>
            <td>
              <span >{{ fcStructureItem.summaryCd }}</span>
            </td>
            <td>
              <span >{{ fcStructureItem.summaryDesc }}</span>
            </td>
            <td>
              <span>{{ fcStructureItem.couponFamilyCode }}</span>
            </td>
            <td>
              <span>{{ fcStructureItem.oldCouponFamilyCode }}</span>
            </td>
            <td>
              <span >{{ fcStructureItem.tradeItemDescription }}</span>
            </td>
            <td>
              <span >{{ fcStructureItem.gtin }}</span>
            </td>
            <td>
              <span >{{ fcStructureItem.status }}</span>
            </td>
            <div v-if="globals.showEditButton == true" >
              <td>
                <span>
                  <button  class="btn btn-primary"  title="edit structure item" v-on:click="editStructure(fcStructureItem)">
                    <span> Edit </span>
                  </button>
                  </span>
              </td>
            </div>
          </tr>
          </tbody>
        </table>
      </div>
      <table id="PlaceHolder2"  class="table table-bordered"></table>

        <!-- new grid code is between here>-->

      </div>

Help on entering the CSS in here would be great if needed.

I've solved this previously having a fixed table layout CSS (or only 1 column is allowed to grow for width), and you do a pair of tables. one only has headers, the other only has rows and is inside a scrollable DIV. This was admittedly ~10years ago and IE6 compatible...

These days you should be able to set a CSS for your <tbody> to have overflow-x:none and overflow-y:scroll (or auto if you want no scrollbar with short data lists) as long as your <tr> are inside your <tbody>

(Edited to add code tag formatting to stop html tags vanishing from the text!)

you can use the new position: sticky property. When the page would scroll over the table header, it sticks to the top of the page and stays there.

For this, you have to define a top value, that specifies the offset (probably 0)

But be careful, sticky does not work in every browser and is especially buggy in tables: https://caniuse.com/#feat=css-sticky

 thead { /* position: sticky; and top: 0; are the only needed properties here. The rest is for showcase only. */ position: sticky; top: 0; background: red; } th { width: 300px; height: 50px; } td { border: 1px solid yellow; height: 200px } 
 <table> <thead> <tr> <th>A</th> <th>B</th> <th>C</th> </tr> </thead> <tbody> <tr> <td>A</td> <td>B</td> <td>C</td> </tr> <tr> <td>A</td> <td>B</td> <td>C</td> </tr> <tr> <td>A</td> <td>B</td> <td>C</td> </tr> <tr> <td>A</td> <td>B</td> <td>C</td> </tr> </tbody> </table> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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