如何获取Spring Data上的总页数?

时间:2018-05-30 15:25:54

标签: java pagination spring-data-jpa repository

我有一个显示实体列表的Angular应用程序,我有一个'显示更多'按钮,增加页码并使用此方法:

private MainWindow parent; // used to call an AddGame event when client adds a game
public ObservableCollection<Game> games;
public class Game
{
    public string Name { get; set; }
}
public HostLobby(MainWindow mw)
{
    InitializeComponent();
    parent = mw;
    games = new ObservableCollection<Game>();

    // add some test items - this works. the ListBox updates properly
    games.Add(new Game() { Name = "game1" });
    games.Add(new Game() { Name = "game2" });
    games.Add(new Game() { Name = "game3" });

    gamesListBox.ItemsSource = games;
}
public void AddGameToList(string n, int maxp)
{
    // called to announce a new game added by another client
    // call stack is Player.AddGameToLobby -> MainWindow.AddGameToLobby -> this.AddGameToList
    string msg = String.Format("{0} (0/{1})", n, maxp.ToString());
    games.Add(new Game() { Name = msg });
}

我格式化此<Grid> <Grid.ColumnDefinitions> <ColumnDefinition/> <ColumnDefinition/> </Grid.ColumnDefinitions> <StackPanel Grid.Column="0" Margin="50" Orientation="Vertical" VerticalAlignment="Center"> <StackPanel Orientation="Horizontal"> <Label HorizontalContentAlignment="Right" Width="80">Game Name:</Label> <TextBox Name="gameNameTextBox" VerticalContentAlignment="Center" Width="200"/> </StackPanel> <StackPanel Orientation="Horizontal" Margin="0,10,0,0"> <Label HorizontalContentAlignment="Right" Width="80">Max Players:</Label> <TextBox Name="maxPlayersTextBox" VerticalContentAlignment="Center" Width="200"/> </StackPanel> <Button Name="addGameButton" Click="addGameButton_Click" Margin="0,20,0,0" Width="200" Height="30">Add Game</Button> </StackPanel> <StackPanel Orientation="Vertical" VerticalAlignment="Center" Margin="50" Grid.Column="1"> <Label>Current Games</Label> <ListBox Name="gamesListBox" MinHeight="200"> <ListBox.ItemTemplate> <DataTemplate> <Grid> <TextBlock Text="{Binding Name}"/> </Grid> </DataTemplate> </ListBox.ItemTemplate> </ListBox> </StackPanel> </Grid> 并通过REST的JSON发送它。如果没有其他页面可以获取,我想禁用“显示更多”按钮。有一个'frameworkie'特定的方法来检索这个数字,或者我应该使用Page<myEntity> result = myRepo.findByAttr(attr, page); 并计算这个列表?

2 个答案:

答案 0 :(得分:4)

这是Page接口

的源代码
public interface Page<T> extends Slice<T> {

    /**
     * Returns the number of total pages.
     * 
     * @return the number of total pages
     */
    int getTotalPages();

    /**
     * Returns the total amount of elements.
     * 
     * @return the total amount of elements
     */
    long getTotalElements();

    /**
     * Returns a new {@link Page} with the content of the current one mapped by the given {@link Converter}.
     * 
     * @param converter must not be {@literal null}.
     * @return a new {@link Page} with the content of the current one mapped by the given {@link Converter}.
     * @since 1.10
     */
    <S> Page<S> map(Converter<? super T, ? extends S> converter);
}

您有getTotalPages()

答案 1 :(得分:3)

使用result.getTotalElements()获取匹配元素的总数。

使用result.getTotalPages()获取总页数。

P.S。使用result.getContent()将内容设为列表&lt;&gt;