Append html to dataTable

时间:2018-01-15 18:19:16

标签: php jquery html ajax datatable

I have an ajax function whose response is in the form of html and has the following contents.

<tr><td>data1</td>td>data1</td>td>data3</td></tr>

Is it possible to add the response to dataTable row like this

$.ajax({
                type:"POST",
                url:myurl,
                data:myformdata,
                cache: false,
                contentType: false,
                processData: false,
                success:function(hasil){  
                    if($.parseHTML(hasil))
                    { 
                        if($(hasil).filter('table').length)
                        {
                          $('#lstNews').DataTable().row.
add($(hasil).filter('table').html()).draw();
                        }

                    }
         });

In the google I have found that I need to make the response to the following format for adding it to dataTable

$('#lstNews').DataTable().row.add(
   [[ "data1", "data2","data3" ]
     ]
).draw();

Please help me

1 个答案:

答案 0 :(得分:1)

您可以在响应中使用jQuery's parseHTML function,并使用maping将每个td的html /数据提取到数组中。

编辑:删除非ES6支持的箭头功能。

@Service
public class UserServiceImpl implements UserService {

public Response addUser(Users user){
   //get details from Users and populate to entities User and UserDetails.
   // Save User and UserDetails. Return Response
 }
 }
var str = '<tr><td>data1</td><td>data1</td><td>data3</td></tr>';

var html = $.parseHTML(str);

console.log($('td', html).map(function(_, el) {
  return $(el).html()
}));