将可排序表与复选框总和输出相结合

2024-03-02

我当前的代码有一个复选框求和函数,它从数据库中获取值。

我还尝试将另一个 Javascript 代码应用到我的表中,以允许我对行中的数据进行排序。

例如,对行进行排序后,将所有 5 个 ACER 资产显示在顶部。我想勾选前 5 个显示的 ACER 资产的复选框并对它们求和。

这是我当前的代码:

<?php
    include('connect1.php');
    $retrieve = $db->prepare("SELECT count(*) FROM Asset");
    $retrieve->execute();
    $fetchrow = $retrieve->fetch(PDO::FETCH_NUM);
    $calculated=$fetchrow[0];
?>

<script type="text/javascript">

    function UpdateCost() {
        var sum = 0;
        var gn, elem;
        for (i=0; i<<?php echo $calculated ?>; i++) {
            gn = 'sum_m_'+i;
            elem = document.getElementById(gn);
            if (elem.checked == true) { sum += Number(elem.value); }
        }
        document.getElementById('totalcost' ).value = sum.toFixed(0);
    }
    window.onload=UpdateCost

</script>

<link rel="stylesheet" href="style.css" />

Total Cost : <input type="text" name="sen" id="totalcost" value="" />

<table cellpadding="0" cellspacing="0" border="0" id="table" class="sortable">
    <thead>
        <tr>
            <th><h3>Asset ID</h3></th>
            <th><h3>Vendor</h3></th>
            <th><h3>Hardware </h3></th>
            <th><h3>Cost</h3></th>
            <th><h3>Date Of Purchase</h3></th>
            <th><h3>Select Values</h3></th>

        </tr>
    </thead>
    <tbody>
        <?php
            // include('connect1.php'); you already included this file
            $result = $db->prepare("SELECT * FROM Asset");
            $result->bindParam(':userid', $res);
            $result->execute();
            for($i=0; $row = $result->fetch(); $i++){
            ?>
            <tr>
                <td><?php echo $row['Asset_ID']; ?></td>
                <td><?php echo $row['Vendor_Name']; ?></td>
                <td><?php echo $row['Hardware_ID']; ?></td>
                <td><?php echo $row['Asset_Cost']; ?></td>
                <td><?php echo $row['DateOfPurchase']; ?></td>
                <th><h3><INPUT TYPE="checkbox" NAME="items[]" value="<?php echo $row['Asset_Cost'] ?>" id="sum_m_<?php echo $i ?>" onclick="UpdateCost()"></h3></th>

            </tr>
            <?php
            }
        ?>
    </tbody>
</table>
<br />

<div id="controls">
    <div id="perpage">
        <select onchange="sorter.size(this.value)">
            <option value="5">5</option>
            <option value="10" selected="selected">10</option>
            <option value="20">20</option>
            <option value="50">50</option>
            <option value="100">100</option>
        </select>
        <span>Entries Per Page</span>
        </div>
        <div id="navigation">
            <img src="images/first.gif" width="16" height="16" alt="First Page" onclick="sorter.move(-1,true)" />
            <img src="images/previous.gif" width="16" height="16" alt="First Page" onclick="sorter.move(-1)" />
            <img src="images/next.gif" width="16" height="16" alt="First Page" onclick="sorter.move(1)" />
            <img src="images/last.gif" width="16" height="16" alt="Last Page" onclick="sorter.move(1,true)" />
        </div>
        <div id="text">Displaying Page <span id="currentpage"></span> of <span id="pagelimit"></span></div>
    </div> 

这是 JavaScript:

var TINY={};

function T$(i){return document.getElementById(i)}
function T$$(e,p){return p.getElementsByTagName(e)}

TINY.table=function(){
    function sorter(n){this.n=n; this.pagesize=20; this.paginate=0}
    sorter.prototype.init=function(e,f){
        var t=ge(e), i=0; this.e=e; this.l=t.r.length; t.a=[];
        t.h=T$$('thead',T$(e))[0].rows[0]; t.w=t.h.cells.length;
        for(i;i<t.w;i++){
            var c=t.h.cells[i];
            if(c.className!='nosort'){
                c.className=this.head; c.onclick=new Function(this.n+'.wk(this.cellIndex)')
            }
        }
        for(i=0;i<this.l;i++){t.a[i]={}}
        if(f!=null){var a=new Function(this.n+'.wk('+f+')'); a()}
        if(this.paginate){this.g=1; this.pages()}
    };
    sorter.prototype.wk=function(y){
        var t=ge(this.e), x=t.h.cells[y], i=0;
        for(i;i<this.l;i++){
      t.a[i].o=i; var v=t.r[i].cells[y]; t.r[i].style.display='';
      while(v.hasChildNodes()){v=v.firstChild}
      t.a[i].v=v.nodeValue?v.nodeValue:''
    }
        for(i=0;i<t.w;i++){var c=t.h.cells[i]; if(c.className!='nosort'){c.className=this.head}}
        if(t.p==y){t.a.reverse(); x.className=t.d?this.asc:this.desc; t.d=t.d?0:1}
        else{t.p=y; t.a.sort(cp); t.d=0; x.className=this.asc}
        var n=document.createElement('tbody');
        for(i=0;i<this.l;i++){
            var r=t.r[t.a[i].o].cloneNode(true); n.appendChild(r);
            r.className=i%2==0?this.even:this.odd; var cells=T$$('td',r);
            for(var z=0;z<t.w;z++){cells[z].className=y==z?i%2==0?this.evensel:this.oddsel:''}
        }
        t.replaceChild(n,t.b); if(this.paginate){this.size(this.pagesize)}
    };
    sorter.prototype.page=function(s){
        var t=ge(this.e), i=0, l=s+parseInt(this.pagesize);
        if(this.currentid&&this.limitid){T$(this.currentid).innerHTML=this.g}
        for(i;i<this.l;i++){t.r[i].style.display=i>=s&&i<l?'':'none'}
    };
    sorter.prototype.move=function(d,m){
        var s=d==1?(m?this.d:this.g+1):(m?1:this.g-1);
        if(s<=this.d&&s>0){this.g=s; this.page((s-1)*this.pagesize)}
    };
    sorter.prototype.size=function(s){
        this.pagesize=s; this.g=1; this.pages(); this.page(0);
        if(this.currentid&&this.limitid){T$(this.limitid).innerHTML=this.d}
    };
    sorter.prototype.pages=function(){this.d=Math.ceil(this.l/this.pagesize)};
    function ge(e){var t=T$(e); t.b=T$$('tbody',t)[0]; t.r=t.b.rows; return t};
    function cp(f,c){
        var g,h; f=g=f.v.toLowerCase(), c=h=c.v.toLowerCase();
        var i=parseFloat(f.replace(/(\$|\,)/g,'')), n=parseFloat(c.replace(/(\$|\,)/g,''));
        if(!isNaN(i)&&!isNaN(n)){g=i,h=n}
        i=Date.parse(f); n=Date.parse(c);
        if(!isNaN(i)&&!isNaN(n)){g=i; h=n}
        return g>h?1:(g<h?-1:0)
    };
    return{sorter:sorter}
}();

这是实际工作的可排序表:

<?php
    include('connect1.php');
    $retrieve = $db->prepare("SELECT count(*) FROM Asset");
    $retrieve->execute();
    $fetchrow = $retrieve->fetch(PDO::FETCH_NUM);
    $calculated=$fetchrow[0];
?>

<script type="text/javascript">

    function UpdateCost() {
        var sum = 0;
        var gn, elem;
        for (i=0; i<<?php echo $calculated ?>; i++) {
            gn = 'sum_m_'+i;
            elem = document.getElementById(gn);
            if (elem.checked == true) { sum += Number(elem.value); }
        }
        document.getElementById('totalcost' ).value = sum.toFixed(0);
    }
    window.onload=UpdateCost

</script>


<input type="button" value="Search" onclick="window.location.href='http://mp04.bit-mp.biz/Ashvin/SearchACost.php'" />

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Costing</title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
    <table cellpadding="0" cellspacing="0" border="0" id="table" class="sortable">
        <thead>
            <tr>


                <th><h3>Asset ID</h3></th>
                <th><h3>Vendor</h3></th>
                <th><h3>Hardware </h3></th>
                <th><h3>Cost</h3></th>
                <th><h3><INPUT TYPE="checkbox" NAME="items[]" value="<?php echo $row['Asset_Cost'] ?>" id="sum_m_<?php echo $i ?>" onclick="UpdateCost()"></h3></th>

            </tr>
        </thead>



        <tbody>

        <?php

        mysql_connect("localhost","user","pass");
mysql_select_db("db");

$result = mysql_query("SELECT * FROM Asset");

        while($row = mysql_fetch_array($result)) 

         for($i=0; $row = $result->fetch(); $i++){
 { 


    echo "<tr>";

    echo "<td>" . $row['Asset_ID'] . "</td>"; 
    echo "<td>" . $row['Vendor_Name'] . "</td>"; 
    echo "<td>" . $row['Hardware_ID'] . "</td>"; 
    echo "<td>" . $row['Asset_Cost'] . "</td>"; 

    echo "</tr>";
 } 
         }
echo "</table>";

            ?>

        </tbody>
  </table>
    <div id="controls">
        <div id="perpage">
            <select onchange="sorter.size(this.value)">
            <option value="5">5</option>
                <option value="10" selected="selected">10</option>
                <option value="20">20</option>
                <option value="50">50</option>
                <option value="100">100</option>
            </select>
            <span>Entries Per Page</span>
        </div>
        <div id="navigation">
            <img src="images/first.gif" width="16" height="16" alt="First Page" onclick="sorter.move(-1,true)" />
            <img src="images/previous.gif" width="16" height="16" alt="First Page" onclick="sorter.move(-1)" />
            <img src="images/next.gif" width="16" height="16" alt="First Page" onclick="sorter.move(1)" />
            <img src="images/last.gif" width="16" height="16" alt="Last Page" onclick="sorter.move(1,true)" />
        </div>
        <div id="text">Displaying Page <span id="currentpage"></span> of <span id="pagelimit"></span></div>
    </div>
    <script type="text/javascript" src="script.js"></script>
<script type="text/javascript">
  var sorter = new TINY.table.sorter("sorter");
    sorter.head = "head";
    sorter.asc = "asc";
    sorter.desc = "desc";
    sorter.even = "evenrow";
    sorter.odd = "oddrow";
    sorter.evensel = "evenselected";
    sorter.oddsel = "oddselected";
    sorter.paginate = true;
    sorter.currentid = "currentpage";
    sorter.limitid = "pagelimit";
    sorter.init("table",1);
  </script>

</body>
</html>

None

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将可排序表与复选框总和输出相结合 的相关文章

随机推荐

  • qt webengine将javascript输出重定向到GUI

    我有一个可以查看页面的应用程序QWebEngineView小部件如何将我的 javascript 控制台日志重定向到我的 GUI 它当前显示在我的调试输出中 您必须子类化 QWebEnginePage 才能覆盖 javaScriptCons
  • 使用immulator时在android studio中使用protocol=http/1.1、code=200在Retrofit中获取F响应

    我正在从 Nodejs 本地服务器调用 API API正在调用 但Retrofit的respose body中没有数据 代码 200 和消息 OK 我收到以下回复 Response protocol http 1 1 code 200 me
  • FlexSlider 2 标题在 chrome 上闪烁

    我在 chrome 上的标题有些闪烁 http tyni cc flexsliderissue http tyni cc flexsliderissue在野生动物园中一切都运行良好 任何人都知道出了什么问题吗 谢谢 丹尼尔 我经常使用 Fl
  • React hooks - 当状态更改为相同值时如何强制 useEffect 运行?

    所以我正在构建一个鼓垫类型的应用程序 除了这个之外 几乎所有东西都可以正常工作 编辑 把整个东西放在codesandbox上 如果有人想看的话 codesandbox io s sleepy darwin jc9b5 file src Ap
  • 连接到存储在 Azure VM 上的 Nexus 3 Preview 上的 Docker 注册表时出现问题

    基本上 我在连接到我尝试使用最新的 Nexus 预览版创建的 docker 注册表时遇到了问题 首先 我在运行 Ubuntu 14 04 trusty 的 MS Azure 云服务器上安装了 Sonotype Nexus 3 Preview
  • 如何在 PHP 中使用 UTF-8 支持替换所有非字母字符

    我想从字符串中删除所有非字母字符 问题是我不知道字母范围 因为它是 UTF8 字符串 可以是英语 我通常会做这样的事情 str preg replace a zA Z str or str preg replace w u str 但它们都
  • CSS 打印预览 - 显示图像/链接 URL

    我正在创建一个打印样式表 并且希望通过在链接和图像旁边显示链接 URL 来使网站尽可能易于访问 对于简单的链接我找到了这个解决方案 http davidwalsh name optimize your links for print usi
  • 如何在 Android 手机上签署“.apk”文件?

    我想在手机上生成一个新的 apk 文件 我的应用程序市场中的一个功能 如应用程序 我必须在安装它之前对其进行签名 我想我必须在手机上运行 JarSigner JarSigner 存在于以下两个项目中 OpenJDK 和 GCC libjav
  • 使 clos 对象可在 lisp 中打印

    如果你想让 common lisp 中的 CLOS 对象可打印 可读打印 你如何在不使用除了 print 和 read 之外的任何东西的情况下做到这一点 至少在我的解决方案中 有两个部分可以做到这一点 但是您将需要这个功能 感谢 cl pr
  • Django API 请求

    我正在尝试使用模型的字段作为 API 请求中的关键字来访问另一个服务的 API URL 会像这样 http api example com json first name FNAME last name LNAME key key 这是我的
  • 使用 sed 计算反向引用

    我想 pm max children 5 成为 pm max children 8 并一直在努力但没有得到解决方案 像 1 或 2 这样的反向引用似乎不适用于计算 sed i E s pm max children s 1 echo 2 3
  • 这个字符串是什么:Ôªø 以及如何避免它?

    不知何故 我设法将 放在 PHP 脚本的顶部 那群可怕的人物到底是什么 我怎么可能进入其中 以及将来我该如何揭露和 或避免它 有趣的是 它之前潜伏在 PHP 脚本的最顶部
  • Cloudera 5.1下作业在LocalJobRunner中保持运行

    需要一些快速帮助 我们的作业在 MapR 下运行良好 但是当我们在 Cloudera 5 1 上启动相同的作业时 它继续以本地模式运行 我确信这是某种配置问题 它是哪个配置设置 14 08 22 12 16 58 INFO mapreduc
  • C# 通用字典 TryGetValue 找不到键

    我有这个简单的例子 using System using System Collections Generic namespace ConsoleApplication1 class Program static void Main str
  • 如何避免一遍又一遍地创建同一个对象?

    如前所述 我继续研究我的 TCP 套接字今天早上 https stackoverflow com questions 74812333 我发现每次我通过这样的套接字发送消息时 套接字的数量都会不断增加 这仅仅意味着我不断创建和打开新的套接字
  • 将最后一个值添加到数据框的顶部。

    最后一个值是 12 我想将最后一个值移到第一个值 这意味着我想附加数据框并将最后一个值移动到数据框的顶部 同时向下移动较早的值 请检查预期的输出 R code a lt c 11 243 121 42 12 a lt data frame
  • Linux - 修改文件修改/访问/更改时间[关闭]

    Closed 这个问题不符合堆栈溢出指南 help closed questions 目前不接受答案 我读了一篇关于新隐写方法的文章 时间在我这边 文件系统元数据中的隐写术 http www sciencedirect com scienc
  • 从 mysqldb 查询获取原始十进制值

    我正在使用 MySQLdb 包在 python 中执行 mysql 查询 代码看起来像这样 c db cursor c execute select from table output for row in c output append
  • Pandas - 查找没有 Nan 值的最长拉伸

    我有一个 pandas 数据框 df 其示例如下 time x 0 1 1 1 2 Nan 2 3 3 3 4 Nan 4 5 8 5 6 7 6 7 5 7 8 Nan 真正的框架要大得多 我试图找到 x 系列中最长的非 NaN 值 并打
  • 将可排序表与复选框总和输出相结合

    我当前的代码有一个复选框求和函数 它从数据库中获取值 我还尝试将另一个 Javascript 代码应用到我的表中 以允许我对行中的数据进行排序 例如 对行进行排序后 将所有 5 个 ACER 资产显示在顶部 我想勾选前 5 个显示的 ACE