Load Array JSON into data Tables

Load Array JSON into data Tables

xytaxyta Posts: 3Questions: 2Answers: 0
edited October 2018 in Free community support

Hello, i have a soap web service, it's using xml version. and i've tried to parse with json encode to be json format. Then i want to load json format into data tables server side.

this is my cont in laravel

public function datatablesStatusTKI(Request $request){  
            $this->validate($request, [
            'nama' => 'required',
            'negara' => 'required',
            'pencarian'=>'required'
            ]);

            $count = Pengaturan::where('key', 'visitor')        
            ->first();
            $array=array_map('intval', str_split($count->value));

            $kategori = $request->input("pencarian");
            $nama = $request->input("nama");
            $negara = $request->input("negara");
            $pages = $request->get("pageby");

            $soapClient = new SoapClient("http://interop.bnp2tki.go.id/perwakilanWS/wsdl"); 
            $param;     
            if($kategori=="2"){ //search by paspor
                $param = array( 
                'user_id'=>'kjri_jed', 
                'user_pass'=>'123',
                'paspor'=>$nama         
                );

                $error = 0; 
                try { 
                    $info = $soapClient->__call("ws_view_tkibypaspor", $param);         
                    if($info!="notfound"){                  
                        $informasi = preg_replace('/&(?!#?[a-z0-9]+;)/', '&', $info);
                        $xml = new SimpleXMLElement($informasi);
                        $data = $xml->tki_data;
                        $pagess = $xml->total_page;
                        $json = json_encode($data);
                        return $json;

                        }else{
                        $data=$info;
                        return $data;           
                    }
                    } catch (SoapFault $fault) { 
                    return view('page.search-tki', compact('data', 'array'));
                } 

                }else{ //by nama
                $param = array( 
                'user_id'=>'kjri_jed', 
                'user_pass'=>'123',
                'nama'=>$nama,
                'negara'=>$negara,
                'page'=>$pages          
                );
                $error = 0; 

                try { 
                    $info = $soapClient->__call("ws_view_tkibyname", $param); 
                    if($info!="notfound"){
                        $informasi = preg_replace('/&(?!#?[a-z0-9]+;)/', '&', $info);
                        $xml = new SimpleXMLElement($informasi);
                        $data = $xml->tki_data;
                        $pagess = $xml->total_page;
                        $json = json_encode($data);
                        return $json;
                        //return Datatables::of($json)->make(true);
                        //return Datatables::eloquent($data)->make(true);

                        }else{
                        $data=$info;
                        return $data;               
                    }
                    } catch (SoapFault $fault) { 
                    return view('page.search-tki', compact('data', 'array'));
                }   
            }       
        }

And its my java script data tables

    <script>
        //alert(""+"{{ $nama }}");
        $(function () {

            var ObjDt = $(".bnp-table").DataTable({
                processing: true,
                serverSide: true,
                ajax: {
                    url: "{{ url('/cekstatustki')}}",
                    dataType: 'json',
                    type: "POST",
                    "data": function ( d ) {
                        d.pencarian = "{{ $kategori }}";
                        d.nama = "{{ $nama }}";
                        d.negara = "{{ $negara }}";
                        d.page = "1";
                        // d.custom = $('#myInput').val();
                        // etc
                    }
                },
                columns: [
                {data: 'id', name: 'id'},
                {data: 'TKI_TKINAME', name: 'TKI_TKINAME'},
                {data: 'TKI_TKICITYDESC', name: 'TKI_TKICITYDESC'},
                {data: 'TKI_NEGARADESC', name: 'TKI_NEGARADESC'},
                {data: 'TKI_PJTKIDESC', name: 'TKI_PJTKIDESC'},
                {data: 'TKI_PJTKADESC', name: 'TKI_PJTKADESC'},
                {data: 'TKI_STATUS', name: 'TKI_STATUS'},
                {data: 'TKI_TANGGAL', name: 'TKI_TANGGAL', searchable: false},
                ],

                "oLanguage": {
                    "sProcessing":   "Sedang memproses...",
                    "sLengthMenu":   "Tampilkan _MENU_ entri",
                    "sZeroRecords":  "Tidak ditemukan data yang sesuai",
                    "sInfo":         "Menampilkan _START_ sampai _END_ dari _TOTAL_ entri",
                    "sInfoEmpty":    "Menampilkan 0 sampai 0 dari 0 entri",
                    "sInfoFiltered": "(disaring dari _MAX_ entri keseluruhan)",
                    "sInfoPostFix":  "",
                    "sSearch":       "Cari:",
                    "sUrl":          "",
                    "oPaginate": {
                        "sFirst":    "Pertama",
                        "sPrevious": "Sebelumnya",
                        "sNext":     "Selanjutnya",
                        "sLast":     "Terakhir"
                    }
                },
                "rowReorder": {
                    "selector": 'td:nth-child(2)'
                },
                "responsive": true
            } );


             ObjDt.on( 'search.dt order.dt page.dt', function () {
                var info = ObjDt.page.info();
                var length = ObjDt.column( 0 ).data().length;
                ObjDt.column(0, {search:'applied', order:'applied'}).nodes().each( function (cell, i) {
                    cell.innerHTML = (info.page)*length+i+1;
                });
            }).draw();
            $.fn.dataTable.ext.errMode = 'throw'; 
        });
    </script>

Can any one help me to load array in data tables? please :smile:
I'm a newbie using data tables, and the url in ajax direct to function that i have shared.

Answers

  • kthorngrenkthorngren Posts: 21,303Questions: 26Answers: 4,947

    Not sure what the JSON string is that your code produces. Maybe you can post an example. You can use the troubleshooting steps in this tech note to see what is being returned.
    https://datatables.net/manual/tech-notes/1

    What errors are you getting?

    Do you see any errors in your browser's console?

    Kevin

  • xytaxyta Posts: 3Questions: 2Answers: 0

    Thank you for replying, it has resolved clearly.

This discussion has been closed.