본문 바로가기

classic asp

[classic asp] 리스트 데이터 정렬

<%
Dim SortInfo, arSortField(1), arSortType(1), arSortGroup, arSortInner, curSortField
'정렬초기화
For i = 0 To UBound(arSortField)
	arSortField(i) = ""
	arSortType(i) = "ASC"
Next

'정렬요청시 정렬정보 배열로 저장
curSortField = Request("curSortField") '최종단계의 정렬필드 - 리스트
SortInfo = Request("SortInfo")
If SortInfo <> "" Then
	arSortGroup = Split(SortInfo, "|")
	For i = 0 To UBound(arSortGroup) - 1
		arSortInner = Split(arSortGroup(i), ",")
		arSortField(i) = arSortInner(0)
		arSortType(i) = arSortInner(1)
	Next
End If
%>
<!-- 검색 폼 -->
<form name="serchForm" action="list.asp" method="post" onsubmit="return serchFormSubmit();">
  <input type="hidden" id="SortInfo" name="SortInfo" value="<%=SortInfo%>">
  <input type="hidden" id="curSortField" name="curSortField" value="<%=curSortField%>">
  <table width="600" cellpadding="0" cellspacing="2">
    <tr>
      <td  height="25"  width="100">
        검색어 <input type="text" name="serchWord">
        <input type="submit" id="btnSearch" value="  검    색  ">
      </td>	
    </tr>
  </table> 
</form>
<%

'정렬쿼리
If SortInfo = "" Then
	Sql = Sql & "ORDER BY Num "
Else
	Sql = Sql & "ORDER BY "
	For i = UBound(arSortField) To 0 Step -1
		If arSortField(i) <> "" Then
			Sql = Sql & fnPatSort(i, arSortField(i), arSortType(i))
		End If
	Next		
End If
%>

<!-- 리스트 데이터 -->
<table cellpadding="4">
	<tr>	
		<td>
			<a href="javascript:SortSet(<%=fnSortString("title",arSortField,arSortType)%>);">제목<%=fnSortArrow("title",curSortField)%></a>
		</td>	
		<td>
			<a href="javascript:SortSet(<%=fnSortString("regdate",arSortField,arSortType)%>);">등록일<%=fnSortArrow("regdate",curSortField)%></a>
		</td>	
	</tr>
	<%
  '리스트 데이터 출력
	%>
</table>

<script type="text/javascript">
function serchFormSubmit()
{
	return true;
}
function SortSet(f, t)
{	
	var i, sInfo, arrSort, resultSort;	
	sInfo = document.getElementById("SortInfo").value;
	document.getElementById("curSortField").value = f + "|" + t;
	if (t == "ASC")
	{
		resultSort = "DESC";
	}
	else
	{
		resultSort = "ASC";
	}
	if (sInfo == "")
	{		
		document.getElementById("SortInfo").value = f + "," + resultSort + "|";
	}
	else
	{		
		arrSort = sInfo.split("|");
		for (i=0; i<=arrSort.length; i++)
		{
			if (arrSort[i] == f+","+t) //동일 sort정보 삭제
			{				
				sInfo = sInfo.replace(f+","+t+"|", "");
			}
		}
		document.getElementById("SortInfo").value = sInfo + f + "," + resultSort + "|";
	}	
	if (serchFormSubmit() == true)
	{
		serchForm.submit();
	}
}
</script>
<%
'-------------------------------------------------------------------------
'정렬 인수
Function fnSortString(FieldCode, arSortField, arSortType) 
	Dim ReturnValue, i	
	For i = 0 To UBound(arSortField)
		If arSortField(i) = FieldCode Then
			ReturnValue = "'" & arSortField(i) & "','" & arSortType(i) & "'"
		End If
	Next
	If ReturnValue = "" Then
		ReturnValue = "'" & FieldCode & "','ASC'"
	End If
	fnSortString = ReturnValue
End Function
'-------------------------------------------------------------------------
'정렬방향 화살표
Function fnSortArrow(FieldCode, curSortField) 
	Dim ReturnValue, arReturnValue
	ReturnValue = ""
	If curSortField <> "" Then
		arReturnValue = Split(curSortField, "|")
		If FieldCode = arReturnValue(0) Then
			If arReturnValue(1) = "ASC" Then
				ReturnValue = "↓"
			Else
				ReturnValue = "↑"
			End If
		End If
	End If
	fnSortArrow = ReturnValue
End Function
'-------------------------------------------------------------------------
'정렬쿼리
Function fnPatSort(i, SortField, SortType) 
	Dim ReturnValue
	Select Case SortField
		Case "title"
			ReturnValue = "DataTitle " & SortType
		Case "regdate"
			ReturnValue = "RegDate " & SortType	
	End Select
	If i > 0 Then
		ReturnValue = ReturnValue & ","
	End If
	fnPatSort = ReturnValue
End Function
'-------------------------------------------------------------------------
%>