如何将角度ui网格单元格中的按钮居中?

时间:2015-06-02 21:45:52

标签: css centering angular-ui-grid

我按如下方式定义一列:

{
  name: 'button', 
  displayName: '', 
  cellClass: 'ui-grid-vcenter',
  enableColumnMenu: false,
  enableFiltering: false,
  enableSorting: false,
  cellTemplate: '<div><button ng-click="grid.appScope.rowButtonHandler(row.entity.id)">clicky</button></div>'
}

导致:

<div class="ui-grid-cell ng-scope ui-grid-coluiGrid-010 ui-grid-vcenter" ui-grid-cell="" ng-class="{ 'ui-grid-row-header-cell': col.isRowHeader }" ng-repeat="(colRenderIndex, col) in colContainer.renderedColumns track by col.colDef.name">
    <div class="ng-scope">
        <button ng-click="grid.appScope.rowButtonHandler(row.entity.id)"></button>
    </div>
</div>

我想垂直和水平居中这个按钮。水平,工作,但垂直,我似乎无法正确的CSS。这是我的通用镜头:

.ui-grid-vcenter div {
  text-align: center;
  vertical-align: middle !important;
  background-color: yellow !important;
}

如何在这种AngularJS网格的单元格中居中内容?

2 个答案:

答案 0 :(得分:7)

使用相对位置:

.ui-grid-vcenter div {
  background-color: yellow !important;
  text-align:center;
  position: relative;
  top: 50%;
   -webkit-transform: translateY(-50%);
  -ms-transform: translateY(-50%);
  transform: translateY(-50%);
}

答案 1 :(得分:1)

使用css flex可以更容易(https://css-tricks.com/snippets/css/a-guide-to-flexbox/):

假设您的 cellTemplate myCellTemplateForEditAndDelBtns.html):

<div id="editBtns" class="flexParent">
<span  class="flexChildren">
    <button><i class="fa fa-pencil-square-o "></i></button>
    <button><i class="fa fa-times gridDelBtn"></i></button>
</span>
</div>

您可以将 columDefs 添加到您的单元格中:

$scope.myGrid.columnDefs = [
    ...
    { name: 'edit',
        enableCellEdit: false,
        enableFiltering: false,
        enableSorting: false,
        cellTemplate: 'location-to/myCellTemplateForEditAndDelBtns.html',
        width: '5%'
    }
];

终于使用 CSS Flex

#editBtns {
    height: 100%;
}

.flexParent {
    display: flex;
    justify-content: center;
}

.flexChildren {
    align-self: center;
}