douji5523 2017-02-08 08:09
浏览 85
已采纳

数据表的X可编辑在值更改时执行操作[重复]

This question already has an answer here:

I have created the following php file to immitate an ajax request:

<?php

$data=[
"key0"=>["item1"=>1,"item2"=>2],
"key1"=>["item1"=>11,"item2"=>21],
"key3"=>["item1"=>101,"item2"=>221],
];

header('Content-Type: application/json');
echo json_encode($data);

And I have created the following html with javascript:

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8"/>
  <link rel="stylesheet" type="text/css" href="//cdn.datatables.net/1.10.13/css/jquery.dataTables.min.css">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

  <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
  <script
              src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"
              integrity="sha256-VazP97ZCwtekAsvgPBSUwPFKdrwD3unUfSGVYrahUqU="
              crossorigin="anonymous"></script>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>

  <link href="//cdnjs.cloudflare.com/ajax/libs/x-editable/1.5.0/bootstrap3-editable/css/bootstrap-editable.css" rel="stylesheet"/>
  <script src="//cdnjs.cloudflare.com/ajax/libs/x-editable/1.5.0/bootstrap3-editable/js/bootstrap-editable.min.js"></script>

  <script src="//cdn.datatables.net/1.10.13/js/jquery.dataTables.min.js"></script>
  <script type="text/javascript">

   var datatable=null;

   var initDatatable=function(){

     if(datatable===null){
        datatable=$("#t1").DataTable({
          createdRow:function(row,data,dataIndex){

                var item=data[data.length-1];
                var editableItem=document.createElement("a");
                $(editableItem).text(item);

                var lastChild=$(row).children().last();
                lastChild.text("");
                lastChild.append(editableItem);

                $(editableItem).editable(function(data){
                  console.log("Hello");
                });
          }
        });

     } else {
       datatable.clear();
     }
   }

    $(document).ready(function(){
      $.ajax({
        method:"GET",
        data:{},
        url:"api.php",
        success:function(data){
          initDatatable();
          Object.keys(data).forEach(function(key,index){
            datatable.row.add([key,data[key]['item1'],data[key]['item2']]);
          });
          datatable.draw(false);
        }
      })
    })
  </script>
</head>
<body>
  <table id="t1">
    <thead>
      <tr>
        <th>Column 1</th>
        <th>Columnt 2</th>
        <th>Column 3</th>
      </tr>
    </thead>
    <tbody>
    </tbody>
  </table>
</body>
</html>

What I am trying to do is when I change a value on the last table of the column to print on the console the word 'Hello'. As seen in:

    $(editableItem).editable(function(data){
         console.log("Hello");
    });

But for some reason I could not make it work and I can't figure out why. Can you help me?

</div>
  • 写回答

1条回答 默认 最新

  • dongwei2983 2017-02-08 08:27
    关注

    On createdRow callback replace the:

     $(editableItem).editable(function(data){
             console.log("Hello");
        });
    

    With:

     $(editableItem).editable({
      mode: "inline"//OPtional PArameter
     });
    
     $(editableItem).on('save',function(e,params){
       console.log("Hello);
      // For cleaner code use external function.
     })
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog