简体   繁体   English

在表中显示来自JavaScript的数组数据

[英]Displaying array data from JavaScript in table

In controller function I have multidimensional array say $jobs I am getting this array in view's JavaScript success function alert(data) . controller函数中,我有一个多维数组,说$jobs我正在view's JavaScript成功函数alert(data)获得此数组。

I have to pass this array to JavaScript success function and display this array data in table view without reloading page, how can I do this? 我必须将此数组传递给JavaScript成功函数,并在表视图中显示此数组数据,而无需重新加载页面,我该怎么办? I tried to convert array to json, but nothing is working, what is right way to do it? 我试图将数组转换为json,但是没有任何效果,正确的方法是什么?

Below is my JavaScript code: 以下是我的JavaScript代码:

function jobsearch()
{
     var form=$("#jobSearchForm")              
     $.ajax({
            type: 'POST',
            url: '/jobs/processjobsearch/',
            dataType: 'json',
            data: form.serialize(),
            success: function(data){
                alert(data);
                var json = JSON.parse(data);
            $.each(json.jobs, function(index, value) { alert(value);
                $.each(value, function(index, value) {
                    $("#data").append("<tr><td>" + value + '</td></tr>');
                });
            });
            $('#errorr-msg').html(json.errormsg);
           }        
        }); 
}

in alert data I am getting this array 在警报数据中,我得到这个数组

Array
(
    [0] => Array
        (
            [id] => 3
            [jsp_title] => efsdf
            [jsp_subtitle] => sdfsdfdfs
            [jsp_desc] => dsfdfsdf
            [jsp_uid] => 1
            [jsp_ex_id] => php
            [jsp_date] => 2015-06-18 12:13:43
            [jsp_stdate] => sdfsdf
            [jsp_endate] => 
            [jsp_nature] => 0
            [jsp_location] => 
            [jsp_active] => 0
            [jsp_etype] => 2
        )

    [1] => Array
        (
            [id] => 4
            [jsp_title] => java devloper
            [jsp_subtitle] => core java advance java
            [jsp_desc] => all java related technologies
            [jsp_uid] => 1
            [jsp_ex_id] => java
            [jsp_date] => 2015-06-18 12:51:21
            [jsp_stdate] => 213123123
            [jsp_endate] => 123123123
            [jsp_nature] => 0
            [jsp_location] => nagar
            [jsp_active] => 0
            [jsp_etype] => 3
        )

)

Edit : Json data after json_encode($jobs) 编辑:json_encode($ jobs)之后的Json数据

[{"id":"1","jsp_title":"php developer","jsp_subtitle":"","jsp_desc":"develop ecommerce site","jsp_uid"
:"1","jsp_ex_id":"1,3","jsp_date":"2015-06-18 12:14:54","jsp_stdate":"","jsp_endate":"","jsp_nature"
:"1","jsp_location":"pune","jsp_active":"1","jsp_etype":"5"},{"id":"5","jsp_title":"web devloper","jsp_subtitle"
:"design , backend ,fruntend","jsp_desc":"2-4 year Exprience in\nPhp,html5,CssAjax...","jsp_uid":"1"
,"jsp_ex_id":"1|2,3,4","jsp_date":"2015-06-18 12:14:35","jsp_stdate":"11112015","jsp_endate":"11112015"
,"jsp_nature":"0","jsp_location":"baner, pune","jsp_active":"0","jsp_etype":"4"}]

If you are getting that data in the response, then you need to use: 如果要在响应中获取数据,则需要使用:

json_encode($array);

That converts the print_r() output into a JavaScript readable JSON. print_r()输出转换为JavaScript可读的JSON。 Then you can use your JavaScript / jQuery function that you have posted to get things on the table. 然后,您可以使用发布的JavaScript / jQuery函数来获取所需的信息。

Based on the JSON data, you need to update your success function: 根据JSON数据,您需要更新success函数:

success: function(data){
  for (var i = 0; i < data.length; i++) {
    tr = $('<tr/>');
    var srno = i+1;
    tr.append("<td>" + srno + "</td>");
    tr.append("<td>" + data[i].jsp_title  + "<br>" + data[i].jsp_subtitle  + "<br>" + data[i].jsp_desc  + "<br>" + data[i].jsp_date  + "</td>");
    $('#data').append(tr);
  }
}

After refering below, link changes in sucess function works for Me : 在参考以下内容后,成功函数中的链接更改对我有效:

https://stackoverflow.com/questions/17066636/parsing-json-objects-for-html-table https://stackoverflow.com/questions/17066636/parsing-json-objects-for-html-table

Also changed dataType attribute in ajax call to 'json' 还将ajax调用中的dataType属性更改为“ json”

dataType: 'json'

success function 成功功能

success: function(data){
                    for (var i = 0; i < data.length; i++) {
                    tr = $('<tr/>');
                    var srno = i+1;
                    tr.append("<td>" + srno + "</td>");
                    tr.append("<td>" + data[i].jsp_title  + "<br>" + data[i].jsp_subtitle  + "<br>" + data[i].jsp_desc  + "<br>" + data[i].jsp_date  + "</td>");
                    $('#data').append(tr);
                }
                   } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM