【问题标题】:How to pull data out from datatable如何从数据表中提取数据
【发布时间】:2019-09-27 09:45:07
【问题描述】:

我已经创建了一个点击函数来显示模态......

在 modal 内部,我创建了一个数据表来显示 item_id 和数量。

如果我想将其他数据拉入模态...如何制作...

在 console.log 显示我仍然有 item_name 等可以拉但现在我只知道如何设置内部数据表。

此代码可用于单击 tbody 然后显示模式。简单易用。

  $('#stock-enquiry-table tbody').on('click', 'tr', function () {
          var data = table.row(this).data();
          **console.log(data);**

          $('#selected-item-id').val(data.item_id);
          table_item.ajax.reload();

          $('#modal-content').modal({
            show: true
          });
      });

请教显示模态内的数据,而不是数据表中的数据。 谢谢

【问题讨论】:

    标签: ajax laravel datatable


    【解决方案1】:

    你似乎完成了一半,实际上已经完成了一半

    你拥有变量data中的所有数据

    您只需要编写 javascript 将数据设置为模态。

    我建议为它编写一个通用函数。

    function setDataInModal(data) {
       $('#input1').val(data.key1);
       $('#input2').val(data.key2);
       $('#input3').val(data.key3);
       ...
    }
    

    调用$('#modal-content').modal({show: true});之前的函数


    在 cmets 对话后更新

    var data = [{
        item_id: "2",
        item_code: "PA",
        name: "Product A",
        short_description: null,
        description: null
      },
      {
        item_id: "3",
        item_code: "PAA",
        name: "Product AA",
        short_description: null,
        description: null
      },
      {
        item_id: "4",
        item_code: "PAAA",
        name: "Product AAA",
        short_description: null,
        description: null
      }
    ]
    
    $('#table').DataTable({
      data: data,
      columns: [{
          data: 'item_id',
          title: 'id'
        },
        {
          data: 'item_code',
          title: 'code'
        },
        {
          data: 'name',
          title: 'name'
        },
        {
          title: 'actions',
          render: function(data, type, row) {
            return '<button class="btn btn-primary" onclick="showEditModal(this)">edit</button>'
          }
        }
      ]
    })
    
    function showEditModal(el) {
      var data = $(el.closest('table')).DataTable().row(el.closest('tr')).data();
      
      $('#modal #id').val(data.item_id);
      $('#modal #code').val(data.item_code);
      $('#modal #name').val(data.name);
      
      $('#modal').modal('show');
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/datatables/1.10.19/css/jquery.dataTables.min.css" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/datatables/1.10.19/js/jquery.dataTables.min.js"></script>
    <!-- Latest compiled and minified CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
    
    <!-- Optional theme -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
    
    <!-- Latest compiled and minified JavaScript -->
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
    <table id="table"></table>
    
    <div class="modal fade" id="modal" tabindex="-1" role="dialog">
      <div class="modal-dialog" role="document">
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            <h4 class="modal-title">Modal title</h4>
          </div>
          <div class="modal-body">
            <input type="text" id="id" placeholder="id">
            <input type="text" id="code" placeholder="code">
            <input type="text" id="name" placeholder="name">
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            <button type="button" class="btn btn-primary">Save changes</button>
          </div>
        </div><!-- /.modal-content -->
      </div><!-- /.modal-dialog -->
    </div><!-- /.modal -->

    【讨论】:

    • data.key1 表示像我有{item_id:1, item_name:product 等等},如果得到 val.(data.key1) 那么它将采用第一个 item_id 对吗?
    • 如果您觉得我的回答有帮助,请点赞或接受答案。这对我也有帮助。
    • 我如何放入模型中?需要打开
      ?要么 ?我是初学者,非常愚蠢。请帮助
    • 你能给我一个你在运行var data = table.row(this).data();时得到的数据的例子@
    • 控制台显示 {item_id: "2", item_code: "PA", name: "Product A", short_description: null, description: null}
    猜你喜欢
    相关资源
    最近更新 更多
    热门标签