简体   繁体   English

如何在按特定列排序的 javascript 表中重新呈现数据

[英]How to rerender data in a javascript table orderd by a specific column

By javascript I generate rows for Agents in a table.通过javascript,我在表中为代理生成行。 every row represent an agent.每行代表一个代理。 after this I receive live data to update the columns.在此之后,我收到实时数据以更新列。 I have a column called (Calls) and i need to order the agents by calls (live update depending on received data) descending.我有一个名为 (Calls) 的列,我需要通过调用(根据收到的数据进行实时更新)降序对代理进行排序。 example例子

agents ----- calls
Sam ---------13
Al  ---------12
Sara---------8

if Sara got the most data by time then she'll be the first.如果 Sara 按时间获得的数据最多,那么她将是第一个。

agents -------calls
Sara----------15
Sam ----------13
Al------------12

and so on ..等等 ..

this is my row rendering这是我的行渲染

var $agentRow = '<tr id="agentRow_' + agentId + '"><th scope="row">' + agentName + '</th><td class="calls" id="agentCalls_' + agentId + '">' + outTotalCalls +
            '</td><td class="minutes" id="agentMinutes_' + agentId + '">' +
            outCallMinutes + '</td>' +
            '<td class="averages" id="agentAverage_' + agentId + '">' + averageOutCallTime + '</td></tr>';

    //if $agentRow exists invoke setIncomingValuesToAgentsFields else append it to the table
    if ($('#agentRow_' + agentId).length) {
        setIncomingValuesToAgentsFields('#agentCalls_' + agentId, outTotalCalls);
        setIncomingValuesToAgentsFields('#agentMinutes_' + agentId, outCallMinutes);
        setIncomingValuesToAgentsFields('#agentAverage_' + agentId, averageOutCallTime);
    } else {
        $('#agentsTable').append($agentRow);
    }


function setIncomingValuesToAgentsFields(elementId, inComingValue) {
    var currentElementValue = 0;
    if ($(elementId).text() !== "") {
        currentElementValue = $(elementId).text();
        currentElementValue = parseFloat(currentElementValue);
        currentElementValue += inComingValue;
        $(elementId).text(currentElementValue);
    } else {
        $(elementId).text(currentElementValue);
    }
}

Hope you will be getting data from server using Ajax call .希望您将使用 Ajax 调用从服务器获取数据。 So If your having the result data in JSON object , then you can sort the data to find out which is having highest value .因此,如果您在 JSON 对象中有结果数据,那么您可以对数据进行排序以找出具有最高值的数据。 The follwing functon will help us to sort the data下面的函数将帮助我们对数据进行排序

sortTable:function (property,asc)
{
    sampleTableObject = sampleTableObject.sort(function(a, b) {
        if (asc) return (a[property] > b[property]) ? 1 : ((a[property] < b[property]) ? -1 : 0);
        else return (b[property] > a[property]) ? 1 : ((b[property] < a[property]) ? -1 : 0);
    });     
}

property is the json object property (here it should be calls) based on which you need to sort .属性是 json 对象属性(这里应该是调用),您需要根据它对 .

Pass false to ' asc ' to sort in descending order.将 false 传递给 ' asc ' 以降序排序。

Assign sampleTableObject with the result json object and call sortTable() .使用结果 json 对象分配sampleTableObject并调用 sortTable() 。 Then use the sorted object to build the table.然后使用排序后的对象来建表。

See the live sample of what you need.查看您需要的实时样本。 After 3 second Al calls become 14, and table rows will be sorted again. 3 秒后 Al 调用变为 14,表行将再次排序。

 var agents = [ { name: 'Sara', calls : 15 }, { name: 'Sam', calls : 13 }, { name: 'Al', calls : 12 } ]; function to_row(obj){ var tr = $('<tr></tr>'); tr.data('obj', obj); $('<td>'+obj.name+'</td>').appendTo(tr); $('<td>'+obj.calls+'</td>').appendTo(tr); return tr; } function table_update(obj){ $('#table tr').each(function(){ var t=$(this); var o=t.data('obj'); if(o.name==obj.name){ t.remove(); }; if(o.calls>obj.calls){ to_row(obj).insertAfter(t); } return t.data('obj'); }) } agents.sort(function(x,y){ return y.calls - x.calls; }).forEach(function(o){ to_row(o).appendTo( $('#table') ); }); setTimeout(function(){ table_update( { name: 'Al', calls : 14 } ); }, 3000);
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table id="table"> </table>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM