【问题标题】:Splitting SQL query returned records into an array将 SQL 查询返回的记录拆分为数组
【发布时间】:2016-09-19 15:00:48
【问题描述】:

嘿,我想将返回的查询记录拆分为“分页”类型的东西,因此我想显示前 51 条记录,然后将其余记录添加到数组中,以便稍后在用户想要移动时获取到另一个页面。

我可以在页面上很好地显示前 51 个结果,但我无法找到将其余记录除以 51 的方法。

到目前为止我的代码:

var arrayHTML       = [];

for(var key in data){
    if(data.hasOwnProperty(key)) {
        if (totalX >= 51) {
            //Start putting the results into an array. 51 results per array
        }else if (x > 1) {
            x = 0;
            _userName = data[key].fname + ' ' + data[key].lname;

            populateCards(_userName, 
                          data[key].email, 
                          data[key].img, 
                          data[key].school, 
                          data[key].userID, 
                          true,
                          data[key].encoded);
        } else {
            _userName = data[key].fname + ' ' + data[key].lname;

            populateCards(_userName, 
                          data[key].email, 
                          data[key].img, 
                          data[key].school, 
                          data[key].userID, 
                          false, 
                          data[key].encoded);
            x++;
        }

        totalRowCnt = data[key].totalRows;
        _tmpMath = Math.round(totalRowCnt / totalNum);
        total++;
        totalX++;

        console.log('totalX: ' + totalX)
    }
}

在达到 51 之后,它进入 if (totalX >= 51) {,这就是我试图弄清楚如何将其余部分分成 51 个每个数组插槽的地方.

上面的代码一直在循环,直到它到达每个 3rd 记录,然后在其后放置一个
,这样它就有一行 3 条记录,然后它就一直这样做,直到达到记录 51。所以 17 行,每行 3 条记录。 true 告诉函数将
strong> 最后,而 false 告诉函数不要放

任何帮助都会很棒!

【问题讨论】:

    标签: javascript sql arrays sql-server sql-server-2014


    【解决方案1】:

    JavaScript 代码如下:

    将此函数添加到您的代码中:

    // this is a simple pager function that return part of the array you want
    //  so you can easily loop over it
    // @param page you want
    // @param how many values you want
    // @return the sliced array with the parts you want
    //     (this will return an empty array if your page is out of bound
    //       e.g. when you array only contains less than 51 and you tell it you want page 2)
    array_pager = function ( array, page, show ) {
       var start = (page -1) * show; // this sets the offset
       return array.slice( start, start + show);
    }
    

    你可以像这样使用这个函数:

    // assuming the array's name is data
    var this_page = array_pager( data, 1, 51); // get this_page 1, with 51 values
    
    // then you can safely loop over it
    for(var key in this_page) {
      _userName = this_page[key].fname + ' ' + this_page[key].lname;
    
      populateCards(_userName, 
                    this_page[key].email, 
                    this_page[key].img, 
                    this_page[key].school, 
                    this_page[key].userID, 
                    false, 
                    this_page[key].encoded);
    }
    
    // for page 2: array_pager( data, 2, 51) ... 
    // 10 per page: array_pager( data, 1, 10) ... I think you get it now
    

    【讨论】:

      猜你喜欢
      • 2016-11-12
      • 2019-08-02
      • 2013-10-31
      • 1970-01-01
      • 2014-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-18
      相关资源
      最近更新 更多