var parentcid; var ispid; var tempid; //父节点选中关联子节点选中 function RelativeTreeGridCheck() { var rows = $('#addProductTbl').find('.datagrid-view2 .datagrid-body .datagrid-btable tr'); for (var i = 0; i < rows.length; i++) { if ($(rows).eq(i).attr('node-id') != undefined) { parentcid = ""; ispid = -1; tempid = ""; catalogid = $(rows).eq(i).attr('node-id'); //alert(catalogid); var cols = $(rows).eq(i).find('td'); var fields = ''; for (var j = 0; j < cols.length; j++) { fields = $(cols).eq(j).attr('field'); //alert('fields:' + fields); switch (fields) { case 'CurrentYearPrices': case 'MirrorCurrentYearPrices': case 'MirrorPrevious3YearPrices': case 'MirrorEarlyPrices': case 'type0': if ($(cols).eq(j).find('div input:checked').length > 0) { parentcid = $(cols).eq(j).parent().find("td[field='CatalogId']").find('div').html(); ispid = $(cols).eq(j).parent().find("td[field='isParent']").find('div').html(); contractproducttype = $(cols).eq(j).find('div input').attr('name'); if (ispid == '1') { //获取checkbox对象 var obj = $(cols).eq(j).find('div input:checkbox'); //如果父节点选中,自己点也连带选中 showProductTree(obj, contractproducttype, parentcid, ispid) } } break; } } } } } function showProductTree(obj, catalogtype, id, isparent) { //alert(id.indexof('2')); if (isparent == 1) { //当前节点下包库子节点 //alert('$(obj).attr(checked)' + $(obj).attr('checked')); var state = $(obj).attr('checked') == undefined ? false : true; //alert('state:'+state+' id:'+id); //找出子节点 var nodes = $('input[name="' + catalogtype + '"][parent="' + catalogtype + id + '"]'); nodes.each(function () { //alert('$(this).attr(checked):' + $(this).attr('checked')); var curobjstate = $(this).attr('checked') == undefined ? false : true; disabledOthersCatalogType($(this), state, catalogtype) //alert('curobjstate:' + curobjstate + ' state:' + state + ' id:' + id + ' isparent: ' + $(this).attr('isparent')); if (curobjstate == state && $(this).attr('isparent') == '0') { //alert('leaf'); //如果当前节点的选中状态和父节点不同,并且当前节点不是父节点 $(this).attr('checked', state); $(this).prop('checked', state); } else { //alert('$(this).attr(catalogid)' + $(this).attr('catalogid') + '---$(this).attr(isparent)' + $(this).attr('isparent')); $(this).attr('checked', state); $(this).prop('checked', state); showProductTree($(this), catalogtype, $(this).attr('catalogid'), $(this).attr('isparent')) } if (state) { $(this).removeAttr('disabled'); } }); $(obj).prop('checked', state); disabledOthersCatalogType($(obj), state, catalogtype) } else { var state = $(obj).attr('checked') == undefined ? false : true; //alert(state); //alert(catalogtype); disabledOthersCatalogType($(obj), state, catalogtype) updateParentNodeCheckState($(obj), state, catalogtype) } } //修改其他产品类型的checkbox的只读状态 function disabledOthersCatalogType(obj, state, catalogtype) { $('input[catalogid="' + $(obj).attr('catalogid') + '"]').each(function () { if ($(this).attr('name') != catalogtype) { if (state) { $(this).attr('disabled', 'disabled'); } else { $(this).removeAttr('disabled'); } $(this).attr('checked', false).prop('checked', false); } }); } //查找上一层节点,修改其状态 function updateParentNodeCheckState(obj, state, catalogtype) { var pid = $(obj).attr('parent'); //如果父节点是根节点,则不再执行 if (pid == catalogtype + root || $('#' + pid).length == 0) return; var parent = $('#' + pid); if (!state) { //取消父节点的选中状态 parent.attr('checked', false) parent.prop('checked', false) } else { //alert('pid:'+pid+'---'+$('input[parent="' + pid + '"]:checked').length+'------'+$('input[parent="' + pid + '"]').length); //alert('checkedLen:' + $('input[parent="' + pid + '"]:checked').length + ' len:' + $('input[parent="' + pid + '"]').length); //子节点全部选中 if ($('input[parent="' + pid + '"]:checked').length == $('input[parent="' + pid + '"]').length) { parent.attr('checked', true); parent.prop('checked', true); } } //修改其他产品类型的checkbox的只读状态 disabledOthersCatalogType(parent, state, catalogtype) //继续查找上一层节点 updateParentNodeCheckState(parent, state, catalogtype) }
3,由于是拼接比较繁杂,在此顺便再说一下传参代码
//新增产品单击操作处理 function subAddProduct() { 5 var strJson = ''; var selectedvalued = $('#cbo_selFirstCombbox').combobox('getValue'); //下拉框选中的value selectedvalued = selectedvalued == '' ? '20544' : selectedvalued; // var ids = ''; var rows = $('#addProductTbl').find('.datagrid-view2 .datagrid-body .datagrid-btable tr'); strJson += "["; for (var i = 0; i < rows.length; i++) { catalogid = -1; catalogname = ''; productfather = -1; contractproducttype = ''; quoteprice = -1; isfather = -1; productfathername = ''; if ($(rows).eq(i).attr('node-id') != undefined) { catalogid = $(rows).eq(i).attr('node-id'); //alert(catalogid); var cols = $(rows).eq(i).find('td'); var fields = ''; for (var j = 0; j < cols.length; j++) { fields = $(cols).eq(j).attr('field'); //alert('fields:' + fields); switch (fields) { case 'CatalogName': $(cols).eq(j).find('div span').each(function (index) { if ($(cols).eq(j).find('div span').eq(index).hasClass('tree-title')) { catalogname = $(cols).eq(j).find('div span').eq(index).html(); } }); //alert(catalogname); break; case 'ParentId': productfather = $(cols).eq(j).find('div').html(); break; case 'IsCheck': //原树选中节点id的获取(不包含修改的id节点),此步骤目的是为了配合后台方法,作用是先删除当前版本下所有树选中的节点,再获取页面中修改后的 //节点,进行更新操作 var oldcheck = $(cols).eq(j).find('div').html(); if (oldcheck == 'True') { var cid = $(cols).eq(j).parent().find("td[field='CatalogId']").find('div').html(); ids += cid + ','; //alert(ids) } break; case 'CurrentYearPrices': case 'MirrorCurrentYearPrices': case 'MirrorPrevious3YearPrices': case 'MirrorEarlyPrices': case 'type0': if ($(cols).eq(j).find('div input:checked').length > 0) { isfather = $(cols).eq(j).parent().find("td[field='isParent']").find('div').html(); productfathername = $(cols).eq(j).parent().find("td[field='ParentName']").find('div').html(); contractproducttype = $(cols).eq(j).find('div input').attr('name'); if (contractproducttype == 'IsPresent') { var type = '<%=Controler.ProductType%>'; contractproducttype = type == '1' ? 'CurrentYearPrices' : 'MirrorCurrentYearPrices'; //如果类型为镜像,则默认为镜像当年 //alert($(cols).eq(j).parent().find("td[field='" + contractproducttype + "']").find('div input').val()); //quoteprice = 0; quoteprice = $(cols).eq(j).parent().find("td[field='" + contractproducttype + "']").find('div input').val(); ispresent = 1; } else { quoteprice = $(cols).eq(j).find('div input').val(); ispresent = 0; } //alert('name:' + $(cols).eq(j).find('div input').attr('name') + ' value:' + $(cols).eq(j).find('div input').val()); } break; } } //alert('catalogid:' + catalogid + '--catalogname:' + catalogname + '--productfather:' + productfather + '--contractproducttype:' + contractproducttype + '--quoteprice:' + quoteprice); if (catalogid != -1 && catalogname != '' && productfather != -1 && contractproducttype != '' && quoteprice != -1 && productfathername != '') { strJson += "{\"ProductID\":\"" + catalogid + "\",\"ContractProductType\":\"" + contractproducttype + "\",\"ProductFather\":\"" + productfather + "\",\"Productname\":\"" + catalogname + "\",\"Quotedprice\":\"" + quoteprice + "\",\"Oldproduct\":\"" + oldproduct + "\",\"IsPresent\":\"" + ispresent + "\",\"ContractID\":\"" + $('#ContractId').val() + "\",\"SelectedID\":\"" + selectedvalued + "\",\"IsParent\":\"" + isfather + "\",\"ProductFatherName\":\"" + productfathername + "\",\"IsNull\":\"0\"},"; } } } //alert(strJson); if (strJson == '[') { strJson = "[{\"ContractID\":\"" + $('#ContractId').val() + "\",\"SelectedID\":\"" + selectedvalued + "\",\"IsNull\":\"1\"}]"; } else { strJson = strJson.substr(0, strJson.length - 1); strJson += "]"; } ids = ids.substr(0, ids.length - 1); subProduct(strJson, ids); //alert(rows.length); } //新增产品提交操作 function subProduct(strJson, ids) { $.post('../Handlers/Contract_ProductHandler.ashx', { 'handlertype': 'subAddProduct', 'strJson': strJson, 'ids': ids, 'pindex': $('#pindex').val() }, function (responseData) {121 switch (responseData.Status) { case "success": //成功的操作 $.messager.alert('提示', responseData.Msg); $('#ProductWinTree').window('close'); //$('#selFirstCombbox').val('6774'); $('#dg_Product').datagrid('reload'); break; case "failed": //失败的操作 $.messager.alert('提示', responseData.Msg); break; } }, 'json'); }
总结