0

I am trying to make UL.pagination to be in the center of the view-area of the parent, without using float as it will interrupt my style. Would this possible?

I am not familiar with SO yet, but my point is to trigger scrollLeft of .pagination parent to keep pagination in the center of its parent.

This is my try on jsFiddle

/* my goal is to make pagination in the center of the view-area */
$.fn.StickyX = function() {
  // alert(this.parentNode.clientWidth);
  // alert($(this).parent().innerWidth());
  return this.css({
    'margin-left': ($(this).parent().innerWidth() / 2) - ($(this).width() / 2) + 'px',
    'position': 'inline'
  });
};
$('.pagination').StickyX();
#dataTable .pagination {
  margin: 0;
  padding: 0;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container" id="wrapper">
  <div id="header">
    Header
  </div>
  <div class="row">
    <div class="col-xs-9 contents">

      <div class="table-responsive">
        <table class="table table-bordered table-hover table-striped table-condensed" id="dataTable">
          <thead class="text-primary">
            <tr>
              <th role="group" aria-label="">
                <button type="button" class="btn btn-default btn-add"><i class="glyphicon glyphicon-plus"></i>
                </button>
                <button type="button" class="btn btn-default btn-refresh"><i class="glyphicon glyphicon-refresh"></i>
                </button>
              </th>
              <th>id</th>
              <th>User name</th>
              <th>password</th>
              <th>Full name</th>
              <th>Email</th>
              <th>URL</th>
              <th>Join date</th>
              <th>activation_key</th>
              <th>lastupdate</th>
              <th>status</th>
              <th>groupID</th>
              <th>title</th>
            </tr>
          </thead>
          <tbody>
            <tr id="row-1">
              <td class="btn-group" role="group" aria-label="">
                <button type="button" class="btn btn-warning btn-edit"><i class="glyphicon glyphicon-pencil"></i>
                </button>
                <button type="button" class="btn btn-danger btn-delete"><i class="glyphicon glyphicon-trash"></i>
                </button>
              </td>
              <td>1</td>
              <td>admin.admin</td>
              <td>**********</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td>2015-12-07 02:37:47</td>
              <td>&nbsp;</td>
              <td>2015-12-06 18:37:47</td>
              <td>isActive</td>
              <td>isSuperAdmin</td>
              <td>Super Admin</td>
            </tr>
            <tr id="row-2">
              <td class="btn-group" role="group" aria-label="">
                <button type="button" class="btn btn-warning btn-edit"><i class="glyphicon glyphicon-pencil"></i>
                </button>
                <button type="button" class="btn btn-danger btn-delete"><i class="glyphicon glyphicon-trash"></i>
                </button>
              </td>
              <td>2</td>
              <td>demo.demo</td>
              <td>***********</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td>0000-00-00 00:00:00</td>
              <td>&nbsp;</td>
              <td>2016-01-05 12:45:32</td>
              <td>isActive</td>
              <td>isSuperAdmin</td>
              <td>demo</td>
            </tr>
            <tr>
              <td class="page-control" colspan="13">
                <ul class="pagination">
                  <li class="disabled">
                    <a href="#Prv" aria-label="Previous">
                      <span aria-hidden="true">&laquo;</span>
                    </a>
                  </li>
                  <li class="active"><a href="#page1">1<span class="sr-only">(current)</span></a>
                  </li>
                  <li><a href="#page2">2</a>
                  </li>
                  <li><a href="#page3">3</a>
                  </li>
                  <li><a href="#page4">4</a>
                  </li>
                  <li><a href="#page5">5</a>
                  </li>
                  <li>
                    <a href="#Nxt" aria-label="Next">
                      <span aria-hidden="true">&raquo;</span>
                    </a>
                  </li>
                </ul>
              </td>
            </tr>
          </tbody>
          <tfoot></tfoot>
        </table>
      </div>
    </div>
    <div class="col-xs-3">
      [Menu]
    </div>
  </div>
  <div>
    Footer
  </div>
</div>

I made some modification js which some how solve the issue, but still need some more help. jsfiddle

    (function($) { // forked from http://stackoverflow.com/questions/4814398/how-can-i-check-if-a-scrollbar-is-visible
      $.fn.hasScrollBar = function() {
        var e = this.get(0);
        return {
          vertical: e.scrollHeight > e.clientHeight,
          horizontal: e.scrollWidth > e.clientWidth
        };
      }
    })(jQuery);

    $.fn.StickyX = function() {
      var parentWithScroll = $(this).parents().filter(function() {return $(this).hasScrollBar().horizontal;}).first();
      if(parentWithScroll)  {
      var e = $(this);
      parentWithScroll.scroll(function() {
        e.css('margin-left', ($(this).width() / 2) - (e.width() / 2) + $(this).scrollLeft() + 'px');
      })
      return this.css('margin-left', (parentWithScroll.width() / 2) - ($(this).width() / 2) + parentWithScroll.scrollLeft() + 'px');
      } else {
        return this.css('margin-left', 'auto;');
      }
    };

    $('.pagination').StickyX();
meYnot
  • 343
  • 2
  • 6
  • 13
  • You did well in creating a jsfiddle and adding all your code in reference, but try to avoid, when possible, to insert irrelevant code. For example, you don't need to show the css and javascript links, nor the entire table. – Enrico Jan 18 '16 at 22:18

1 Answers1

0

to achieve this you only need css and html. Try this:

td.page-control { text-align:center; }
ul.pagination { margin:0 auto; }

Adapt the code to make sure it's not overridden by your existing styles.

And disable the javascript that is adding the margin to the ul

https://jsfiddle.net/ksqzpfm9/1/

You can use the same method to horizontally center other elements

Enrico
  • 408
  • 6
  • 13
  • Thanks @Enrico for your reply, but what I am looking for is center .pagination in the view area not the whole width of the parent td, if you resize the #wrapper to 300px you shall notice that .pagination is not in the view-area anymore! – meYnot Jan 19 '16 at 00:53
  • Ok, wrap the ul with a container and dynamically set its width to that of the table. Updated [jsfiddle](https://jsfiddle.net/ksqzpfm9/2/) – Enrico Jan 19 '16 at 01:03
  • pagination is in the center of its parent, but the parent itself is not shown in whole, what I am looking for is that pagination to be in the center of the shown/visible part of the parent! – meYnot Jan 19 '16 at 01:53
  • Which is exactly what i did in the last JSfiddle. I see it does not work on chrome, but it does on safari. Check this again now: https://jsfiddle.net/ksqzpfm9/3/ – Enrico Jan 19 '16 at 10:17
  • well, I am not satisfied of the solution @Enrico, I guess that 10! thing is not logical there, but I made this (which I am not satisfied with either) https://jsfiddle.net/emad/ksqzpfm9/4/ – meYnot Jan 20 '16 at 22:52
  • That "10" thing is there to compensate the padding and actually center the pagination. Both solutions works good as long as you don't scroll the table. If you have to deal with this situation, i suggest you set the pagination `position:fixed;` and then dynamically set the values with javascript/jquery. – Enrico Jan 21 '16 at 00:01