- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
简短描述:
01) 我将数据从 JSON url 动态加载到 HTML 表中。该脚本位于 HTML
文件 header 中调用的外部 .js
文件中。
02) 我使用页面顶部的第三列(主值)过滤器来过滤结果。当我有静态数据时,该脚本运行良好。自从我开始从 JSON url 动态提取表数据后,它就停止工作了。
JS 脚本在这里:
//It loads the data from the JSON file
$.getJSON(
'http://apolosiskos.co.uk/TEB/MOCK_DATA.json',
function(data){
var tr;
//It loads data from the JSON file to the table
$.each (data, function (key, val) {
tr = $('<tr/>');
tr.append('<td class="name" rel="' + val.first_name + '">' + val.first_name + '</td>');
tr.append('<td >' + 'TEST' + '</td>');
tr.append('<td class="metric2" >' + val.id + '</td>');
tr.append('<td class="metric3"><span class="multTotal">' +'0.00'+ '</span></td>');
tr.append('<td class="metric3-100"><span class="metric3-100">' +'0.00'+ '</span></td>');
tr.append('<td class="metric1-100"><span class="metric1-100">' +'0.00'+ '</span></td>');
$('table').append(tr);
});
$("input").keyup(minmax);
//I even tried the below but did not work
$('body').on('input', '#counter-low, #counter-high', minmax);
});
//The filter function for the MIN MAX values in the MAIN VALUE column
function minmax() {
var table = $('table').DataTable();
$.fn.dataTable.ext.search.push(function (settings, data, dataIndex) {
return parseFloat(data[2]) >= parseFloat($('#counter-low').val() || data[2])
&& parseFloat(data[2]) <= parseFloat($('#counter-high').val() || data[2])
});
$('#counter-low, #counter-high').on('keyup', table.draw);
}
UPD:我将在下面的答案之后有效的代码粘贴到此处:
$(function(){
//It loads the data from the JSON file
$.getJSON(
'http://apolosiskos.co.uk/TEB/MOCK_DATA.json',
function(data){
var tr;
//It loads data from the JSON file to the table
$.each (data, function (key, val) {
tr = $('<tr/>');
tr.append('<td class="name" rel="' + val.first_name + '">' + val.first_name + '</td>');
tr.append('<td ><input class="metric1"/>' + '</td>');
tr.append('<td class="metric2" >' + val.id + '</td>');
tr.append('<td class="metric3"><span class="multTotal">' +'0.00'+ '</span></td>');
tr.append('<td class="metric3-100"><span class="metric3-100">' +'0.00'+ '</span></td>');
tr.append('<td class="metric1-100"><span class="metric1-100">' +'0.00'+ '</span></td>');
$('table').append(tr);
});
//It loads dimension1 from the JSON file to the filter
$.each (data, function (key, val) {
li = $('<li/>');
li.append('<input rel="name" type="checkbox" value="' + val.first_name + '"><label for="cb1">' + val.first_name + '</label></li>');
$('ul').append(li);
});
$('.counter').keyup(minmax);
$('body').on('input click', 'input:checkbox', filters);
});
});
//Multiplication of the cells function
function multInputs() {
var mult = 0;
$("tr").each(function () {
var $val1 = $('.metric1', this).val();
var $val2 = $('.metric2', this).text();
var $total = ($val1 * 1) * $val2 - $val1;
$('.multTotal', this).text($total.toPrecision(3));
var $val3 = $('.multTotal', this).text();
var $total2 = $val3 / 100
$('.metric3-100',this).text($total2.toPrecision(3));
var $total3 = $val1 / 100
$('.metric1-100',this).text($total3.toPrecision(2));
mult += $total;
});
}
//Filter function for the Dimension1 values
function filters() {
var showAll = true;
$('tr').not('.first').hide();
$('input[type=checkbox]').each(function () {
if ($(this)[0].checked) {
showAll = false;
var dimension1= $(this).attr('rel');
var value = $(this).val();
$('td.' + dimension1+ '[rel="' + value + '"]').parent('tr').show();
}
});
if(showAll){
$('tr').show();
}
}
//The filter function for the MIN MAX values in the MAIN VALUE column
function minmax() {
var table = $('table').DataTable();
$.fn.dataTable.ext.search.push(function (settings, data, dataIndex) {
return parseFloat(data[2]) >= parseFloat($('#counter-low').val() || data[2])
&& parseFloat(data[2]) <= parseFloat($('#counter-high').val() || data[2])
});
$('#counter-low, #counter-high').on('keyup', table.draw);
}
最佳答案
所以,在这里回答,我已经下载了你的html、json和js。然后我做了 3 处更改。
$(function(){});
里面让 JS 在整个 DOM 加载完成后执行所有操作。在最小和最大输入中添加一个类,例如:
<input id="counter-min" class="counter"/>
<input id="counter-max" class="counter"/>
并在js中设置为$('.counter').keyup( minmax )
;将这个 minmax 作为您已经创建的函数;
我将表格 html 像这样放置:
<table>
<thead>
<tr id="ProductID" class="first">
<th>NAME</th>
<th>INPUT</th>
<th>MAIN VALUE</th>
<th>DIFF</th>
<th>DIFF /100</th>
<th>MV /100</th>
</tr>
</thead>
<tbody> </tbody>
</table>
所以,这对我有用,我不知道是否有效
第二次编辑我在这里这样做,我只是注释了你的 getJson 来直接将数据添加到你的表上,然后,我做了我之前说的:
1 - 使用 $(function() { .. });
2 - 在反高和反低上添加类,只是将事件附加到它们,然后将事件附加到 $('.className').keyup( minmax );
和
3 - 在表格中添加 thead 和 tbody 元素
//It loads the data from the JSON file
$(function() {
var data = [{"id":2.2,"first_name":"Debra","last_name":"Rodriguez","email":"drodriguez0@admin.ch","gender":"Female","ip_address":"90.22.159.108"},
{"id":2,"first_name":"Julie","last_name":"Lynch","email":"jlynch1@tumblr.com","gender":"Female","ip_address":"54.182.62.180"},
{"id":3,"first_name":"Norma","last_name":"Washington","email":"nwashington2@theatlantic.com","gender":"Female","ip_address":"70.163.106.64"},
{"id":4,"first_name":"Bobby","last_name":"Castillo","email":"bcastillo3@nbcnews.com","gender":"Male","ip_address":"91.202.59.171"},
{"id":5,"first_name":"Henry","last_name":"Chavez","email":"hchavez4@chronoengine.com","gender":"Male","ip_address":"32.237.37.185"},
{"id":6,"first_name":"Debra","last_name":"Howard","email":"showard5@stumbleupon.com","gender":"Female","ip_address":"17.217.42.49"},
{"id":7,"first_name":"Jason","last_name":"Powell","email":"jpowell6@telegraph.co.uk","gender":"Male","ip_address":"14.81.195.117"},
{"id":8,"first_name":"Sean","last_name":"Burns","email":"sburns7@hp.com","gender":"Male","ip_address":"213.164.85.212"},
{"id":9,"first_name":"Jacqueline","last_name":"Gordon","email":"jgordon8@bloglines.com","gender":"Female","ip_address":"18.251.62.55"},
{"id":10,"first_name":"Russell","last_name":"Richards","email":"rrichards9@com.com","gender":"Male","ip_address":"27.226.59.80"},
{"id":11,"first_name":"Albert","last_name":"Perkins","email":"aperkinsa@hc360.com","gender":"Male","ip_address":"243.122.251.248"},
{"id":12,"first_name":"Michael","last_name":"Willis","email":"mwillisb@deviantart.com","gender":"Male","ip_address":"252.197.211.230"},
{"id":13,"first_name":"Joan","last_name":"Hamilton","email":"jhamiltonc@weebly.com","gender":"Female","ip_address":"204.70.110.117"},
{"id":14,"first_name":"Eric","last_name":"Garcia","email":"egarciad@yahoo.co.jp","gender":"Male","ip_address":"178.221.216.3"},
{"id":15,"first_name":"Frank","last_name":"Olson","email":"folsone@amazon.co.uk","gender":"Male","ip_address":"245.25.170.33"},
{"id":16,"first_name":"Kelly","last_name":"Fuller","email":"kfullerf@tripod.com","gender":"Female","ip_address":"199.209.173.51"},
{"id":17,"first_name":"Frank","last_name":"Cook","email":"fcookg@google.com","gender":"Male","ip_address":"58.30.243.1"},
{"id":18,"first_name":"Alan","last_name":"Rice","email":"ariceh@sciencedirect.com","gender":"Male","ip_address":"44.231.199.117"},
{"id":19,"first_name":"Mark","last_name":"Greene","email":"mgreenei@paypal.com","gender":"Male","ip_address":"45.34.44.2"},
{"id":20,"first_name":"Charles","last_name":"King","email":"ckingj@clickbank.net","gender":"Male","ip_address":"237.30.205.186"}];
//$.getJSON(
//'http://apolosiskos.co.uk/TEB/MOCK_DATA.json',
//function(data) {
var tr;
//It loads data from the JSON file to the table
$.each(data, function(key, val) {
tr = $('<tr/>');
tr.append('<td class="name" rel="' + val.first_name + '">' + val.first_name + '</td>');
tr.append('<td >' + 'TEST' + '</td>');
tr.append('<td class="metric2" >' + val.id + '</td>');
tr.append('<td class="metric3"><span class="multTotal">' + '0.00' + '</span></td>');
tr.append('<td class="metric3-100"><span class="metric3-100">' + '0.00' + '</span></td>');
tr.append('<td class="metric1-100"><span class="metric1-100">' + '0.00' + '</span></td>');
$('table').append(tr);
});
$('body').on('click', 'input[type=checkbox]', minmax);
$('.counter').keyup(minmax);
$('input').keyup(multInputs);
$('body').on('click', '#btnFilter', filtermin);
});
//});
//The filter function for the first column (NAME)
//This have to work together of the minmax function
function filters() {
//if anyone is checked, return true to show all
var checkboxChecked = $('input[type=checkbox]:checked');
if (checkboxChecked.length <= 0) {
$('tr').show();
return true;
}
$('tr').not('.first').hide();
//else, find the name checked and verify with the name passed as parameter
checkboxChecked.each(function() {
var dimension1 = $(this).attr('rel');
var value = $(this).val();
$('td.' + dimension1 + '[rel="' + value + '"]').parent('tr').show();
});
}
//Multiplication of the cells function
function multInputs() {
var mult = 0;
$("tr").each(function() {
var $val1 = $('.metric1', this).val();
var $val2 = $('.metric2', this).text();
var $total = ($val1 * 1) * $val2 - $val1;
$('.multTotal', this).text($total.toPrecision(3));
var $val3 = $('.multTotal', this).text();
var $total2 = $val3 / 100
$('.metric3-100', this).text($total2.toPrecision(3));
var $total3 = $val1 / 100
$('.metric1-100', this).text($total3.toPrecision(2));
mult += $total;
});
}
//The filter function for the MIN MAX values in the MAIN VALUE column
function minmax() {
filters();
$.fn.dataTable.ext.search.push(function(settings, data, dataIndex) {
return parseFloat(data[2]) >= parseFloat($('#counter-low').val() || data[2]) &&
parseFloat(data[2]) <= parseFloat($('#counter-high').val() || data[2]);
});
var table = $('table').DataTable();
$('#counter-low, #counter-high').on('keyup', table.draw);
}
function filtermin() {
var value = $('#filter').val();
$('tr').show();
$('tr td.odds').each(function() {
if ($(this).text() < value) {
$(this).parent().hide();
}
});
}
<head>
<meta charset="utf-8">
<meta http-equiv="Access-Control-Allow-Origin" content="*" />
<title>json extract in datorama tables</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script src="https://cdn.datatables.net/1.10.13/js/jquery.dataTables.min.js"></script>
<script type="text/javascript" language="javascript" src="teste.js"></script>
<link rel="stylesheet" href="css/main.css"/>
<style href="https://cdn.datatables.net/1.10.13/css/jquery.dataTables.min.css"></style>
</head>
<body>
<h1>discrepancy checker after extracting data in JSON</h1>
<hr/>
<div class="row">
<div class="col-sm-4 filter-columns">
<div class="ac-custom ac-checkbox ac-cross" autocomplete="off">
<h2>Dimension1</h2>
<ul>
<li><input rel="name" type="checkbox" value="Debra" id="cb1"><label for="cb1">Debra</label></li>
<li><input rel="name" type="checkbox" value="Julie" id="cb2"><label for="cb2">Julie</label></li>
<li><input rel="name" type="checkbox" value="Norma" id="cb3"><label for="cb3">Norma</label></li>
<li><input rel="name" type="checkbox" value="Bobby" id="cb4"><label for="cb4">Bobby</label></li>
<li><input rel="name" type="checkbox" value="Henry" id="cb5"><label for="cb5">Henry</label></li>
</ul>
</div>
</div>
<div class="col-sm-4 filter-columns-alt">
<div class="ac-custom ac-checkbox ac-cross" autocomplete="off">
<h2>MIN MAX</h2>
Min:<input id="counter-low" class="counter" type="text" />
Max:<input id="counter-high" class="counter" type="text" />
</div>
</div>
<div class="col-sm-4 filter-columns-alt">
<input type='text' id='filter' /> <button id='btnFilter'>Go</button>
</div>
</div>
<div id="body">
<table>
<thead>
<tr id="ProductID" class="first">
<th>NAME</th>
<th>INPUT</th>
<th>MAIN VALUE</th>
<th>DIFF</th>
<th>DIFF /100</th>
<th>MV /100</th>
</tr>
</thead>
<tbody></tbody>
</table>
</div>
</body>
关于javascript - 使用困惑的查询(HTML 表)从 JSON url 添加数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41510656/
我有一台 MySQL 服务器和一台 PostgreSQL 服务器。 需要从多个表中复制或重新插入一组数据 MySQL 流式传输/同步到 PostgreSQL 表。 这种复制可以基于时间(Sync)或事
如果两个表的 id 彼此相等,我尝试从一个表中获取数据。这是我使用的代码: SELECT id_to , email_to , name_to , status_to
我有一个 Excel 工作表。顶行对应于列名称,而连续的行每行代表一个条目。 如何将此 Excel 工作表转换为 SQL 表? 我使用的是 SQL Server 2005。 最佳答案 这取决于您使用哪
我想合并两个 Django 模型并创建一个模型。让我们假设我有第一个表表 A,其中包含一些列和数据。 Table A -------------- col1 col2 col3 col
我有两个表:table1,table2,如下所示 table1: id name 1 tamil 2 english 3 maths 4 science table2: p
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 1 年前。 Improve th
下面两个语句有什么区别? newTable = orginalTable 或 newTable.data(originalTable) 我怀疑 .data() 方法具有性能优势,因为它在标准 AX 中
我有一个表,我没有在其中显式定义主键,它并不是真正需要的功能......但是一位同事建议我添加一个列作为唯一主键以随着数据库的增长提高性能...... 谁能解释一下这是如何提高性能的? 没有使用索引(
如何将表“产品”中的产品记录与其不同表“图像”中的图像相关联? 我正在对产品 ID 使用自动增量。 我觉得不可能进行关联,因为产品 ID 是自动递增的,因此在插入期间不可用! 如何插入新产品,获取产品
我有一个 sql 表,其中包含关键字和出现次数,如下所示(尽管出现次数并不重要): ____________ dog | 3 | ____________ rat | 7 | ____
是否可以使用目标表中的LAST_INSERT_ID更新源表? INSERT INTO `target` SELECT `a`, `b` FROM `source` 目标表有一个自动增量键id,我想将其
我正在重建一个搜索查询,因为它在“我看到的”中变得多余,我想知道什么 (albums_artists, artists) ( ) does in join? is it for boosting pe
以下是我使用 mysqldump 备份数据库的开关: /usr/bin/mysqldump -u **** --password=**** --single-transaction --databas
我试图获取 MySQL 表中的所有行并将它们放入 HTML 表中: Exam ID Status Assigned Examiner
如何查询名为 photos 的表中的所有记录,并知道当前用户使用单个查询将哪些结果照片添加为书签? 这是我的表格: -- -- Table structure for table `photos` -
我的网站都在 InnoDB 表上运行,目前为止运行良好。现在我想知道在我的网站上实时发生了什么,所以我将每个页面浏览量(页面、引荐来源网址、IP、主机名等)存储在 InnoDB 表中。每秒大约有 10
我在想我会为 mysql 准备两个表。一个用于存储登录信息,另一个用于存储送货地址。这是传统方式还是所有内容都存储在一张表中? 对于两个表...有没有办法自动将表 A 的列复制到表 B,以便我可以引用
我不是程序员,我从这个表格中阅读了很多关于如何解决我的问题的内容,但我的搜索效果不好 我有两张 table 表 1:成员 id*| name | surname -------------------
我知道如何在 ASP.NET 中显示真实表,例如 public ActionResult Index() { var s = db.StaffInfoDBSet.ToList(); r
我正在尝试运行以下查询: "insert into visits set source = 'http://google.com' and country = 'en' and ref = '1234
我是一名优秀的程序员,十分优秀!