drs7798 2014-11-29 18:58
浏览 62

数据表行选择 - PHP 5.2上的匿名函数

I'm using this Datatables example, on my server with a PHP version 5.2.

I know anonymous functions cannot be used, like the 'Server-side script' in the example on line 35, but I need some kind of solution for that, since I can't upgrade my server.

This is the problem:

array( 'db' => 'id', 'dt' => 'DT_RowId', 'formatter' => function( $d, $row ) { // Technically a DOM id cannot start with an integer, so we prefix // a string. This can also be useful if you have multiple tables // to ensure that the id is unique with a different prefix return 'row_'.$d; }),

Does any one have a solution for that function($d, $row)?

  • 写回答

1条回答 默认 最新

  • doudeng9425 2014-11-29 19:24
    关注

    'Maybe you can use array_map:

    array_map("function", array('db' => 'id', ...)),

    then define function to do what you want to the 'formatter' element, but leaving all other elements as is.

    I'll try to elaborate, here is the suggested code (I'm assuming that only $d is an integer, if not there may be another recognizer you could use instead of is_int()):

    array_map(
      function($val) {$val = is_int($val) ? 'row_'.$d : $val}, 
      array('db' => 'id', 'dt' => 'DT_RowId','formatter' => $d)
    )
    

    Depending on which php version you are using, you may have to define a non-anonymous function.

    Here is a working example which should work in 5.2:

    <?php
      $d = 10;
    
      function func($val) {
        return is_int($val) ? 'row_'.$val : $val;}
    
      print_r(array_map(     
        "func", 
        array('db' => 'id', 'dt' => 'DT_RowId','formatter' => $d)
       ));
    ?>
    

    This outputs:

    Array ( [db] => id [dt] => DT_RowId [formatter] => row_10 )
    
    评论

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程