Display JSON Data in HTML Table

后端 未结 9 2123
既然无缘
既然无缘 2020-11-27 03:47

I get the following JSON String from server as response

[{\"city\":\"AMBALA\",\"cStatus\":\"Y\"},{\"city\":\"ASANKHURD\",\"cStatus\":\"Y\"},{\"city\":\"ASSA         


        
9条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-27 04:36

    As an alternative to the answers you already have, and for others that come accross this post. I recently had a similar task and created a small jquery plug in to do it for me. Its pretty small under 3KB minified, and has sorting, paging and the ability to show and hide columns.

    It should be pretty easy to customize using css. More information can be found here http://mrjsontable.azurewebsites.net/ and the project is available for you to do as you wish with on github https://github.com/MatchingRadar/Mr.JsonTable

    To get it to work download the files and pop them in your site. Follow the instructions and you should end up with something like the following:

    Then in the ajax success method you will want something like this

    success: function(data){ 
        $("#citytable").mrjsontable({
            tableClass: "my-table",
            pageSize: 10, //you can change the page size here
            columns: [
                new $.fn.mrjsontablecolumn({
                    heading: "City",
                    data: "city"
                }),
                new $.fn.mrjsontablecolumn({
                    heading: "City Status",
                    data: "cStatus"
                })
            ],
            data: data
        });
    }
    

    Hope it helps somebody else!

提交回复
热议问题