How to get table data in bootstrap

藏色散人
Release: 2021-11-04 16:19:59
Original
8258 people have browsed it

How bootstrap obtains table data: 1. Obtain it through the table parameter url; 2. Operate through the "$.get()" method, and use the data of the table parameter to customize the method to receive the data format. Can.

How to get table data in bootstrap

The operating environment of this article: Windows 7 system, bootsrap version 3.3.7, DELL G3 computer

How to obtain table data with bootstrap ?

bootstrap table two ways to obtain data

There are two commonly used ways to obtain data, one is to obtain it through the table parameter url json data, the other is to obtain it through $.get(). The effects of the two implementations are the same, but they are slightly different when receiving data. Let's introduce the differences between the two respectively

1. Obtain it through the table parameter url. The url here is the address of the backend interface. The final returned data will be directly rendered into the table. However, there is something to note here, that is, the json format returned by the interface must be consistent with that defined in the table. According to the following example, the data format returned by json is as follows.

{ "id": 1, "name": "张三", "price" : "100" }
Copy after login

The code snippet is as follows:

$ ( '#table' ). bootstrapTable ({ url : 'data1.json' , columns : [{ field : 'id' , title : 'Item ID' }, { field : 'name' , title : 'Item Name' }, { field : 'price' , title : 'Item Price' } ] });
Copy after login

But if the returned json format is as follows, the table cannot be rendered directly, and the formatter in the column parameter must be used to customize the method.

For the json below, you need to implement custom methods for id, name, and price respectively. For a development model where the front and back ends are completely separated, using this method to manipulate data is obviously not optimal.

{ "errcode": "OK", "data_list": [ { "id": 1, "name": "张三", "price" : "100" } ] }
Copy after login

2. By operating in the $.get() method, you can more flexibly operate the data returned by the background. Here we use the data of the table parameter to customize the method to receive the data format.

Code snippet

$.get('/data/', function(data){ $ ( '#table' ). bootstrapTable ({ columns : [{ field : 'id' , title : 'Item ID' }, { field : 'name' , title : 'Item Name' }, { field : 'price' , title : 'Item Price' } ] data: formatData(data) }); }) // 格式化数据 var formatData = function (data) { var l = [] ; for ( var i = 0 ; i < data.data_list.length ; i++) { var m = data.data_list[i] var d = { 'id': m. id , 'name': m. name , 'price': m. price , } l. push(d) } return l } ;
Copy after login

Recommended study: "bootstrap usage tutorial"

The above is the detailed content of How to get table data in bootstrap. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!