首页 > 解决方案 > DataTables 无法读取未定义的属性“行”

问题描述

更新成功后,我尝试在表中显示我的新数据,但它返回

无法读取未定义的属性“行”

代码

Script

<script>
    $(function() {
        $.ajaxSetup({
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            }
        });
        var table = $("#dataTableLocations").DataTable();
        $('#dataTableLocations').on('click', '.locationUpdate', function(e){
            e.preventDefault();
            var ordID = $(this).data('id');
            var row = $(this).closest('tr');

            var province_id = $(this).closest("form").find("#province_idLocationUpdate").val();
            var city_id = $(this).closest("form").find("#city_idLocationUpdate").val();
            var name = $(this).closest("form").find("#nameLocUpdate").val();
            var address = $(this).closest("form").find("#addressLocationUpdate").val();
            var postalCode = $(this).closest("form").find("#postalCodeUpdate").val();
            var seqNo = $(this).closest("form").find("#seqNoUpdate").val();

            $.ajax({
                type:'PUT',
                url:'{{url('dashboard/customer-locations')}}/'+ordID,
                data:{
                    province_id:province_id,
                    city_id:city_id,
                    name:name,
                    address:address,
                    postalCode:postalCode,
                    seqNo:seqNo
                },
                success:function(data){
                    console.log('check data', data); // see the screenshot below
                    console.log('check row', row); // see the screenshot below



                    table.cell( row, 0 ).data( data.data.seqNo );
                    table.cell( row, 1 ).data( data.data.name );
                    table.cell( row, 2 ).data( data.data.customer.name );
                    table.cell( row, 3 ).data( data.data.province.name );
                    table.cell( row, 4 ).data( data.data.city.name );
                    table.cell( row, 5 ).data( data.data.address );
                    table.cell( row, 6 ).data( data.data.postalCode );
                    $('.editModalLocation').modal('hide');
                    $(".customerMessage").append('<div class="alert alert-success fade in">'+data.success+'</div>').hide(4000);
                }
            });

        });
    });
</script>

Table

<table id="dataTableLocations" class="table table-striped table-bordered">
    <thead>
    <tr>
        <th width="30">Seq No.</th>
        <th>Name</th>
        <th>Customer</th>
        <th>Province</th>
        <th>City</th>
        <th>Address</th>
        <th>Postal Code</th>
        <th width="120">Options</th>
    </tr>
    </thead>
    <tbody id="table_dataLocations"></tbody>
</table>

截屏

一

谁能告诉我这段代码有什么问题?

标签: javascriptdatatables

解决方案


尝试改变这个:

var row = $(this).closest('tr')

改成

var row = table.row(this);


推荐阅读