一个页面有多个datagrid如何封装复用?

news/2024/7/10 0:47:08 标签: 多个datagrid复用, DataGrid, radio, vue

文章目录

    • 一个页面有多个DataGrid如何复用?
      • 介绍指南
      • 项目需求:
      • 效果图1:没有数据
      • 效果图2: 有数据
      • 1. 封装DataGrid
      • 2. 结合radio禁用启用 DataGrid 删除&添加按钮
      • 3. vue 遍历 "无"数据 状态 控制 DataGrid + - 按钮
      • 4. html

DataGrid_1">一个页面有多个DataGrid如何复用?

介绍指南

  • 封装DataGrid
  • 使用radio禁用启用
  • 页面其他地方使用vue v-model数据绑定( 获取单选按钮时,在updated钩子遍历)生命周期执行关系
  • html代码

项目需求:

  • 1.没有数据,默认.单选框状态选 “无” 并且 禁用DataGrid中(添加、删除)按钮
  • 2.有数据时,单选框状态选中 " 有" 且 DataGrid中(添加、删除)为启用
  • 3.点击 “无” 状态时,禁用 + - 按钮,否则启用

效果图1:没有数据

这里写图片描述

效果图2: 有数据

这里写图片描述

DataGrid_20">1. 封装DataGrid

/**********************DataGrid封装   start************************/
var editIndex = undefined;

// 编辑方法
function endEditing($el) {

	if (editIndex == undefined) {
		return true
	}

	if ($el.datagrid('validateRow', editIndex)) {
		$el.datagrid('endEdit', editIndex);
		editIndex = undefined;
		return true;
	} else {
		return false;
	}
}



// 点击选中行
function onClickRow(index) {
	if (editIndex != index) {
		if (endEditing($(this))) {
			$(this).datagrid('selectRow', index)
				.datagrid('beginEdit', index);
			editIndex = index;
		} else {
			$(this).datagrid('selectRow', editIndex);
		}
	}
}


// 添加方法
function append(el) {
	// 获取当前操作DateGrid的id
	var $idEl = $(el).parents(".datagrid-wrap").find(".easyui-datagrid")

	if (endEditing($idEl)) {
		$idEl.datagrid('appendRow', {
		// 这里是我页面字段名称(可忽略)
			disease_name: "感冒", // 给定默认值
			disease_remark: "",
			diagnosis_date: "",
			create_time: ""

		});
		editIndex = $idEl.datagrid('getRows').length - 1;
		$idEl.datagrid('selectRow', editIndex)
			.datagrid('beginEdit', editIndex);
	}
}



// 删除方法
function removeit(el) {
	var $idEl = $(el).parents(".datagrid-wrap").find(".easyui-datagrid")
	if (editIndex == undefined) {
		return
	}
	$idEl.datagrid('cancelEdit', editIndex)
		.datagrid('deleteRow', editIndex);
	editIndex = undefined;
}

radio_DataGrid__92">2. 结合radio禁用启用 DataGrid 删除&添加按钮

	/**************禁用 启用(在vue 钩子中遍历 获取 无的 单选框)**************/
	$(function () {
		//    获取所有  无 的单选框
		var $radios = $('.radioSpan');
		var $btns = $('.addAndRemoveBtn');


		//页面加载 遍历所有 "无的" 单选框
		$radios.each(function () {
			if ($(this).prop("checked")) {
				var index = $(this).parents('.table-box').index();
				// 当点击  “无”  DataGrid 下的 + - 按钮 禁用
				$btns.eq(index).children('.adds,.dels').linkbutton('disable');

			}
		})

		// 给 无 注册单击事件
		$radios.on('click', function () {
			// 获取li的 index下标
			var index = $(this).parents('.table-box').index();
			// 当点击  “无”  DataGrid 下的 + - 按钮 禁用
	$btns.eq(index).children('.adds,.dels').linkbutton('disable');

		});
		// 遍历所有  “有的”  单选框
		var $has = $('.has');
		$has.on('click', function () {
			// 获取 当前 li标签
			var index = $(this).parents(".table-box").index();
			// 当点击  “有”  DataGrid 下的 + - 按钮 启用
			$btns.eq(index).children('.adds,.dels').linkbutton('enable');
		})
	})

vue_____DataGrid____130">3. vue 遍历 "无"数据 状态 控制 DataGrid + - 按钮

var app = new Vue({
el: '#app',
data: data,

/*************vue  updated生命周期钩子  中  获取所有  无 的单选框******************/
updated: function () {
	//    获取所有  无 的单选框
	var $radios = $('.radioSpan');
	var $btns = $('.addAndRemoveBtn');

	// 遍历所有 "无的" 单选框
	$radios.each(function () {
		// 判断 选中的 按钮
		if ($(this).prop("checked")) {
			// 获取li的 index下标
			var index = $(this).parents('.table-box').index();
			console.log(index);
			// 当点击  “无”  DataGrid 下的 + - 按钮 禁用
			$btns.eq(index).children('.adds,.dels').linkbutton('disable');
		} else {
			var index = $(this).parents('.table-box').index();
			console.log(index);
			// 当点击  “有”  DataGrid 下的 + - 按钮 启用
			$btns.eq(index).children('.adds,.dels').linkbutton('enable');
		}
	})
}

4. html

<!-- 疾病 -->
<div class="one table-box">
	<ul class="radioChoose">
		<li>
			<span class="input-radio">
				<input type="radio" class="radioSpan" value="1" name="groupdisease" v-model="is_disease">
				<span></span>
			</span>
			<span class="input-radio">
						<input type="radio" class="has" value="2" name="groupdisease" v-model="is_disease">
						<span>有疾病</span>
			</span>
		</li>
	</ul>
	<table id="oneDG" class="easyui-datagrid" style="width:98%;height:auto" data-options="
						iconCls: 'icon-edit',
						singleSelect: true,
						toolbar: '#oneRow',
			  
						onClickRow: onClickRow
					">
		<thead>
			<tr>
				<th data-options="field:'disease_name',width:350,align:'center',editor:'textbox'">疾病名称</th>
				<th data-options="field:'disease_remark',width:250,align:'center',editor:'textbox'">备注</th>
				<th data-options="field:'diagnosis_date',width:142,align:'center',editor:{
						type:'datebox',
						options:{
							required:true,
							editable:false
						}

					}">确诊日期</th>
			</tr>
		</thead>
	</table>

	// 添加,删除调用方法  把 当前 this 进行传参
	<div id="oneRow" style="height:auto" class="addAndRemoveBtn">
		<a href="javascript:void(0)" class="easyui-linkbutton adds" data-options="iconCls:'icon-add',plain:true" onclick="append(this)">添加</a>
		<a href="javascript:void(0)" class="easyui-linkbutton dels" data-options="iconCls:'icon-remove',plain:true" onclick="removeit(this)">删除</a>
	</div>
</div>

http://www.niftyadmin.cn/n/1581811.html

相关文章

moment计算年龄《当前时间减去出生时间》

文章目录Easyui 中使用 datebox&#xff0c;计算年龄测试Easyui 中使用 datebox&#xff0c;计算年龄 提示&#xff1a; moment是一个 JavaScript 日期处理类库 //需求&#xff1a; 出生日期 【如果 年龄超过 12岁(含) 就隐藏 儿童部分】$(function(){// 获取到当前datebox日期…

开始时间不能大于结束时间

文章目录两个时间比较开始时间不能大于结束时间使用两个时间比较 开始时间不能大于结束时间 /*** [checkTime 两个时间比较]* param startTime [开始时间]* param endTime [结束时间]* return */ function checkTime(startTime,endTime){if(startTime || endTime ){ret…

第一个数字不大于第二个数字

第一个数字不能大于第二个数字 /*** [checkNumber 检测两个数字大小]* param numbe1 * param numb2 * return*/ function checkNum(num1,num2){if(num1 || num2 ){return true}if(parseInt(num1)>parseInt(num2)){return false;}return true; }// 使用 var age check…

datebox《选择时间不能大于当前时间后》

选择时间不大于当前时间 /*** [dateCheck 选择时间不能大于当前时间]* param [class名或id名 class.class id#id]* return*/ function dateCheck(type){$(type).datebox(calendar).calendar({validator : function(date){var now new Date();var d1 new Date(now.getFullYea…

Enter 发送,Ctrl+Enter 换行

实现快捷&#xff1a; Enter 发送&#xff0c;CtrlEnter 换行 模拟 qq 聊天窗口效果图 js部分 var keyEnter document.querySelector(.rongcloud-text);// 监听事件 键盘按下触发keyEnter.addEventListener(keydown, function (e) {e e || window.event;// console.log(…

IndexedDB基本使用

IndexedDB基本使用 Student 案例 //打开数据库 function openDB (name,version) {var versionversion || 1;var requestwindow.indexedDB.open(name,version);request.onerrorfunction(e){console.log(e.currentTarget.error.message);};request.onsuccessfunction(e){myDB.db…

IndexedDB封装

文章目录IndexedDB封装封装&#xff08;增&#xff0c;删除&#xff0c;查&#xff09;&#xff1a;使用IndexedDB封装 封装&#xff08;增&#xff0c;删除&#xff0c;查&#xff09;&#xff1a; var inquiryDB function (cb) {var obj new Object();obj.name "imC…

使用Express搭建Node.js服务器

Express: 基于 Node.js 平台&#xff0c;快速、开放、极简的 web 开发框架。 文章目录Express: 基于 Node.js 平台&#xff0c;快速、开放、极简的 web 开发框架。说明安装&#xff1a;搭建 Express 详细步骤&#xff1a;测试 Express调用 服务器 演示两种准备工作&#xff1a;…