HOW TO USE JQUERY TO SORT AND REORGANIZE YOUR CONTENT :

jquery html 정렬 sort

HOW TO USE JQUERY TO SORT AND REORGANIZE YOUR CONTENT

Hello!

Recently we were tasked with finding ways to re-organize search results on a Drupal page in such a way that we could prioritize, group and sort the results in a coherent way.

The simplest way to look at organizing in general, especially content that is dynamically generated, is to either adjust the query that actually produces the results (back-end) OR adjust the results with jQuery (front-end).

There are advantages and reasons why one way may be more advantageous over the other. Adjusting the query that produces the results may produce the sorted results quicker however the downside might be that making further adjustments or improvements requires extra overhead since building and executing database queries (for example) requires additional testing, QA and all that sort of stuff.

Alternatively, if you sort the content / results on your page with jQuery, it is much more flexible and easier to manipulate the results as the needs and requirements of your site or your client may change. The time to test and implement sorting and reorganizing content with jQuery is significantly lower as a result. You are also potentially reducing load off the database by executing less “expensive” queries.

So jQuery was the route to move forward with!

MAKE YOUR HTML MARKUP SORT-ABLE BY JQUERY

Before getting into the actual sorting and reorganizing, we need to take a look at the content to understand how to parse and shuffle it around. The best way to go about doing this is to establish a data field for each content.

In our example, the content itself is displayed on the page in the form of a styled unordered list. Below find an example of the list of content :

 

 

So painfully simple, right? Obviously this is over simplified for a reason. You have to imagine the above list being generated dynamically. In Drupal we build a pretty substantial database query with many conditions across many fields for a specific content type. The results (in our real world scenario) have 3-4 fields and an image. All contained within the li.

When producing the results, we want to be able to sort or group the results based on the taxonomy that was assigned to them. So when producing the HTML markup, we want to have a data field that identifies this. So our markup now becomes this :

 

 

So now you can see that each list item has a data-category field. We’re going to tell jQuery to use this data field to sort and re-organize our results.

CREATE A JQUERY FUNCTION TO PARSE YOUR HTML MARKUP

The first thing we want to do is create a simple function that we can use to parse and re-sort all the data-category fields on the page and sort by all the matching data-category fields in ascending or descending order.

 

 

What does this function do? Well it takes 4 arguments : arg, sel, elem and order. Here’s a breakdown of what each arugment is :

경축! 아무것도 안하여 에스천사게임즈가 새로운 모습으로 재오픈 하였습니다.
어린이용이며, 설치가 필요없는 브라우저 게임입니다.
https://s1004games.com

arg : The data-filter to parse for sorting. In our case it would be “data-category”
sel : This will be the parent selector that contains the data filter. In our case it is the class “search-results”
elem : This further narrows down the child elements within the parent selector, which is just a “li” in this case
order : This specifies the sort order. “asc” or “desc” would either be acceptable

What we want to do is trigger the newly created function and store the results in a variable :

 

 

After the function is executed, everything magically re-sorts! You probably want to encapsulate everything in a document ready function :

 

 

You might be wondering why we stored everything in a variable called “deferred”. This is because we wanted to inject the category name above all the list items that belong in that category.

What we will need to do is identify the first item in the grouped list of items within a particular category. That is because we want to inject html before that first item only (i.e. a category heading title) :

 

 

Whats happening in the above snippet? Once the deferred variable is finished populating, we parse all the list item id’s with regex filtering “result-tax” and assigning the class “firsttest” to the first one only.

Then we parse all the list items with the class “firsttest” , pull the category name from the data-category field and inject html before the item. The result is for all the items there will be a heading title of the category name before each grouped list.

Hopefully this will help someone out there!

 

[출처] https://www.shift8web.ca/2017/01/use-jquery-sort-reorganize-content/

 

 

본 웹사이트는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 웹사이트 서버의 유지 및 관리, 그리고 기술 콘텐츠 향상을 위해 쓰여집니다.
번호 제목 글쓴이 날짜 조회 수
34 [javascript] React - Apache에 배포하기 file 졸리운_곰 2026.01.25 444
33 Python으로 GraphQL 서버 구현 file 졸리운_곰 2019.12.17 541
32 처음 만나는 GraphQL file 졸리운_곰 2019.12.17 373
31 웹팩(Webpack) 이란, 웹팩 간단 정리 및 리액트(React) 기본 개발환경 세팅. [2] file 졸리운_곰 2019.11.08 581
30 웹팩(Webpack) 이란, 웹팩 간단 정리 및 리액트(React) 기본 개발환경 세팅. [1] file 졸리운_곰 2019.11.08 411
29 PHP 로 css/js 보호하기 졸리운_곰 2019.11.08 438
28 Three.js를 이용한 WebGL: 기본 file 졸리운_곰 2019.11.08 495
27 underscore.js로 편해지자 졸리운_곰 2018.10.16 545
26 자바스크립트로 각종 값넘기는방법 졸리운_곰 2018.01.24 518
25 form 데이터 주고 받기 file 졸리운_곰 2018.01.24 489
24 Node.js & WebSocket — Simple chat tutorial file 졸리운_곰 2017.12.08 612
23 JavaScript 모듈화 도구, webpack file 졸리운_곰 2017.10.30 539
22 웹팩이란? 졸리운_곰 2017.10.30 532
21 이해하기 쉬운 Webpack 가이드 file 졸리운_곰 2017.10.30 855
20 [jquery] Ajax를 품은 jQuery file 졸리운_곰 2017.04.25 466
19 Create Your First Mobile App with AngularJS and Ionic file 졸리운_곰 2016.11.20 1269
18 Single Page Application using AngularJs Tutorial file 졸리운_곰 2016.11.20 458
17 AngularJS Tutorial - Building a Web App in 5 minutes file 졸리운_곰 2016.11.20 460
16 자바스크립트의 'this' 키워드 이해하기 졸리운_곰 2016.11.17 634
15 jQuery 핵심 - 노드 다루기 졸리운_곰 2016.11.17 777
대표 김성준 주소 : 경기 용인 분당수지 U타워 등록번호 : 142-07-27414
통신판매업 신고 : 제2012-용인수지-0185호 출판업 신고 : 수지구청 제 123호 개인정보보호최고책임자 : 김성준 sjkim70@stechstar.com
대표전화 : 010-4589-2193 [fax] 02-6280-1294 COPYRIGHT(C) stechstar.com ALL RIGHTS RESERVED