使用行和列值angularjs对齐gridster项

时间:2018-04-11 07:54:49

标签: javascript html angularjs gridster angular-grid

我使用angularjs,kendoui图表和角度网格(https://github.com/ManifestWebDesign/angular-gridster)创建了一个仪表板。

所以我有一个函数来添加一个带有如下图表的gridster: -

// add widget to a dashboard
$scope.addWidget = function () {
  
  // new widget configuration
  var newWidget = {
    id: $scope.allWidgetData.selectedOption.id,
    data_source: {},
    config: {
      name: $scope.allWidgetData.selectedOption.name,
      sizeX: 3,
      sizeY: 2,
      row: $scope.row,
      col: $scope.col
    }  
  };
}

// make api call and save data

我可以将此对象保存到后端并获取值。我按如下方式为每个图表设置行和列值,因为sizeX和sizeY值恒定为3和2。

  1. 第一个图表行:0和col:0
  2. 第二个图表行:0和col:3
  3. 第三个图表行:2和col:0
  4. 第四个图表行:2和col:3
  5. 我在下面的网页上提到并搜索了解决方案: -

    1. Save gridster layout using angularjs

    2. https://github.com/ManifestWebDesign/angular-gridster

    3. 所以现在我的HTML就像下面一样,所以我设置了行和列值: -

      <div gridster="gridsterOptions" ng-if="isShowing(index)">
          <ul>
              <li gridster-item="widget" ng-repeat="widget in widgetData.availableOptions" row="widget.config.row" col="widget.config.col" style="overflow:hidden;" >
                  <div class="box">                         
      
                  // kendo ui chart in  div
              </li>
          </ul>       
      </div> 

      我的网格配置如下: -

      $scope.gridsterOptions = {
            minRows: 2, // the minimum height of the grid, in rows
            maxRows: 100,
            columns: 6, // the width of the grid, in columns
            colWidth: 'auto', // can be an integer or 'auto'.  'auto' uses the pixel width of the element divided by 'columns'
            rowHeight: 'match', // can be an integer or 'match'.  Match uses the colWidth, giving you square widgets.
            margins: [10, 10], // the pixel distance between each widget
            defaultSizeX: 3, // the default width of a gridster item, if not specifed
            defaultSizeY: 2, // the default height of a gridster item, if not specified
            mobileBreakPoint: 600, // if the screen is not wider that this, remove the grid layout and stack the items
            swapping: false,
            pushing: true,
            floating: false,
            resizable: {
                enabled: true,
                start: function(event, uiWidget, $element) {}, // optional callback fired when resize is started,
                resize: function (event, uiWidget, $element) {
                }, // optional callback fired when item is resized,
                stop: function(event, uiWidget, $element) {
                } // optional callback fired when item is finished resizing
            },
            draggable: {
               enabled: true, // whether dragging items is supported
               handle: '.box-header', // optional selector for resize handle
               start: function(event, uiWidget, $element) {}, // optional callback fired when drag is started,
               drag: function(event, uiWidget, $element) {}, // optional callback fired when item is moved,
               stop: function(event, uiWidget, $element) {          
              } // optional callback fired when item is finished dragging
            }
        };  

        

      当我从后端获取gridster项目时,它们是随机顺序的   和gridster没有按行和列值对齐它们。

1 个答案:

答案 0 :(得分:1)

我为您创建了一支工作笔:https://codepen.io/shnigi/pen/JLVZpK

我有类似的问题,这就是我解决它的方法。确保引用正确的对象值。您可能还需要在ajax呼叫准备就绪时设置位置:

<li ng-repeat="widget in widgets"
          class="panel" gridster-item
          row="widget.posY"
          col="widget.posX"
          size-x="widget.width"
          size-y="widget.height">
        <div class="panel-heading">
          <h3 class="panel-title">{{widget.title}}</h3>
        </div>
</li>

移动断点也会在您设置窗口小部件位置时出现问题,然后您可能会有不同的分辨率。所以我在这里做的是我现在正在使用这个分支:

https://github.com/arkabide/angular-gridster

我根据分辨率对列进行计数,并打开“动态”列。

$scope.gridsterOptions = {
  resizable: {
    enabled: false,
  },
  floating: true,
  pushing: true,
  swapping: true,
  isMobile: true,
  mobileBreakPoint: 768,
  columns: getColumns(),
  dynamicColumns: true,
  minWidthToAddANewColumn: 255,
  rowHeight: 400,
  draggable: {
    enabled: true,
    handle: '.panel-heading'
  }
};

const getColumns = () => {
  const browserWidth = $window.innerWidth;
  if (browserWidth < 1300) {
    return 3;
  } else if (browserWidth < 1500) {
    return 4;
  } else if (browserWidth < 1700) {
    return 5;
  }
  return 6;
};

如果分辨率发生变化,小部件定位会有一些问题,但这是迄今为止我所获得的最佳解决方案。

因此,请确保在使用promises分配数据之前加载所有数据。像这样的伪代码:

const loadedWidgets = [];
    if (settings) {
      const loadAllWidgets = () => new Promise(resolve =>
          loadedWidgets.push(widget);
          resolve();
        }));

  const actions = settings.chartsettings.map(loadAllWidgets);
  Promise.all(actions).then(() => {
    const widgetList = loadedWidgets.map(widget => widget);
    $scope.widgets = widgetList;
  });
}

我猜你的问题与对象引用有关,或者你没有用promises设置位置。你也可以试试我正在使用的叉子。因为对我来说这个设置有效,小部件出现在我保存的地方。