jQuery Easyui Treegrid实现显示checkbox功能
程序员文章站
2022-05-14 19:21:48
下面通过本文给大家介绍下图中的treegrid如何实现?
要求:动态加载;级联勾选;通关类型判断显示包库/还是镜像(列有所不同,镜像共4列),勾选一个复选框,后面的复...
下面通过本文给大家介绍下图中的treegrid如何实现?
要求:动态加载;级联勾选;通关类型判断显示包库/还是镜像(列有所不同,镜像共4列),勾选一个复选框,后面的复选框变为不可勾选状态。
下面是具体代码:
1,初始化treegrid,(其中有几个type列,是由后台人员提供的字段名,虽然我也不想弄一堆type...汗)
var root = 20543; //初始化产品树 function initproducttreegrid(rootid) { var type = '<%=controler.producttype%>'; var ishowpack = true; var ishowmirro = true; //1,包库;2,镜像 if (type == '1') { ishowpack = false; ishowmirro = true; } else { ishowpack = true; ishowmirro = false; }; $('#tt_product').treegrid({ url: '../handlers/contract_producthandler.ashx', queryparams: { handlertype: "initproducttreegrid", contractid: $('#contractid').val(), catalogid: rootid, pindex: $('#pindex').val() }, idfield: 'id', width: 930, treefield: 'catalogname', fitcolumns: true, //宽度自适应窗口 rownumbers: false, //是否加行号 singleselect: true, scrollbarsize: 0, //去除滚动条,否则右边最后一列会自动多处一块 columns: [[ { title: '产品列表', field: 'catalogname', width: 210 }, { title: '产品id', field: 'catalogid', hidden: true }, { title: '父产品id', field: 'parentid', hidden: true }, { title: '父产品名称', field: 'parentname', hidden: true }, { title: '产品类型', field: 'producttype', hidden: true }, { title: '是否为子节点', field: 'isleaf', hidden: true }, //备注:(1,是;0,否) { title: '是否为父节点', field: 'isparent', hidden: true }, { title: 'ischecked', field: 'ischeck', hidden: true }, { title: 'currentyearprices', field: 'type1', hidden: true }, { title: 'mirrorcurrentyearprices', field: 'type3', hidden: true }, { title: 'mirrorearlyprices', field: 'type4', hidden: true }, { title: 'mirrorprevious3yearprices', field: 'type5', hidden: true }, { field: 'currentyearprices', title: '当前价格', width: 200, hidden: ishowpack, formatter: function (value, rec, index) { var d = '<input type="checkbox" name="currentyearprices" catalogid="' + rec.catalogid + '" ' + (rec.type1 == 'true' ? 'checked="checked"' : '') + ' id="currentyearprices' + rec.catalogid + '" onclick="showproducttree(this,\'currentyearprices\',' + rec.catalogid + ',' + rec.isparent + ');" parent="currentyearprices' + rec.parentid + '" isparent="' + rec.isparent + '" value="' + value + '" /> ' + (value != 0 ? value.substr(0, value.length - 2) : '0.00'); return d; } }, { field: 'mirrorcurrentyearprices', title: '当前价格', width: 200, hidden: ishowmirro, formatter: function (value, rec, index) { var d = '<input type="checkbox" name="mirrorcurrentyearprices" catalogid="' + rec.catalogid + '" ' + (rec.type3 == 'true' ? 'checked="checked"' : '') + ' id="mirrorcurrentyearprices' + rec.catalogid + '" onclick="showproducttree(this,\'mirrorcurrentyearprices\',' + rec.catalogid + ',' + rec.isparent + ');" parent="mirrorcurrentyearprices' + rec.parentid + '" isparent="' + rec.isparent + '" value="' + value + '" /> ' + value.substr(0, value.length - 2); //var d = '<span name="currentyearmirrorprice" id="currentyearmirrorprice' + rec.catalogid + '" class="tree-checkbox tree-checkbox0">' + value + '</span>'; return d; } }, { field: 'mirrorprevious3yearprices', title: '前阶段价格', width: 200, hidden: ishowmirro, formatter: function (value, rec, index) { var d = '<input type="checkbox" name="mirrorprevious3yearprices" catalogid="' + rec.catalogid + '" ' + (rec.type5 == 'true' ? 'checked="checked"' : '') + ' id="mirrorprevious3yearprices' + rec.catalogid + '" onclick="showproducttree(this,\'mirrorprevious3yearprices\',' + rec.catalogid + ',' + rec.isparent + ');" parent="mirrorprevious3yearprices' + rec.parentid + '" isparent="' + rec.isparent + '" value="' + value + '" /> ' + value.substr(0, value.length - 2); return d; } }, { field: 'mirrorearlyprices', title: '早期价格', width: 200, hidden: ishowmirro, formatter: function (value, rec, index) { var d = '<input type="checkbox" name="mirrorearlyprices" catalogid="' + rec.catalogid + '" ' + (rec.type4 == 'true' ? 'checked="checked"' : '') + ' id="mirrorearlyprices' + rec.catalogid + '" onclick="showproducttree(this,\'mirrorearlyprices\',' + rec.catalogid + ',' + rec.isparent + ');" parent="mirrorearlyprices' + rec.parentid + '" isparent="' + rec.isparent + '" value="' + value + '" /> ' + value.substr(0, value.length - 2); return d; } }, { field: 'type0', title: '是否赠送', width: 200, formatter: function (value, rec, index) { //alert(rec.ispresent); var d = '<input type="checkbox" name="ispresent" catalogid="' + rec.catalogid + '" ' + (rec.type0 == 'true' ? 'checked="checked"' : '') + ' id="ispresent' + rec.catalogid + '" onclick="showproducttree(this,\'ispresent\',' + rec.catalogid + ',' + rec.isparent + ');" parent="ispresent' + rec.parentid + '" isparent="' + rec.isparent + '" value="0" /> '; return d; } } ]], loadfilter: function (data, parentid) { //逐层加载 function setdata() { var todo = []; for (var i = 0; i < data.length; i++) { todo.push(data[i]); } while (todo.length) { var node = todo.shift(); if (node.children) { node.state = 'closed'; node.children1 = node.children; node.children = undefined; todo = todo.concat(node.children1); } } } setdata(data); var tg = $(this); var opts = tg.treegrid('options'); opts.onbeforeexpand = function (row) { if (row.children1) { tg.treegrid('append', { parent: row[opts.idfield], data: row.children1 }); row.children1 = undefined; tg.treegrid('expand', row[opts.idfield]); } return row.children1 == undefined; }; return data; }, onloadsuccess: function (row, data) { //alert(data[0].catalogid) relativetreegridcheck(); } }); };
2,onloadsuccess中的relativetreegridcheck()级联方法
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'); }
总结
以上所述是小编给大家介绍的jquery easyui treegrid实现显示checkbox功能,希望对大家有所帮助
下一篇: JScript实现表格的简单操作
推荐阅读
-
jQuery实现鼠标移到某个对象时弹出显示层功能
-
jQuery实现checkbox全选功能完整实例
-
jQuery实现input输入框获取焦点与失去焦点时提示的消失与显示功能示例
-
jQuery实现的点击显示隐藏下拉菜单功能完整示例
-
jQuery EasyUI tree增加搜索功能的实现方法
-
jQuery Easyui Treegrid实现显示checkbox功能
-
jQuery实现菜单的显示和隐藏功能示例
-
jQuery实现鼠标滑过商品小图片上显示对应大图片功能【测试可用】
-
jQuery EasyUI的TreeGrid查询功能实现方法
-
jQuery easyui datagird编辑行删除行功能的实现代码