当前位置: 首页 > 编程日记 > 正文

asp.net mvc jqgrid 同一个页面查询不同的表,jqgrid显示不同表的表头和数据并且分页...

基于我上一篇文章<a href="http://www.cnblogs.com/alasai/p/4765756.html">asp.net mvc excel导入</a>中不同的部门上传不同的excel文件类型,当在同一个页面查询时怎么办呢。

解决方案:根据传过来的表名和时间参数一次性把数据全部加载到table中,其中表头要我们一个个去定,主体的顺序我们也要和表头一样,加载到前台再用表格分页控件来分页,我这里大概有100多个excel文件类型,他们的列名都不要,想想这样的做法不且实际。

有没有其他的解决方案呢,看了很多的jqgrid示例,他们的表头(colNames)和内容(colModel)都是首先定死的。这里我想到一个解决方案就是把colNames和colModel都做成活的,这样不就可以完美解决上面的问题了吗,

想法总是好的,但做起来不是一帆风顺的,但前提是你得有这种想法才行。

想法和思路:

1.把jqgrid的colNames和colModel都做成活的,但是每个表的colNames都不一样,而且他们的顺序必须一致才行,怎么办呢,在<a href="http://www.cnblogs.com/alasai/p/4765756.html">asp.net mvc excel导入</a>这篇文章中我上传文件成功之后会把他们的colNames和colModel都保存在以他们表名命名的txt文件中。代码片段如下

所以展示每个表的colNames和colModel就不成问题

后台c#代码如下:

 [HttpPost]public ActionResult GetTestData(string department, string tablename, string StartTime, string EndTime){Stopwatch watch = CommonHelper.TimerStart();string sql5 = "SELECT * FROM " + department + "_" + tablename + " where 1=1 and enabled='1' ";if (!string.IsNullOrEmpty(StartTime)){sql5 += " and convert(varchar(10),addtime,120)>='" + StartTime + "' ";}if (!string.IsNullOrEmpty(EndTime)){sql5 += " and convert(varchar(10),addtime,120)<='" + EndTime + "' ";}DataTable ListData = DataFactory.Database().FindTableBySql(sql5);this.dirCSV = Server.MapPath("~/Content/uploads/");StreamReader sr = new StreamReader(this.dirCSV + "\\" + department + "_" + tablename + ".txt");String line;List<string> list = new List<string>();while ((line = sr.ReadLine()) != null){list.Add(line.ToString());}string colnames = "";string[] chinesname = list[0].ToString().Trim(',').Split(',');string[] englishname = list[1].ToString().Trim(',').Split(',');for (int i = 0; i < chinesname.Length; i++){colnames += "'" + chinesname[i].ToString() + "',";}List<Department> list1 = new List<Department>();for (int j = 0; j < englishname.Length; j++){list1.Add(new Department { index = englishname[j].ToString().ToLower(), lable = chinesname[j].ToString(), name = englishname[j].ToString().ToLower(), sortable = "false" });}var result = new{Json = new{colNames = chinesname,colModels = (from dept in list1select new{index = dept.index,lable = dept.lable,name = dept.name,sortable = false}),data = new{options = new{page = "1",total = "1",records = "1",costtime = CommonHelper.TimerEnd(watch),rows = ListData}}}};return Content(result.ToJson());}

那么前台改如何解析上面生成的json呢。

jquery代码如下

 $.ajax({url: "@Url.Content("/DataSwitch/GetTestData")?department=" + $("#seldepartment").val() + "&tablename=" + $("#ExcelFileId").val() + "&sjs=" + new Date().getTime() + "&StartTime=" + $("#StartTime").val() + "&EndTime=" + $("#EndTime").val(),type: 'POST',cache: false,data: {},success: function (result) {result = eval('('+result+')');var colModels = result.Json.colModels;var colNames = result.Json.colNames;var data = result.Json.data.options;$("#gridTable").jqGrid({datatype: 'jsonstring',datastr: data,colNames: colNames,colModel: colModels,jsonReader: {root: 'rows',repeatitems: false},gridview: true,pager: $('#gridPager'),height: $(window).height() - 111,autowidth: true,rowNum: 15,rowList: [15, 30, 50, 100],viewrecords: true,rownumbers: true,shrinkToFit: false})},error: function (result) {}}); //end ajax

现在查询不同的表可以显示在jqgrid中显示不同的表内容了,但是这里又出现了一个问题(这个问题你是在百度上很难找得到解决方案的)

问题就是只能显示第一次选择的表内容,而且分页也没有效果,这个问题困扰了我三个小时,最后在jqgrid群里问了一下,有人说是加载之后,加载数据的html没有了。这时我就试试了再加载不同表格之前我重新构造一下html。

  $grid = $("<table id='gridTable'></table><div id='gridPager'></div>");$('#grid_List').empty().html($grid);

这时这段简短而神奇的代码解决了上面遇到的问题。

完整的jquery代码如下

 //加载表格function GetGrid() {var eid = $("#ExcelFileId").val();if (eid == ""){tipDialog("请先选择文件类型", 3,0);return false;}$grid = $("<table id='gridTable'></table><div id='gridPager'></div>");$('#grid_List').empty().html($grid);$.ajax({url: "@Url.Content("/DataSwitch/GetTestData")?department=" + $("#seldepartment").val() + "&tablename=" + $("#ExcelFileId").val() + "&sjs=" + new Date().getTime() + "&StartTime=" + $("#StartTime").val() + "&EndTime=" + $("#EndTime").val(),type: 'POST',cache: false,data: {},success: function (result) {result = eval('('+result+')');var colModels = result.Json.colModels;var colNames = result.Json.colNames;var data = result.Json.data.options;$("#gridTable").jqGrid({datatype: 'jsonstring',datastr: data,colNames: colNames,colModel: colModels,jsonReader: {root: 'rows',repeatitems: false},gridview: true,pager: $('#gridPager'),height: $(window).height() - 111,autowidth: true,rowNum: 15,rowList: [15, 30, 50, 100],viewrecords: true,rownumbers: true,shrinkToFit: false})},error: function (result) {}}); //end ajax}

至此问题就被完美的解决了。

转载于:https://www.cnblogs.com/alasai/p/4765945.html

相关文章:

降维后的高维特征的参数_高维超参数调整简介

降维后的高维特征的参数by Thalles Silva由Thalles Silva 高维超参数调整简介 (An introduction to high-dimensional hyper-parameter tuning) 优化ML模型的最佳做法 (Best practices for optimizing ML models) If you ever struggled with tuning Machine Learning (ML) mo…

细细品味大数据--初识hadoop

初识hadoop 前言 之前在学校的时候一直就想学习大数据方面的技术&#xff0c;包括hadoop和机器学习啊什么的&#xff0c;但是归根结底就是因为自己太懒了&#xff0c;导致没有坚持多长时间&#xff0c;加上一直为offer做准备&#xff0c;所以当时重心放在C上面了&#xff08;虽…

js中Array数组中的常用方法汇总

Array的push与unshift方法性能比较分析从原理就可以知道&#xff0c;unshift的效率是较低的。原因是&#xff0c;它每添加一个元素&#xff0c;都要把现有元素往下移一个位置。unshift比push要慢差不多100倍&#xff01;Array有一个叫做reverse的方法&#xff0c;能够把一个数组…

vue写一个通用的toast弹窗 toast 弹窗 提示

效果图 代码 <!DOCTYPE html> <html lang"en"><head><title>弹窗</title><meta charset"UTF-8"><meta name"viewport" content"widthdevice-width, initial-scale1.0, user-scalableno, minimum-s…

我对Node.js Core的首次贡献中学到了什么

by Yael Hermon通过Yael Hermon 我对Node.js Core的首次贡献中学到了什么 (What I Learned from My First Contribution To Node.js Core) A couple of weeks ago my very first PR for Node.js core was merged! A few days later, I decided to tweet about it and share ho…

SHA204A加密芯片配置

1、参考配置文章&#xff1a;http://blog.csdn.net/a5882230/article/details/522148452、可以选slot configuration 1作为密钥区&#xff0c;该区的配置应为&#xff1a;0x81 81&#xff0c;对应的data 0的数据就密钥数据。3、slot configuration的配置有7个参数&#xff0c;其…

[剑指offer] 二叉搜索树的后序遍历序列

二叉搜索树的后序遍历序列 P157 题目&#xff1a;输入一个数组&#xff0c;判断这个数组是不是一个二叉搜索树的后序遍历的结果。 solution&#xff1a;我们知道后序遍历序列的最后一个item是根节点&#xff0c;如果确实是二叉搜索树的后序遍历结果&#xff0c;那么在[0,N-2]中…

HbuilderX中的git的使用 git HbuilderXgit HbuilderX 使用git

【前戏】&#xff1a;得在HubilderX中找到 "工具"->"插件安装" -> "Git插件" 。 【提交代码】&#xff1a;&#xff08;1&#xff09;选中该项目的文件&#xff0c;右键&#xff0c;Git提交 &#xff08;2&#xff09;填写提交信息、查看…

Swift与Objective-C:与恐龙有关的趋势

by Colin Smith通过科林史密斯 Swift与Objective-C&#xff1a;与恐龙有关的趋势 (Swift vs. Objective-C: The trending up-and-comer vs. the dinosaur) Swift的简短历史 (A short history of Swift) I remember how pivotal it was when Swift was introduced at Apple’s …

我的JavaScript学习笔记

1. 那些老旧的实例可能会在 <script> 标签中使用 type"text/javascript"。现在已经不必这样做了。JavaScript 是所有现代浏览器以及 HTML5 中的默认脚本语言。 2. <head> 或 <body> 中的 JavaScript 您可以在 HTML 文档中放入不限数量的脚本。 …

获取枚举值上的Description特性说明

/// <summary> /// 获取枚举值上的Description特性说明 /// </summary> /// <typeparam name"T">枚举类型</typeparam> /// <param name"obj">枚举值</param> /// <returns></returns> public static str…

微信小程序实时获取用户经纬度

注意&#xff0c;使用这个功能之前手机得先打开位置信息。 现在app.json配置 {"pages": ["pages/map/map","pages/index/index","pages/register/register","pages/logs/logs"],"window": {"backgroundTex…

pwa js_如何在互联网信息亭中实现PWA和Barba.js

pwa jsby Nino Mihovilić由NinoMihovilić 如何在互联网信息亭中实现PWA和Barba.js (How to Implement a PWA and Barba.js into internet kiosks) The project we’ll describe here is an interactive internet kiosk that’s used as an extension for the LikeUs mobile …

Vant 组件库(VUE)的使用 Vant滚动选择器 选择器 传值

在 vue- cli 项目中安装 官方文档链接 npm ( 后面内容需要在控制台终端输入) # 通过 npm 安装 npm i vant -S 自动按需引入组件&#xff1a;babel-plugin-import 是一款 babel 插件&#xff0c;它会在编译过程中将 import 的写法自动转换为按需引入的方式 # 安装插件 npm…

MySQL的安装过程

&#xfeff;&#xfeff;近期对MySQL做了一些研究。曾经主要接触的是SQL SERVER。所以&#xff0c;今天对该安装过程做了一些总结以及使用过程中的一些心得。并分享给大家。记得前面。分享过一篇关于数据库的几种连接方式。而本系列文章。将以对数据库的详细操作为主。 MySQL是…

一个球从100米高度自由落下,每次落地后反弹回原高度的一半; * 再落下,求在第几次之后反弹高度小于0.1米, * 并计算在这一次落地时共经过多少米?...

package com.db2;/*** 一个球从100米高度自由落下&#xff0c;每次落地后反弹回原高度的一半&#xff1b; * 再落下&#xff0c;求在第几次之后反弹高度小于0.1米&#xff0c;* 并计算在这一次落地时共经过多少米&#xff1f;* * author denny**/ public class Demo1 {static …

在遗传算法中出现等式约束_排序算法中的稳定性-等式的处理

在遗传算法中出现等式约束by Onel Harrison通过Onel Harrison 排序算法中的稳定性-等式的处理 (Stability in Sorting Algorithms — A Treatment of Equality) Algorithms are at the heart of computer science. Algorithms used for sorting are some of the most fundamen…

vconsole 调试 查看LOG VUE在手机上调试 手机查看h5的日志

简单介绍下vConsole&#xff1a; vConsole是一个由微信公众平台前端团队研发的web前端开发者面板&#xff0c;可用于展示console日志&#xff0c;方便开发、调试。 使用场景1&#xff0c;在vue-cli 构建的项目中使用&#xff1a; 通过npm安装和使用: npm install vconsole -…

无参数的lambda匿名函数

lambda 语法&#xff1a; lambda [arg1[,arg2,arg3....argN]]:expression 1.单个参数的&#xff1a; g lambda x:x*2 print g(3) 结果是6 2.多个参数的&#xff1a; m lambda x,y,z: (x-y)*z print m(3,1,2) 结果是4 3.无参数 # 使用def定义函数的方法 def true():return Tru…

scrum项目管理_Scrum,用于初创企业(或针对该项目的任何项目)

scrum项目管理Scrum is a lightweight framework designed to help small, close-knit teams of people develop complex products.Scrum是一个轻量级框架&#xff0c;旨在帮助紧密联系的小型团队开发复杂的产品。 Of course, Scrum isn’t just applicable to software proje…

升级Jekyll 3.0

每一次的升级都得又一次折腾一次&#xff0c;jekyll也不例外 从jekyll 2.5.2 升级为jekyll 3.0.1 错误一: jekyll 3.0.1 | Error: Permission denied - bind(2) for 127.0.0.1:4000 端口被占有&#xff0c;打开_config.yml 在最后加上一行 port: 5001 (其它也可)问题解决 错误…

jquery学习(3)--高级选择器

自己手写的学习笔记。常规选择器&#xff1a; /****************学习--高级选择器&#xff08;1&#xff09;****************/---高级选择器&#xff1a;ie7层次选择器&#xff1a;后代选择器 ul li a $(ul li a) 获取追溯到的多个dom对象 ie6子选择器 …

h5 返回上一页并且刷新页面

window.history.go(-1) 和 window.history.back(-1) 都用了&#xff0c;安卓会刷新&#xff0c;IOS系统不刷新&#xff08;IOS会读取浏览器缓存&#xff09; 下面用了一种比较low的方法&#xff0c;但是好歹实现了。。 //home.html localStorage.setItem(homeHref,window.loc…

kotlin中的异常处理_如何使用assertFailsWith在Kotlin中测试异常

kotlin中的异常处理by Daniel Newton丹尼尔牛顿 如何使用assertFailsWith在Kotlin中测试异常 (How to test exceptions in Kotlin with assertFailsWith) I wanted to write this short post to highlight the assertFailsWith function available to Kotlin. This function m…

学习dubbo框架的问题

InputStream &#xff1a; 是所有字节输入流的超类&#xff0c;一般使用它的子类&#xff1a;FileInputStream等&#xff0c;它能输出字节流&#xff1b;InputStreamReader &#xff1a; 是字节流与字符流之间的桥梁&#xff0c;能将字节流输出为字符流&#xff0c;并且能为字节…

Android 控件 之 Menu 菜单

http://www.cnblogs.com/Mrs-cc/archive/2012/07/21/2603042.html 1.OptionsMenu &#xff08;选项菜单&#xff09;用法总结使用方法&#xff1a;方法一&#xff1a;添加菜单项&#xff1a;onCreateOptionsMenu&#xff08;Menu menu&#xff09;中添加menu.add(Menu.NONE,Men…

VUE保存页面的数据,VUE页面显示就执行某个函数,VUE页面隐藏就执行某个函数

用 VUE 默认的 keep-alive 组件实现 保存页面的数据,页面显示就执行某个函数&#xff0c;页面隐藏就执行某个函数实现方式&#xff1a; 1.在路由内设置页面是否需要缓存&#xff1b; 示例代码&#xff1a;&#xff08;在需要的组件里面添加meta 对象&#xff0c;keepAlive属…

npm i和npm_让您的NPM套件包含Jest和Codecov☂️

npm i和npmby Carl-Johan Kihl卡尔约翰基尔(Carl-Johan Kihl) 让您的NPM套件包含Jest和Codecov☂️ (Get your NPM-package covered with Jest and Codecov ☂️) 介绍 (Introduction) Let’s talk about code coverage, and how you can do coverage reports in Jest and Cod…

分页传页数的方法

<!DOCTYPE html><html> <head> <meta charset"UTF-8"> <title></title> </head> <body> <div> <span id"num">1</span> <button id"prev">上一页</button> <…

VUE input唤起键盘 底部固定的标签被顶上去解决办法

通过输入框的失去焦点事件和点击事件&#xff0c;当出现键盘时把绝对定位的底部文字隐藏&#xff0c;失去焦点&#xff08;键盘隐藏的时候&#xff09;显示底部文字 解决代码 <input type"text" class"weui-input" click"input_click" blur…