dynamic row

jquery 2010. 9. 9. 14:38


<script language="javascript" type="text/javascript" >
        var maxid = 0;
        var isfirst = "Y";
        function RowAdd(tableid, type, IsSearchYN, urlname) {

            var $id = eval("$('#" + tableid + "')");

            if (isfirst == "Y") {
                maxid = $id.find('tr').length;
                isfirst = "N";
            }

            maxid = parseInt(maxid) + 1
            //trid를 정함
            var trid = tableid + "tr" + maxid;

            //첫번째 행을 복사한다.
            var row = $id.children().find('tr').eq(1).clone(true);

            //증가할 tr의 id값을 정함
            row.attr("id", trid);
            //삭제 버튼의 Delete값의 ID값을 정한다.
            //row.find('td').eq(0).children("input[type='button']").attr("id", tableid + "Delete" + maxid);
            row.find('td').eq(0).find("input[type='button']").attr("id", tableid + "Delete" + maxid);

            var tds = row.find('td');

            //검색 버튼 여부 확인
            if (IsSearchYN == "true") {

                //검색 버튼을 제거하고 새로생성(IE에서는 Clone시 javascript가 호출안돼서 제거후 생성함)
                var text = tds.eq(1).find('a').html();
                tds.eq(1).find('a').remove();
                tds.eq(1).find('span').append("<a onclick='javascript:Search(\"" + trid + "\", \"" + urlname + "\");' class='btn_font'>" + text + "</a>");
            }

            var startindex = row.find("input[type='button']").length;
            var endindex = row.find("td").length;
            //복사한 td들의 값을 Reset해준다.
            for (var i = startindex; i < endindex; i++) {
                //tds.eq(i).children().attr("value", "");
                if (tds.eq(i).children("input[type='hidden']").length > 0) {
                    tds.eq(i).children("input[type='hidden']").attr("value", ""); //hidden reset
                } else if (tds.eq(i).children("input[type='text']").length > 0) {
                    tds.eq(i).children().attr("value", "");                       //text reset
                } else if (tds.eq(i).find("option").length > 0) {
                    tds.eq(i).find("Select").val(tds.eq(i).find("Select option:first").val()); //select box reset
                } else if (tds.eq(i).children("input[type='checkbox']").length > 0) {          //checkbox reset

                } else if (tds.eq(i).children("input[type='radio']").length > 0) {             //radio button reset
                    for (var j = 0; j < tds.eq(i).children("input[type='radio']").length; j++) {
                        tds.eq(i).children("input[type='radio']").eq(j).attr("name", name + maxid);
                    }
                }else {
                    tds.eq(i).text("");         //td값 reset
                }

            }

            var tempID;
            if (type == "UP") {
                //복사한 Row를 위로 생성한다.
                tempID = eval("'#" + tableid + " tr:first'");
                $(tempID).after(row);
            } else if (type == "DOWN") {
                //복사한 Row를 아래로 생성한다.
                tempID = eval("'#" + tableid + " tr:last'");
                $(tempID).after(row);
            }
            return false;
        }

        function RowDelete(row) {

            var $id = eval("'#" + row.id + "'");

            var len = $($id).parent().parent().parent().find('tr').length;

            if (len > 2) {
                $($id).parent().parent().remove();
            } else {
                var tr = $($id).parent().parent();
                var startindex = tr.find("input[type='button']").length;
                var endindex = tr.find("td").length;
                for (var i = startindex; i < endindex; i++) {
                    if (tr.find('td').eq(i).children("input[type='hidden']").length > 0) {
                        tr.find('td').eq(i).children("input[type='hidden']").attr("value", ""); //hidden값 reset
                    } else if (tr.find('td').eq(i).children("input[type='text']").length > 0) {
                        tr.find('td').eq(i).children().attr("value", "");
                    } else if (tr.find('td').eq(i).find("option").length > 0) {
                        tr.find('td').eq(i).find("Select").val(tr.find('td').eq(i).find("Select option:first").val()); //select box reset
                    } else if (tr.find('td').eq(i).children("input[type='checkbox']").length > 0) {          //checkbox reset
                        tr.find('td').eq(i).children("input[type='checkbox']").attr("checked", "");
                    } else if (tr.find('td').eq(i).children("input[type='radio']").length > 0) {             //radio button reset
                        tr.find('td').eq(i).children("input[type='radio']:eq(0)").attr("checked", true);
                    } else {
                        tr.find('td').eq(i).text("");         //td값 reset
                    }

                }
            }

        }
    </script>

'jquery' 카테고리의 다른 글

jQuery 속성(Attrbute)으로 셀렉터 하기...  (1) 2010.10.28
ImageNavi  (3) 2010.09.10
table의 row 선택하여 data가져오기  (1) 2010.09.09
select option의 첫번째 및 선택된 값 가져오기  (2) 2010.09.09
jquery 접근방식  (1) 2010.09.09
Posted by 정광원
,