zoukankan      html  css  js  c++  java
  • jquery通过AJAX从后台获取信息并显示在表格上的类

    前一阵我写了:《jquery通过AJAX从后台获取信息并显示在表格上,并支持行选中。》现在,我把他们处理了一下,不需要每次写代码了:

    具体代码如下:

    //获取数据并显示数据表格
    function GetTableData(tableId,ChlickEvent) {
        var table = $(tableId);
        var url=table.data('url');
        $.ajax({
            url: url,
            type: 'post',
            dataType: 'json',
        })
        .done(function (json) {
            var fileds = new Array();
            table.children('thead').children('tr').children('th').each(function (index, el) {
                var field = $(this).data('field');
                fileds[index] = field;
            });
            var tbody = '';
            $.each(json, function (index, el) {
                var tr = "<tr>";
                $.each(fileds, function (i, el) {
                    if (fileds[i]) {
                        tr += '<td>' + formatJsonData(json[index][fileds[i]]) + '</td>';
                    }
                });
                tr += "</tr>";
                tbody += tr;
            });
            table.children('tbody').append(tbody);
            if (ChlickEvent) {//如果需要支持行选中事件
                table.children('tbody').addClass('sel');
                table.children('tbody.sel').children('tr').click(function (event) {
                    $(this).siblings('tr').removeClass('active');//删除其他行的选中效果
                    $(this).addClass('active');//增加选中效果
                    ChlickEvent($(this).children('td:eq(0)').text());//新增点击事件
                });
            }
        }).fail(function () {
            alert("Err");
        });
    }
    //格式化JSON数据,比如日期
    function formatJsonData(jsondata){
        if(jsondata==null){
            return '无数据';
        }
        else if(//Date(d+)/.exec(jsondata)){
            var date = new Date(parseInt(jsondata.replace("/Date(", "").replace(")/", ""), 10));
            return date.toLocaleString();
        }
        return jsondata;
    }

    写的非常简单,功能也很少,但是我自己用暂时足够了。满足简单需求。

    HTML代码必须以下格式,必须以POST方式获取JSON数据,获取地址写到data-url里,数据列名写到data-field里。

    支持点击事件。


    用法:

    <table id="RoleGroupTable" class="table" data-url="@Url.Action("GetRoleGroups", "Account")">
      <thead>
        <tr>
          <th data-field="ID">ID</th>
          <th data-field="Name">名称</th>
          <th data-field="Remark">简介</th>
        </tr>
      </thead>
      <tbody></tbody>
    </table>
    <script>
      jQuery(document).ready(function ($) {
        GetTableData('#RoleGroupTable', function (id) {
          alert(id)
        });
      });
    </script>
  • 相关阅读:
    (转载)SAPI 包含sphelper.h编译错误解决方案
    C++11标准的智能指针、野指针、内存泄露的理解(日后还会补充,先浅谈自己的理解)
    504. Base 7(LeetCode)
    242. Valid Anagram(LeetCode)
    169. Majority Element(LeetCode)
    100. Same Tree(LeetCode)
    171. Excel Sheet Column Number(LeetCode)
    168. Excel Sheet Column Title(LeetCode)
    122.Best Time to Buy and Sell Stock II(LeetCode)
    404. Sum of Left Leaves(LeetCode)
  • 原文地址:https://www.cnblogs.com/ANPY/p/4825597.html
Copyright © 2011-2022 走看看