gpt4 book ai didi

javascript - 单击 HTML 表格内的下 zipper 接时如何动态调用页面?

转载 作者:行者123 更新时间:2023-11-29 20:47:57 24 4
gpt4 key购买 nike

我有一个 HTML 表格,其中包含指向多个列的下 zipper 接。

我已经实现了,如果有人点击链接,它就会转发到那个链接。

我想要实现的是,就像我有不同的 HTML 表格一样,当特定的选定下拉列发生点击事件时,我想在同一页面上调用它。

这是我的代码

<div id="tbl"></div>

<ul id="contextMenu" class="dropdown-menu" role="menu">
<li><a href="test1.html" class="link1">report1</a></li>
<li><a href="" class="Link2">report2</a></li>
</ul>

$(window).load(function() {
$dropdown = $("#contextMenu");
$(".actionButton").click(function() {
//get row ID

//move dropdown menu
$(this).after($dropdown);
//update links

$(this).dropdown();
});
});

data= [
{
"amount": 291589,
"billdate": "2018-08-01",
"outlet": "JAYANAGAR"
},
{
"amount": 58337,
"billdate": "2018-08-01",
"outlet": "MALLESHWARAM"
},
{
"amount": 65970,
"billdate": "2018-08-01",
"outlet": "KOLAR"
},
{
"amount": 296125,
"billdate": "2018-08-02",
"outlet": "JAYANAGAR"
},
{
"amount": 56545,
"billdate": "2018-08-02",
"outlet": "MALLESHWARAM"
},
{
"amount": 72213,
"billdate": "2018-08-02",
"outlet": "KOLAR"
},
{
"amount": 346605,
"billdate": "2018-08-03",
"outlet": "JAYANAGAR"
},
{
"amount": 62459,
"billdate": "2018-08-03",
"outlet": "MALLESHWARAM"
},
{
"amount": 65248,
"billdate": "2018-08-03",
"outlet": "KOLAR"
},
{
"amount": 518212,
"billdate": "2018-08-04",
"outlet": "JAYANAGAR"
},
{
"amount": 104801,
"billdate": "2018-08-04",
"outlet": "MALLESHWARAM"
},
{
"amount": 138151,
"billdate": "2018-08-04",
"outlet": "KOLAR"
},
{
"amount": 628358,
"billdate": "2018-08-05",
"outlet": "JAYANAGAR"
},
{
"amount": 115223,
"billdate": "2018-08-05",
"outlet": "MALLESHWARAM"
},
{
"amount": 134107,
"billdate": "2018-08-05",
"outlet": "KOLAR"
},
{
"amount": 177866,
"billdate": "2018-08-06",
"outlet": "JAYANAGAR"
},
{
"amount": 66095,
"billdate": "2018-08-06",
"outlet": "KOLAR"
},
{
"amount": 284069,
"billdate": "2018-08-07",
"outlet": "JAYANAGAR"
},
{
"amount": 58789,
"billdate": "2018-08-07",
"outlet": "MALLESHWARAM"
},
{
"amount": 67886,
"billdate": "2018-08-07",
"outlet": "KOLAR"
},
{
"amount": 313128,
"billdate": "2018-08-08",
"outlet": "JAYANAGAR"
},
{
"amount": 59939,
"billdate": "2018-08-08",
"outlet": "MALLESHWARAM"
},
{
"amount": 68558,
"billdate": "2018-08-08",
"outlet": "KOLAR"
}
]

let formatData = function (data) {

let billdates = [];
let outlets = [];
data.forEach(element => {
if (billdates.indexOf(element.billdate) == -1) {
billdates.push(element.billdate);
}
if (outlets.indexOf(element.outlet) == -1) {
outlets.push(element.outlet);
}
});
return {
data: data,
billdates: billdates,
outlets: outlets,

};
};



let renderTable = function (data) {
billdates = data.billdates;
outlets = data.outlets;
data = data.data;
let tbl = document.getElementById("tbl");
let table = document.createElement("table");
let thead = document.createElement("thead");
let headerRow = document.createElement("tr");
let th = document.createElement("th");
th.innerHTML = "Bill___Date";
th.classList.add("text-center");
headerRow.appendChild(th);
let grandTotal = 0;
let outletWiseTotal = {};
th = document.createElement("th");
th.innerHTML = "Total";
th.classList.add("text-center");
headerRow.appendChild(th);

outlets.forEach(element => {
th = document.createElement("th");
th.innerHTML = element;
th.classList.add("text-center");
headerRow.appendChild(th);
outletWiseTotal[element] = 0;
data.forEach(el => {
if (el.outlet == element) {
outletWiseTotal[element] += parseInt(el.amount);
}
});
grandTotal += outletWiseTotal[element];
});


thead.appendChild(headerRow);
headerRow = document.createElement("tr");
th = document.createElement("th");
th.innerHTML = "Total";
th.classList.add("text-center");

headerRow.appendChild(th);

outlets.forEach(element => {
th = document.createElement("th");
th.innerHTML = outletWiseTotal[element];
th.classList.add("text-right"); //ol totals
headerRow.appendChild(th);
});
/* th = document.createElement("th");
a = document.createElement("a");
// th.innerHTML = "Action drop"; // removed
th.classList.add("text-center");
th.classList.add("dropdown");
a.classList.add("btn-default");
a.classList.add("actionButton");


// added
a.setAttribute("data-toggle", "dropdown");
a.innerHTML = "Action drop";
th.appendChild(a); // added

headerRow.appendChild(th); */
th = document.createElement("th");
th.innerHTML = grandTotal;
th.classList.add("text-right"); // grand total
/* console.log(grandTotal);*/
// headerRow.appendChild(th);
headerRow.insertBefore(th , headerRow.children[1] );
thead.appendChild(headerRow);
table.appendChild(thead);

let tbody = document.createElement("tbody");


billdates.forEach(element => {
let row = document.createElement("tr");
td = document.createElement("td");
td.innerHTML = element;
row.appendChild(td);
let total = 0;
outlets.forEach(outlet => {
let el = 0;
data.forEach(d => {
if (d.billdate == element && d.outlet == outlet) {
total += parseInt(d.amount);
el = d.amount;
}
});
/* td = document.createElement("td");
td.innerHTML = el; */



td = document.createElement("td");
a = document.createElement("a");
// th.innerHTML = "Action drop"; // removed
td.classList.add("text-right");
td.classList.add("dropdown");
a.classList.add("btn-default");
a.classList.add("actionButton");


// added
a.setAttribute("data-toggle", "dropdown");
a.innerHTML = el;
td.appendChild(a); // added

row.appendChild(td);



/* td.classList.add("text-right"); //oldata
row.appendChild(td); */
});
/*console.log("row is : " , row.children )*/
td = document.createElement("td");
td.innerHTML = total;
td.classList.add("text-right"); //column total
// row.appendChild(td);
row.insertBefore(td , row.children[1] );
tbody.appendChild(row);

});

table.appendChild(tbody);

tbl.innerHTML = "";
tbl.appendChild(table);
table.classList.add("table");
table.classList.add("table-striped");
table.classList.add("table-bordered");
table.classList.add("table-hover");
}
let formatedData = formatData(data);
renderTable(formatedData);

在这里,我提供了一个链接('test.html')来检查。

在我的 HTML 表格中,我有三个列,分别命名为 JAYANAGARMALLESHWARAMKOLAR。对于这三列,我将下拉菜单命名为 Report1 和 Report2。当用户单击 JAYANAGAR 列的 Report1 下拉列表时,它应该仅显示 Jayanagar 数据。对于所有三列,我的代码中已有链接或新表。我只想在单击列下拉列表并重定向或呈现分配给该链接的表时过滤动态调用。

  • 如果用户点击 jayanagar 列的下拉菜单,那么它应该在同一页面的某些 div 标签中显示 jayanagar 数据
  • 如果用户点击 Maleeshwaram 的下拉菜单,那么它应该在同一个 div 标签中显示 malleshwaram 数据。

  • 我已经根据网点过滤了数据。只想在 java 脚本结束时执行它。

  • 我只想知道用户点击的是哪一栏的下拉菜单。

Fiddle

最佳答案

您已经有了通用功能,您只需要根据点击和完成来过滤数据,我已经过滤了您的数据并绑定(bind)到特定的 div。它现在应该可以工作了。

这部分很重要,需要对数据进行过滤,绑定(bind)到不同的div。

$(".dropdown-menu li a").click(function() {

if (currentlyClickedPlace !== "" && currentlyClickedPlace !== undefined) {
var filteredData = data.filter(a => a.outlet === currentlyClickedPlace);
let formatedData = formatData(filteredData);
renderTable(formatedData, 'test', '');
}
})

var currentlyClickedPlace = "";
$(window).load(function() {
$dropdown = $("#contextMenu");
$(".actionButton").click(function() {
//move dropdown menu
$(this).after($dropdown);
//update links
$(this).dropdown();

currentlyClickedPlace = $(this).attr("data-place");
});
$(".dropdown-menu li a").click(function() {
if (currentlyClickedPlace !== "" && currentlyClickedPlace !== undefined) {
var filteredData = data.filter(a => a.outlet === currentlyClickedPlace);
let formatedData = formatData(filteredData);
renderTable(formatedData, 'test', '');
}
})
});

data = [{
"amount": 291589,
"billdate": "2018-08-01",
"outlet": "JAYANAGAR"
},
{
"amount": 58337,
"billdate": "2018-08-01",
"outlet": "MALLESHWARAM"
},
{
"amount": 65970,
"billdate": "2018-08-01",
"outlet": "KOLAR"
},
{
"amount": 296125,
"billdate": "2018-08-02",
"outlet": "JAYANAGAR"
},
{
"amount": 56545,
"billdate": "2018-08-02",
"outlet": "MALLESHWARAM"
},
{
"amount": 72213,
"billdate": "2018-08-02",
"outlet": "KOLAR"
},
{
"amount": 346605,
"billdate": "2018-08-03",
"outlet": "JAYANAGAR"
},
{
"amount": 62459,
"billdate": "2018-08-03",
"outlet": "MALLESHWARAM"
},
{
"amount": 65248,
"billdate": "2018-08-03",
"outlet": "KOLAR"
},
{
"amount": 518212,
"billdate": "2018-08-04",
"outlet": "JAYANAGAR"
},
{
"amount": 104801,
"billdate": "2018-08-04",
"outlet": "MALLESHWARAM"
},
{
"amount": 138151,
"billdate": "2018-08-04",
"outlet": "KOLAR"
},
{
"amount": 628358,
"billdate": "2018-08-05",
"outlet": "JAYANAGAR"
},
{
"amount": 115223,
"billdate": "2018-08-05",
"outlet": "MALLESHWARAM"
},
{
"amount": 134107,
"billdate": "2018-08-05",
"outlet": "KOLAR"
},
{
"amount": 177866,
"billdate": "2018-08-06",
"outlet": "JAYANAGAR"
},
{
"amount": 66095,
"billdate": "2018-08-06",
"outlet": "KOLAR"
},
{
"amount": 284069,
"billdate": "2018-08-07",
"outlet": "JAYANAGAR"
},
{
"amount": 58789,
"billdate": "2018-08-07",
"outlet": "MALLESHWARAM"
},
{
"amount": 67886,
"billdate": "2018-08-07",
"outlet": "KOLAR"
},
{
"amount": 313128,
"billdate": "2018-08-08",
"outlet": "JAYANAGAR"
},
{
"amount": 59939,
"billdate": "2018-08-08",
"outlet": "MALLESHWARAM"
},
{
"amount": 68558,
"billdate": "2018-08-08",
"outlet": "KOLAR"
}
]

let formatData = function(data) {

let billdates = [];
let outlets = [];
data.forEach(element => {
if (billdates.indexOf(element.billdate) == -1) {
billdates.push(element.billdate);
}
if (outlets.indexOf(element.outlet) == -1) {
outlets.push(element.outlet);
}
});
return {
data: data,
billdates: billdates,
outlets: outlets,

};
};



let renderTable = function(data, divId, filterdata) {
billdates = data.billdates;
outlets = data.outlets;
data = data.data;
let tbl = document.getElementById(divId);
let table = document.createElement("table");
let thead = document.createElement("thead");
let headerRow = document.createElement("tr");
let th = document.createElement("th");
th.innerHTML = "Bill___Date";
th.classList.add("text-center");
headerRow.appendChild(th);
let grandTotal = 0;
let outletWiseTotal = {};
th = document.createElement("th");
th.innerHTML = "Total";
th.classList.add("text-center");
headerRow.appendChild(th);

outlets.forEach(element => {
th = document.createElement("th");
th.innerHTML = element;
th.classList.add("text-center");
headerRow.appendChild(th);
outletWiseTotal[element] = 0;
data.forEach(el => {
if (el.outlet == element) {
outletWiseTotal[element] += parseInt(el.amount);
}
});
grandTotal += outletWiseTotal[element];
});


thead.appendChild(headerRow);
headerRow = document.createElement("tr");
th = document.createElement("th");
th.innerHTML = "Total";
th.classList.add("text-center");

headerRow.appendChild(th);

outlets.forEach(element => {
th = document.createElement("th");
th.innerHTML = outletWiseTotal[element];
th.classList.add("text-right"); //ol totals
headerRow.appendChild(th);
});
/* th = document.createElement("th");
a = document.createElement("a");
// th.innerHTML = "Action drop"; // removed
th.classList.add("text-center");
th.classList.add("dropdown");
a.classList.add("btn-default");
a.classList.add("actionButton");


// added
a.setAttribute("data-toggle", "dropdown");
a.innerHTML = "Action drop";
th.appendChild(a); // added

headerRow.appendChild(th); */
th = document.createElement("th");
th.innerHTML = grandTotal;
th.classList.add("text-right"); // grand total
/* console.log(grandTotal);*/
// headerRow.appendChild(th);
headerRow.insertBefore(th, headerRow.children[1]);
thead.appendChild(headerRow);
table.appendChild(thead);

let tbody = document.createElement("tbody");

billdates.forEach(element => {
let row = document.createElement("tr");
td = document.createElement("td");
td.innerHTML = element;
row.appendChild(td);
let total = 0;
outlets.forEach(outlet => {
let el = 0;
data.forEach(d => {
if (d.billdate == element && d.outlet == outlet) {
total += parseInt(d.amount);
el = d.amount;
}
});
/* td = document.createElement("td");
td.innerHTML = el; */



td = document.createElement("td");
a = document.createElement("a");
// th.innerHTML = "Action drop"; // removed
td.classList.add("text-right");
td.classList.add("dropdown");
a.classList.add("btn-default");
a.classList.add("actionButton");

a.setAttribute("data-place", outlet);
// added
a.setAttribute("data-toggle", "dropdown");
a.innerHTML = el;
td.appendChild(a); // added

row.appendChild(td);



/* td.classList.add("text-right"); //oldata
row.appendChild(td); */
});
/*console.log("row is : " , row.children )*/
td = document.createElement("td");
td.innerHTML = total;
td.classList.add("text-right"); //column total
// row.appendChild(td);
row.insertBefore(td, row.children[1]);
tbody.appendChild(row);

});

table.appendChild(tbody);

tbl.innerHTML = "";
tbl.appendChild(table);
table.classList.add("table");
table.classList.add("table-striped");
table.classList.add("table-bordered");
table.classList.add("table-hover");
}
let formatedData = formatData(data);
renderTable(formatedData, 'tbl', '');
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<title>JS Bin</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-2.0.2.js"></script>
<link rel="stylesheet" type="text/css" href="http://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" />
<script type="text/javascript" src="http://netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
<link rel="stylesheet" type="text/css" href="http://netdna.bootstrapcdn.com/font-awesome/3.2.1/css/font-awesome.min.css" />

<div id="tbl"></div>
<div id="test"></div>
<ul id="contextMenu" class="dropdown-menu" role="menu">
<li><a href="#" class="link1">Report1</a></li>
<li><a href="#" class="Link2">Report2</a></li>
</ul>

编辑:您可以为报告 1 和报告 2 绑定(bind)单独的链接并调用 ajax 函数

var currentlyClickedPlace = "";
$(window).load(function() {
$dropdown = $("#contextMenu");
$(".actionButton").click(function() {
//move dropdown menu
$(this).after($dropdown);
//update links
$(this).dropdown();

currentlyClickedPlace = $(this).attr("data-place");
});
$(".link1").click(function(){
alert('report 1 is clicked in ' + currentlyClickedPlace);
//call ajax and bind the data into test div
});
$(".Link2").click(function(){
alert('report 2 is clicked in ' + currentlyClickedPlace);
//call ajax and bind the data into test div
});
});

data = [{
"amount": 291589,
"billdate": "2018-08-01",
"outlet": "JAYANAGAR"
},
{
"amount": 58337,
"billdate": "2018-08-01",
"outlet": "MALLESHWARAM"
},
{
"amount": 65970,
"billdate": "2018-08-01",
"outlet": "KOLAR"
},
{
"amount": 296125,
"billdate": "2018-08-02",
"outlet": "JAYANAGAR"
},
{
"amount": 56545,
"billdate": "2018-08-02",
"outlet": "MALLESHWARAM"
},
{
"amount": 72213,
"billdate": "2018-08-02",
"outlet": "KOLAR"
},
{
"amount": 346605,
"billdate": "2018-08-03",
"outlet": "JAYANAGAR"
},
{
"amount": 62459,
"billdate": "2018-08-03",
"outlet": "MALLESHWARAM"
},
{
"amount": 65248,
"billdate": "2018-08-03",
"outlet": "KOLAR"
},
{
"amount": 518212,
"billdate": "2018-08-04",
"outlet": "JAYANAGAR"
},
{
"amount": 104801,
"billdate": "2018-08-04",
"outlet": "MALLESHWARAM"
},
{
"amount": 138151,
"billdate": "2018-08-04",
"outlet": "KOLAR"
},
{
"amount": 628358,
"billdate": "2018-08-05",
"outlet": "JAYANAGAR"
},
{
"amount": 115223,
"billdate": "2018-08-05",
"outlet": "MALLESHWARAM"
},
{
"amount": 134107,
"billdate": "2018-08-05",
"outlet": "KOLAR"
},
{
"amount": 177866,
"billdate": "2018-08-06",
"outlet": "JAYANAGAR"
},
{
"amount": 66095,
"billdate": "2018-08-06",
"outlet": "KOLAR"
},
{
"amount": 284069,
"billdate": "2018-08-07",
"outlet": "JAYANAGAR"
},
{
"amount": 58789,
"billdate": "2018-08-07",
"outlet": "MALLESHWARAM"
},
{
"amount": 67886,
"billdate": "2018-08-07",
"outlet": "KOLAR"
},
{
"amount": 313128,
"billdate": "2018-08-08",
"outlet": "JAYANAGAR"
},
{
"amount": 59939,
"billdate": "2018-08-08",
"outlet": "MALLESHWARAM"
},
{
"amount": 68558,
"billdate": "2018-08-08",
"outlet": "KOLAR"
}
]

let formatData = function(data) {

let billdates = [];
let outlets = [];
data.forEach(element => {
if (billdates.indexOf(element.billdate) == -1) {
billdates.push(element.billdate);
}
if (outlets.indexOf(element.outlet) == -1) {
outlets.push(element.outlet);
}
});
return {
data: data,
billdates: billdates,
outlets: outlets,

};
};



let renderTable = function(data, divId, filterdata) {
billdates = data.billdates;
outlets = data.outlets;
data = data.data;
let tbl = document.getElementById(divId);
let table = document.createElement("table");
let thead = document.createElement("thead");
let headerRow = document.createElement("tr");
let th = document.createElement("th");
th.innerHTML = "Bill___Date";
th.classList.add("text-center");
headerRow.appendChild(th);
let grandTotal = 0;
let outletWiseTotal = {};
th = document.createElement("th");
th.innerHTML = "Total";
th.classList.add("text-center");
headerRow.appendChild(th);

outlets.forEach(element => {
th = document.createElement("th");
th.innerHTML = element;
th.classList.add("text-center");
headerRow.appendChild(th);
outletWiseTotal[element] = 0;
data.forEach(el => {
if (el.outlet == element) {
outletWiseTotal[element] += parseInt(el.amount);
}
});
grandTotal += outletWiseTotal[element];
});


thead.appendChild(headerRow);
headerRow = document.createElement("tr");
th = document.createElement("th");
th.innerHTML = "Total";
th.classList.add("text-center");

headerRow.appendChild(th);

outlets.forEach(element => {
th = document.createElement("th");
th.innerHTML = outletWiseTotal[element];
th.classList.add("text-right"); //ol totals
headerRow.appendChild(th);
});
/* th = document.createElement("th");
a = document.createElement("a");
// th.innerHTML = "Action drop"; // removed
th.classList.add("text-center");
th.classList.add("dropdown");
a.classList.add("btn-default");
a.classList.add("actionButton");


// added
a.setAttribute("data-toggle", "dropdown");
a.innerHTML = "Action drop";
th.appendChild(a); // added

headerRow.appendChild(th); */
th = document.createElement("th");
th.innerHTML = grandTotal;
th.classList.add("text-right"); // grand total
/* console.log(grandTotal);*/
// headerRow.appendChild(th);
headerRow.insertBefore(th, headerRow.children[1]);
thead.appendChild(headerRow);
table.appendChild(thead);

let tbody = document.createElement("tbody");

billdates.forEach(element => {
let row = document.createElement("tr");
td = document.createElement("td");
td.innerHTML = element;
row.appendChild(td);
let total = 0;
outlets.forEach(outlet => {
let el = 0;
data.forEach(d => {
if (d.billdate == element && d.outlet == outlet) {
total += parseInt(d.amount);
el = d.amount;
}
});
/* td = document.createElement("td");
td.innerHTML = el; */



td = document.createElement("td");
a = document.createElement("a");
// th.innerHTML = "Action drop"; // removed
td.classList.add("text-right");
td.classList.add("dropdown");
a.classList.add("btn-default");
a.classList.add("actionButton");

a.setAttribute("data-place", outlet);
// added
a.setAttribute("data-toggle", "dropdown");
a.innerHTML = el;
td.appendChild(a); // added

row.appendChild(td);



/* td.classList.add("text-right"); //oldata
row.appendChild(td); */
});
/*console.log("row is : " , row.children )*/
td = document.createElement("td");
td.innerHTML = total;
td.classList.add("text-right"); //column total
// row.appendChild(td);
row.insertBefore(td, row.children[1]);
tbody.appendChild(row);

});

table.appendChild(tbody);

tbl.innerHTML = "";
tbl.appendChild(table);
table.classList.add("table");
table.classList.add("table-striped");
table.classList.add("table-bordered");
table.classList.add("table-hover");
}
let formatedData = formatData(data);
renderTable(formatedData, 'tbl', '');
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<title>JS Bin</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-2.0.2.js"></script>
<link rel="stylesheet" type="text/css" href="http://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" />
<script type="text/javascript" src="http://netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
<link rel="stylesheet" type="text/css" href="http://netdna.bootstrapcdn.com/font-awesome/3.2.1/css/font-awesome.min.css" />

<div id="tbl"></div>
<div id="test"></div>
<ul id="contextMenu" class="dropdown-menu" role="menu">
<li><a href="#" class="link1">Report1</a></li>
<li><a href="#" class="Link2">Report2</a></li>
</ul>

关于javascript - 单击 HTML 表格内的下 zipper 接时如何动态调用页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53589075/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com