首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我是否可以遍历datatable列对象并添加+2的新行

我是否可以遍历datatable列对象并添加+2的新行
EN

Stack Overflow用户
提问于 2019-02-02 18:27:30
回答 1查看 254关注 0票数 0

我正在使用ajax jquery从mysqli表中获取数据,并使用列对象将其作为行填充到Datatable中,我是否能够循环通过从服务器获得的数据,然后通过我定义的X数字添加新的行。

我已经尝试过了,但是使用这段代码,我在X次循环运行中得到了相同的行,但没有在其他行中添加任何类型的增量。

我从mysql获取数据的PHP API

代码语言:javascript
复制
if (isset($_POST['what_need']) AND $_POST['what_need'] == 'signup_getslabs') {

  $getTotalcountry = $obj -> getSlabSignup($_POST['placeid'], $_POST['range']);
  $return_ardr = array();

  foreach($getTotalcountry as $row) {
    $same_city_per_kg = $row['wc_slabr_hkg'];
    $diff_city_per_kg = $row['dc_slabr_hkg'];
    $fnfsamecityperkg = $same_city_per_kg - 50;
    $fnfdifferentcityperkg = $diff_city_per_kg - 50;
    $id = $row['id_slabr'];

    for ($i = 3; $i >= 0; $i--) {
      $samecit = $fnfsamecityperkg + 50;
      $diffcit = $fnfdifferentcityperkg + 50;
      $return_ardr[] = array(
        "samecity" => $samecit,
        "diffcity" => $diffcit,
        "weight" => 0.5,
        "idslab" => $id
      );
    }
  }

  function utf8ize($d) {
    if (is_array($d)) {
      foreach($d as $k => $v) {
        $d[$k] = utf8ize($v);
      }
    } else if (is_string($d)) {
      return utf8_encode($d);
    }
    return $d;
  }

  echo json_encode(utf8ize($return_ardr));
}

我的datatable代码,它将ajax请求发送到api,并在id定义的表中打印数据

代码语言:javascript
复制
$('#ratestd').DataTable({
  "bDestroy": true,
  "serverSide": false,
  "ajax": {
    "url": "ajax-requests/ajaxm.php",
    "type": "POST",
    "dataSrc": function(d) {
      return d
    },
    "dataType": "json",

    "data": function(data) {
      data.what_need = 'signup_getslabs';
      data.placeid = placeid;
      data.range = range;
    }

  },
  dom: 'Bflrtip',
  "buttons": [
    'copyHtml5', 'excelHtml5', 'pdfHtml5', 'csvHtml5', 'colvis'
  ],
  "columns": [{
      "data": "weight"
    },
    {
      "data": "samecity"
    },
    {
      "data": "diffcity"
    }
  ]
});

我希望得到这样的输出

代码语言:javascript
复制
WEIGHT     SAMECITY   DIFFERENT CITY
0.5 KG     50         100
1.0 KG     100        150
1.5 KG     150        200
2.0 KG     200        250

但我得到的是

代码语言:javascript
复制
WEIGHT     SAMECITY   DIFFERENT CITY
0.5 KG     50         100
0.5 KG     50         100
0.5 KG     50         100
0.5 KG     50         100

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-02-02 19:01:12

问题看起来像是来自于你的for循环。

代码语言:javascript
复制
for ($i = 3; $i >= 0; $i--) {
  $samecit = $fnfsamecityperkg + 50;
  $diffcit = $fnfdifferentcityperkg + 50;
  $return_ardr[] = array(
    "samecity" => $samecit,
    "diffcity" => $diffcit,
    "weight" => 0.5,
    "idslab" => $id
  );
}

假设这是您创建4个值的地方,您可能需要尝试如下操作:

代码语言:javascript
复制
$samecit = $fnfsamecityperkg;
$diffcit = $fnfdifferentcityperkg;
$weight = 0;

for ($i = 3; $i >= 0; $i--) {
    $samecit +=  50;
    $diffcit +=  50;
    $weight += 0.5;
    $return_ardr[] = array(
        "samecity" => $samecit,
        "diffcity" => $diffcit,
        "weight" => $weight,
        "idslab" => $id
    );
}

返回:

代码语言:javascript
复制
array (size=4)
  0 => 
    array (size=4)
      'samecity' => int 50
      'diffcity' => int 100
      'weight' => float 0.5
      'idslab' => null
  1 => 
    array (size=4)
      'samecity' => int 100
      'diffcity' => int 150
      'weight' => float 1
      'idslab' => null
  2 => 
    array (size=4)
      'samecity' => int 150
      'diffcity' => int 200
      'weight' => float 1.5
      'idslab' => null
  3 => 
    array (size=4)
      'samecity' => int 200
      'diffcity' => int 250
      'weight' => float 2
      'idslab' => null
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54492125

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档