jquery 기반 동적 생 성 표 플러그 인
16069 단어 동적 생 성 표
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm4.aspx.cs" Inherits="table.WebForm4" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
<script src="jquery-1.3.2-vsdoc2.js" type="text/javascript"></script>
<script src="jquery-1.3.2.js" type="text/javascript"></script>
<script src="jquery.DynamicTable.js" type="text/javascript"></script>
<link href="style2.css" type="text/css" rel="Stylesheet"/>
<script type="text/javascript">
var rowtmplate = "<tr><td class='TableData'><input type='text' style='border:0px; width:98%;'/></td>";
rowtmplate += "<td class='TableData'><input type='text' style='border:0px; width:98%;'/></td>";
rowtmplate += "<td class='TableData'><input type='text' style='border:0px; width:98%;'/></td>";
rowtmplate += "<td class='TableData'><input type='text' style='border:0px; width:98%;'/></td>";
rowtmplate += "<td class='TableData'><input type='text' style='border:0px; width:98%;'/></td>";
rowtmplate += "<td class='TableData'><a href='#' > </a></td></tr>";
$(document).ready(function() {
$(this).bind('keyup', function(e) {
switch (e.keyCode) {
case 38: // ↑
var arr = $.fn.getFocus();
var rowIndex = arr[0] - 1;
$.fn.setFocus({ rowIndex: rowIndex, colIndex: arr[1] });
$.fn.setCellsFocus();
break;
case 40: // ↓
var arr = $.fn.getFocus();
var rowIndex = arr[0] + 1;
$.fn.setFocus({ rowIndex: rowIndex, colIndex: arr[1] });
$.fn.setCellsFocus();
break;
default:
break;
}
});
$('#mytbl').DynamicTable({
rowCount: 3, //
identity: 1, // 1
arrFocus: [2, 1], //
rowTmplate: rowtmplate //
});
$('#mytbl').BindEvent({
eventName: "click",
colIndex: 1,
fn: alertMsg
}); // click
$('#mytbl').setCellsFocus(); //
$('#mytbl').deleteRow(); // 6
$('#mytbl').AutoFillData({ colIndex: 2, fn: getData }); //
$('#mytbl').Identity({ colIndex: 1 }); //
$('#mytbl').validationText({ reg: /^((\d+\.\d{2})|\d+)$/, colIndex: 5, defalutValue: 0.00 }); // ( money )
});
//
function addRow(count) {
$('#mytbl').addRow({ rowCount: count });
$('#mytbl').Identity();
$.fn.deleteRow();
}
//
function getData(key) {
var arr = [];
arrFoucs = $.fn.getFocus();
$.ajax({
type: "post",
async: false, //
url: "getData.ashx",
dataType: "json",
cache: false,
success: function(data) {
var idx = arrFoucs[0] - 2;
arr.push(data[idx].id);
arr.push(data[idx].Name);
arr.push(data[idx].Code);
arr.push(data[idx].Units);
arr.push(data[idx].Price);
},
Error: function(err) {
alert(err);
}
});
$.fn.setCellsFocus({ rowIndex: arrFoucs[0], colIndex: 4 });
return arr;
}
function alertMsg() {
arrFoucs = $.fn.getFocus();
alert(' X:'+arrFoucs[0]+' Y:'+arrFoucs[1]+' ');
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<table cellpadding="0" cellspacing="0" class="tablestyle1" id="mytbl">
<tr>
<td class="TableData"> </td>
<td class="TableData"> </td>
<td class="TableData"> </td>
<td class="TableData"> </td>
<td class="TableData"> </td>
<td class="TableData"><a href="#" onclick="addRow(5);"> 5 </a></td>
</tr>
</table>
<input type="button" value=" " onclick="javascript:alert($.fn.getValue({}));" />
</div>
</form>
</body>
</html>
jquery.DynamicTable.js
///<reference path="jquery-1.3.2-vsdoc2.js" />
2
3 (function($) {
4 var rowtmplate = "";
5 var arrFocus = [];
6
7 $.fn.DynamicTable = function(options) { // , userCp
8 var deafult = {
9 //
rowCount: 5, //
identity: 1, // 1
arrFocus: [2, 1], //
rowTmplate: "" //
};
var ops = $.extend(deafult, options);
rowtmplate = ops.rowTmplate;
arrFocus = ops.arrFocus;
$(this).addRow(ops.rowCount);
};
/* */
/*count-- */
$.fn.addRow = function(options) {
var deafult = {
rowCount: 5
};
var ops = $.extend(deafult, options);
var rowData = "";
var count = ops.rowCount;
for (var i = 1; i <= count; i++) {
rowData += rowtmplate;
}
$(this).find('tr:last-child').after(rowData);
CellsFocus();
};
/* , fn */
/*eventName-- ;colIndex-- ( 1 );fn-- */
$.fn.BindEvent = function(options) {
var deafult = {
eventName: 'click',
colIndex: 1,
fn: function() { alert(' !') }
};
var ops = $.extend(deafult, options);
eventName = ops.eventName;
colIndex = ops.colIndex;
fn = ops.fn;
$("tr:gt(0) td:nth-child(" + colIndex + ")").bind(eventName, fn);
};
/* */
/*colIndex-- ( 1 )*/
$.fn.deleteRow = function(options) {
var deafult = {
colIndex: 6
};
var ops = $.extend(deafult, options);
var colIndex = ops.colIndex;
$("tr:gt(0) td:nth-child(" + colIndex + ")").bind("click", function() {
var obj = $(this).parent(); // tr
if (confirm(' ?'))
obj.remove();
});
};
/* */
/*colIndex-- ( 1 )*/
$.fn.Identity = function(options) {
var deafult = {
colIndex: 1
};
var ops = $.extend(deafult, options);
var colIndex = ops.colIndex;
var i = 1;
$("td:nth-child(" + colIndex + ")").find('input').each(function() {
$(this).attr('value', i)
i++;
});
};
/* */
$.fn.getFocus = function() {
return arrFocus;
};
/* */
/*rowIndex-- ( 1 );colIndex-- ( 1 )*/
$.fn.setFocus = function(options) {
var deafult = {
rowIndex: 2,
colIndex: 1
};
var ops = $.extend(deafult, options);
var rowIndex = ops.rowIndex;
var colIndex = ops.colIndex;
arrFocus[0] = rowIndex;
arrFocus[1] = colIndex;
};
/* , Enter */
/*colIndex-- Enter ;fn-- */
$.fn.AutoFillData = function(options) {
colIndex = options.colIndex;
fn = options.fn;
$("td:nth-child(" + colIndex + ")").bind("keyup", function() {
var obj = $(this).parent(); // tr
$(this).find('input').each(function() {
if (event.keyCode == 13) {
var vl = $(this).val();
var arr = new Array();
arr = fn(vl);
var i = 0;
obj.find("td").each(function() {
$(this).find("input").each(function() {
$(this).attr('value', arr[i]);
i++;
});
});
}
});
});
};
/* */
/*rowIndex-- ( 1 );colIndex-- ( 1 )*/
$.fn.setCellsFocus = function(options) {
var deafult = {
rowIndex: arrFocus[0],
colIndex: arrFocus[1]
};
var ops = $.extend(deafult, options);
var rowIndex = ops.rowIndex;
var colIndex = ops.colIndex;
$("tr:nth-child(" + rowIndex + ") td:nth-child(" + colIndex + ")").each(function() {
$(this).find('input').each(function() {
$(this)[0].focus();
$(this).attr('value', $(this).attr('value'));
arrFocus = [];
arrFocus.push(rowIndex);
arrFocus.push(colIndex); //
});
});
};
/* */
/*rowIndex-- ( 1 );colIndex-- ( 1 )*/
$.fn.setCellsSelect = function(options) {
var deafult = {
rowIndex: arrFocus[0],
colIndex: arrFocus[1]
};
var ops = $.extend(deafult, options);
var rowIndex = ops.rowIndex;
var colIndex = ops.colIndex;
$("tr:nth-child(" + rowIndex + ") td:nth-child(" + colIndex + ")").each(function() {
$(this).find('input').each(function() {
$(this)[0].select();
});
});
};
/* */
/*reg-- ;colIndex-- ( 1 );defaultValue-- */
$.fn.validationText = function(options) {
var deafult = {
reg: /^((\d+\.\d{2})|\d+)$/,
colIndex: 2,
defaultValue: 0
};
var ops = $.extend(deafult, options);
var reg = ops.reg;
var colIndex = ops.colIndex;
var defaultValue = ops.defaultValue;
$("tr:gt(0) td:nth-child(" + colIndex + ")").each(function() {
$(this).find('input').each(function() {
//
$(this).bind('blur', function() {
var vl = $(this).attr('value');
if (!reg.test(vl))
$(this).attr('value', defaultValue);
});
});
});
};
/* */
$.fn.getValue = function(options) {
var deafult = {
rowIndex: 0, // ( 2 )
colIndex: 0 // ( 1 )
};
var ops = $.extend(deafult, options);
rowIndex = ops.rowIndex;
colIndex = ops.colIndex;
var val = "";
if (rowIndex == 0) { //
$('tr:gt(0)').each(function() {
$(this).find("td").each(function() {
$(this).find("input").each(function() {
val += $(this).attr('value') + "&";
});
});
val = val.substring(0, val.length - 1) + "|";
});
}
else {
if (colIndex == 0) { //
$('tr:nth-child(' + rowIndex + ')').each(function() {
$(this).find("td").each(function() {
$(this).find("input").each(function() {
val += $(this).attr('value') + "&";
});
});
val = val.substring(0, val.length - 1) + "|";
});
}
else { //
$("tr:nth-child(" + rowIndex + ") td:nth-child(" + colIndex + ")").each(function() {
$(this).find('input').each(function() {
val += $(this).attr('value');
});
});
}
}
return val;
};
/* */
function CellsFocus() {
var colCount = $("tr:nth-child(1) td").size(); //
$("tr:gt(0) td").each(function() {
var obj = $(this);
$(this).find('input').each(function() {
$(this).bind('focus', function() {
var cellTotal = $('td').index(obj); //
arrFocus[0] = parseInt(cellTotal / colCount) + 1; //
arrFocus[1] = cellTotal % colCount + 1; //
});
});
});
};
})(jQuery);
getData.ashx
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
namespace table
{
/// <summary>
/// $codebehindclassname$
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class getData : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.Clear();
string value = GetResult();
context.Response.Write(value);
context.Response.End();
}
private string GetResult()
{
string result = string.Empty;
result = @"
[{""id"":""1"",""Name"":"" "",""Code"":""1371"",""Units"":"" "",""Price"":""200""},
{""id"":""2"",""Name"":"" "",""Code"":""1372"",""Units"":"" "",""Price"":""300""},
{""id"":""3"",""Name"":"" "",""Code"":""1373"",""Units"":"" "",""Price"":""20000""},
{""id"":""4"",""Name"":"" "",""Code"":""1374"",""Units"":"" "",""Price"":""400""},
{""id"":""5"",""Name"":"" "",""Code"":""1375"",""Units"":"" "",""Price"":""500""},
{""id"":""6"",""Name"":"" "",""Code"":""1376"",""Units"":"" "",""Price"":""400""},
{""id"":""7"",""Name"":"" "",""Code"":""1377"",""Units"":"" "",""Price"":""100""}]";
return result;
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
style2.css
/* ---------- ---------- */
body
{
background-color:#ffffff;
MARGIN:0px;
font-size: 10pt; /* */
font-family:Verdana; /* */
}
/* ---------- - ---------- */
A:link {
color: #0000FF;
TEXT-DECORATION: none;}
/* ---------- - ---------- */
A:visited {
COLOR: #0000FF;
TEXT-DECORATION: none}
/* ---------- - ---------- */
A:active {
COLOR: #3333ff;
TEXT-DECORATION: none}
/* ---------- - ---------- */
A:hover {
COLOR: #ff0000;
text-decoration: underline;}
/* ---------- 1( ) ---------- */
.tablestyle1{
font-size: 9pt; /* */
width: 100%; /* */
border: 0px none; /* */
background-color: #0077B2; /* */
cellSpacing:expression(this.cellSpacing=1); /* */
cellPadding:expression(this.cellPadding=3); }
.TableData {
BACKGROUND: #FFFFFF;
FONT-SIZE: 10pt;
}
파일 을 어떻게 업로드 해 야 할 지 몰라 코드 를 붙 일 수 밖 에 없 었 습 니 다.양해 바 랍 니 다!!