javascript - ajax sorting problem
天蓬老师
天蓬老师 2017-06-13 09:21:54
0
9
833

This is the form I loop out by reading the database

Now I want to sort by clicking name, gender, family, how should I write this using ajax?

天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

reply all(9)
三叔

If the number of data items is particularly large, it is strongly recommended that you use the dataTables plug-in. This plug-in is quite complete for displaying data in tables. Issues like sorting are minor issues.

为情所困

You have so many problems with one project, is this an internship? This is a graduation project...
1. To get sorted data with ajax, at least two more fields are needed, one is the field that needs to be sorted, and one is desc or asc
2. Since the sorting is not set at the beginning, the default sorting is required , generally id desc.
3. To avoid system-level query errors caused by problems with the passed sorting parameters, you must verify the parameters, verify whether the field exists in the table, or whether the field is set to be sorted. Use the ternary operator to set whether the sorting is from small to large or large to small

The front end binds a click event to th of each thead, and th must have two attributes, the attribute field and the sorting method

世界只因有你

ajax just requests the backend data, passes the sorting method you want to the backend as parameters, and then the backend queries and returns the data for display.

淡淡烟草味

This has nothing to do with ajax, the sorting results are generated in the background.

You can submit the field name + sorting method to the background through ajax, and the background will output the sorted json, and then update it to the dom.

The front desk can also sort the current table by traversing the current table.

黄舟

dataTables This plug-in can do it. You can go to the official website to see the examples. The operation is also very convenient and the function is very powerful

巴扎黑

One solution is to use mvvm frameworks such as vue and react, and the other is to return to the client after sorting on the server by adjusting different interfaces. Recommend the first option

我想大声告诉你

For local sorting, you just use the algorithm to compare the obtained data. Reference: /a/11... The second half.
Otherwise, just like Datatables, just give instructions and get the data in the background.
However, I guess you want client-side sorting. PS: I have used the Datatables plug-in before. There are a lot of APIs to implement your functions. I feel that it is overkill and may take more time. Datatables has a Chinese website, but the introduction is not very detailed.

扔个三星炸死你

If you use jquery, you can use the jquery plug-in tablesort, see the instructions: http://blog.csdn.net/classicb...

左手右手慢动作

If you feel that there is no paging, just use js's .sort(). Delete the table and regenerate it every time you click it. If you want to be more efficient, it is better to find an html template library, such as doT.js.

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!