Jump to content

Adding a teams to a gridlist


Gravestone

Recommended Posts

I have created a gridlist which should display all the available team names but the problem is that the code only gets the team name of the first team tho it adds the correct number of rows to the column. e.g If there are 4 teams, Ballas, Grove, Aztecs and Vagos, it writes Ballas in all the four lines also it displays in the chatbox only Ballas.

rankinggrid = guiCreateGridList(24, 58, 439, 240, false, Window) 

Edited by Guest
Link to comment

rankinggrid = guiCreateGridList(24, 58, 439, 240, false, Window) 
guiGridListAddColumn(rankinggrid, "Team name", 0.2) 
    for key,value in ipairs ( getElementsByType("team") ) do 
        guiGridListAddRow(rankinggrid, getTeamName(value)) 
        outputChatBox(getTeamName(value)) -- Added this for testing 
    end 
Link to comment
for _,v in ipairs (getElementsByType ("team")) do 
    guiGridListAddRow (rankinggrid, getTeamName (v)) 
end 
  

Replace all your lines above with these ones, should work. So you literally don't need to 'unpack' the table when loops like this above exist.

EDIT: I had the page open for like 30 minutes than I replied, didn't see your comment (the guy above) x)

Edited by Guest
Link to comment
for _,v in ipairs (getElementsByType ("team")) do 
    guiGridListAddRow (rankinggrid, getTeamName (v)) 
end 
  

Replace all your lines above with these ones, should work. So you literally don't need to 'unpack' the table when loops like this above exist.

what's the difference between your code and my code !

Link to comment

Thanks, another thing. I am adding the team name with it's kills to a gridlist. I want to sort it according to the kills, how do I use this table.sort function in combination with this code?

local allteams = getElementsByType("team") 
  
local function sort (a,b) 
    return (getElementData(a,"Kills") or 0) > (getElementData(b,"Kills") or 0) 
end 
  
for key,value in ipairs (allteams) do    
    local row = guiGridListAddRow(rankinggrid, getTeamName(value)) 
    setTimer( function() 
    local r, g, b = getTeamColor(value) 
    local kills = getElementData(value, "Kills") 
        guiGridListSetItemColor(rankinggrid, row, 1, r, g, b) 
        guiGridListSetItemText(rankinggrid, row, 2, tonumber(kills), false, true) 
        guiGridListSetItemColor(rankinggrid, row, 2, 0, 255, 0) 
    end, 1000, 0)    
end 

And there's a problem with the above code, the teams keep moving if I click on the column name to sort.

YOUYyYN.png

Link to comment
Thanks, another thing. I am adding the team name with it's kills to a gridlist. I want to sort it according to the kills, how do I use this table.sort function in combination with this code?
local allteams = getElementsByType("team") 
  
local function sort (a,b) 
    return (getElementData(a,"Kills") or 0) > (getElementData(b,"Kills") or 0) 
end 
  
for key,value in ipairs (allteams) do    
    local row = guiGridListAddRow(rankinggrid, getTeamName(value)) 
    setTimer( function() 
    local r, g, b = getTeamColor(value) 
    local kills = getElementData(value, "Kills") 
        guiGridListSetItemColor(rankinggrid, row, 1, r, g, b) 
        guiGridListSetItemText(rankinggrid, row, 2, tonumber(kills), false, true) 
        guiGridListSetItemColor(rankinggrid, row, 2, 0, 255, 0) 
    end, 1000, 0)    
end 

And there's a problem with the above code, the teams keep moving if I click on the column name to sort.

YOUYyYN.png

for The first Problem .

How to create a list of highest to lowest

And about Teams Sorting.

guiGridListSetSortingEnabled 
Link to comment

table.sort only sorts number values so even if the kills are sorted, how do I sort the teams according to the kills?

Edit: Tried this and adds the values successfully according to the kills but it doesn't change the order in the gridlist every second if the data changes of a team. Restart is required.

kills = {} 
setTimer(function () 
    for _,team in ipairs (getElementsByType ("team")) do 
           -- local account = getPlayerAccount (player) 
        local teamkills = getElementData (team, "Kills") 
        if not table.contains(kills, teamkills) then 
            table.insert (kills, teamkills) 
        end 
    end  
  
  
  
function sortData() 
       setTimer(function() table.sort (percentages, 
            function (a, b) 
                return a > b 
            end 
        )end, 1000, 0) 
        
    for _,v in ipairs (kills) do 
        for _,team in ipairs (getElementsByType ("team")) do 
        local teamkills = getElementData (team, "Kills") 
            if v == teamkills then 
        --  outputChatBox("Team "..getTeamName(team).." has "..v) 
                if not isTextInGridList(rankinggrid, getTeamName(team)) then 
                local row = guiGridListAddRow(rankinggrid, getTeamName(team)) 
                setTimer( function() 
                local r, g, b = getTeamColor(team) 
                local kills = getElementData(team, "Kills") 
                        guiGridListSetItemColor(rankinggrid, row, 1, r, g, b) 
                        guiGridListSetItemText(rankinggrid, row, 2, tonumber(kills), false, true) 
                        guiGridListSetItemColor(rankinggrid, row, 2, 0, 255, 0) 
                     
                 
            end 
        end 
    end end 
, 1000, 0) 
end 
setTimer (sortData, 1000, 0) 

Edited by Guest
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...