0

嗨,我在尝试了解如何将数据表与服务器端处理一起使用时遇到了一些重大问题。对于某些背景,我正在使用服务调用 Gamesparks 来创建视频游戏的后端,并且在该服务中他们有一个 mongodb 的实现。

我有一个端点可以获取我的所有用户,我可以在我的表格中看到它们,但问题是我获取了所有用户,我怎样才能实现分页?在文档中,他们声明我们必须将 serverSide 设置为 true 但不起作用。我真的不知道如何继续我需要帮助。

Gamesparks 事件以获取所有用户

require("LeaderboardMethods");

var playerList = Spark.runtimeCollection("playerList").find({},{"_id":0});

var finalData = [];

while(playerList.hasNext()){
    var current = playerList.next();
    var playerStats = Spark.runtimeCollection("playerStatistics").findOne({
        "playerId":current.playerId
    });

    var loadedPlayer = Spark.loadPlayer(current.playerId);
    var score = getScore(current.playerId);
    if(loadedPlayer === null){
        var toReturn = {
            "playerId": current.playerId,
            "displayName": current.displayName,
            "email": "DELETED",
            "rank": current.rank,
            "coins": "DELETED",
            "ban": "DELETED",
            "score": score
        }
        finalData.push(toReturn);
    } else{
        var coins = loadedPlayer.getBalance("COIN");

        var toReturn = {
            "playerId": current.playerId,
            "displayName": current.displayName,
            "email": current.email,
            "rank":playerStats.rank,
            "coins": coins,
            "ban": playerStats.isBlocked,
            "score":score

        }
        finalData.push(toReturn);

    }

}

Spark.setScriptData("playerList",finalData);

数据表调用

App.getUsers = function(){
var bodyData = {
  "@class": ".LogEventRequest",
  "eventKey": "GET_PLAYER_DATA",
  "playerId": "MY_ID"
}

var table = $('#table1').DataTable({
  "dom": "<'row be-datatable-header'<'col-sm-4'l><'col-sm-4'B><'col-sm-4'f>>" +
    "<'row be-datatable-body'<'col-sm-12'tr>>" +
    "<'row be-datatable-footer'<'col-sm-5'i><'col-sm-7'p>>",
  "buttons": [
    {
      text: 'Edit',
      action: function (e, dt, node, config) {
        var sel_row = table.rows({
          selected: true
        }).data();

        if (sel_row.length != 0) {
          window.location.href = "edit-user.html";
          localStorage.setItem("editUser", JSON.stringify(sel_row[0]));
        }

      }
    },
    {
      text: 'Create',
      action: function (e, dt, node, config) {
        window.location.href = "create-user.html";
      }
    },
    {
      text: 'Delete',
      className: 'delete-btn',
      action: function (e, dt, node, config) {
        var filtered = table.rows({
          filter: 'applied',
          selected: true
        }).data();

        // Only open modal when are users selected
        if(filtered.length != 0){
          $("#proceed-delete").prop('disabled', true)
          $("#mod-danger-delete").modal();

          if(filtered.length != 1) {
            $('#length-users').append(document.createTextNode(filtered.length + " users"));
          } else {
            $('#length-users').append(document.createTextNode(filtered.length + " user"));
          }

          $("#delete-confirmation").change(function () {
            if ($("#delete-confirmation").val() === "DELETE"){
              $("#proceed-delete").prop('disabled', false)
              $('#proceed-delete').on('click', function () {
                if (filtered.length === 1) {
                  deleteUserRequest(filtered[0]);
                } else {
                  for (let index = 0; index < filtered.length; index++) {
                    deleteUserRequest(filtered[index])
                  }
                }
              });
            }
          });
        }
      }
    }, 'selectAll', 'selectNone'
  ],
  "paging":true,
  "pageLength":50,
  "serverSide":true,
  "ajax": {
    "data": function (d) {
      return JSON.stringify(bodyData);
    },
    "contentType": "application/json; charset=utf-8",
    "url": config.REQUEST_API + '/rs/' + config.API_CREDENTIAL_SERVER + '/' + config.API_SERVER_SECRET + '/LogEventRequest',
    "type":"POST",
    "dataSrc":function(json){
      console.log(json);

      $('#loading-row').removeClass('be-loading-active');
      return json.scriptData.playerList
    },
  },
  "columns": [
        {
          data: null,
          defaultContent: "<td></td>",
          className: 'select-checkbox'
        },
        { data: "playerId"},
        { data: "displayName" },
        { data: "email" },
        { data: "score"},
        { data: "rank" },
        { data: "isBlocked" },
        { data: "coins" },
        {
          "data": null,
          "defaultContent": "<button class='btn btn-space btn-primary' onclick='App.click()'>View more</button>"
        }
      ],
  "select": {
    style: 'multi',
    selector: 'td:first-child'
  },
}).on('error.dt', function(e, settings, techNote, message){
  var err = settings.jqXHR.responseJSON.error;
  // GS err
  if(err === "UNAUTHORIZED"){
    location.href = "pages-login.html";
    return true;
  } else{
    $('#error-container-dt').show();
    console.log(message);
    return true;
  } 
});

}

4

1 回答 1

0

快速查看 Gamesparks SDK 并找到以下示例:

ListTransactionsRequest
  dateFrom      Optional date constraint to list transactions from
  dateTo        Optional date constraint to list transactions to
  entryCount    The number of items to return in a page (default=50)
  include       An optional filter that limits the transaction types returned
  offset        The offset (page number) to start from (default=0)

现在,对于分页,您需要 entryCount 和 offset。首先是一页的大小,默认50,你可以改变它。服务器返回“entryCount”记录数。

偏移量是起始记录。例如,初始列表(第 1 页)确实有 50 条记录,单击“下一步”按钮将向服务器发送请求“偏移量:51”。并且服务器回复记录从 50(偏移)到 100(偏移 + entryCount)。

var bodyData = {
  "@class": ".LogEventRequest",
  "eventKey": "GET_PLAYER_DATA",
  "playerId": "MY_ID",
  "entryCount": entryCount,  // you have to send it if you dont like the default value
  "offset": offset  // gets his value from "NEXT" or "PREV" button 
}

这就是分页的工作原理。由于我自己不使用 Gamesparks,因此我无法给出更详细的答案。希望它至少能给你一些指导。

于 2018-10-12T19:46:41.840 回答