Jump to content

SEARCH FOR VEHICLES FROM THE GRIDLIST


ÆBKV

Recommended Posts

-- CLIENT --

vehicles = {{400},{401},{402},{403},{404},{405},{406},{407},{408},{409},{410},
{411},{412},{413},{414},{415},{416},{417},{418},{419},{420},{421},{422},{423},
{424},{425},{426},{427},{428},{429},{430},{431},{432},{433},{434},{435},{436},
{437},{438},{439},{440},{441},{442},{443},{444},{445},{446},{447},{448},{449},
{450},{451},{452},{453},{454},{455},{456},{457},{458},{459},{460},{461},{462},
{463},{464},{465},{466},{467},{468},{469},{470},{471},{472},{473},{474},{475},
{476},{477},{478},{479},{480},{481},{482},{483},{484},{485},{486},{487},{488},
{489},{490},{491},{492},{493},{494},{495},{496},{497},{498},{499},{500},{501},
{502},{503},{504},{505},{506},{507},{508},{509},{510},{511},{512},{513},{514},
{515},{516},{517},{518},{519},{520},{521},{522},{523},{524},{525},{526},{527},
{528},{529},{530},{531},{532},{533},{534},{535},{536},{537},{538},{539},{540},
{541},{542},{543},{544},{545},{546},{547},{548},{549},{550},{551},{552},{553},
{554},{555},{556},{557},{558},{559},{560},{561},{562},{563},{564},{565},{566},
{567},{568},{569},{570},{571},{572},{573},{574},{575},{576},{577},{578},{579},
{580},{581},{582},{583},{584},{585},{586},{587},{588},{589},{590},{591},{592},
{593},{594},{595},{596},{597},{598},{599},{600},{601},{602},{603},{604},{605},
{606},{607},{608},{609},{610},{611}}

local screenW,screenH = guiGetScreenSize()

window = guiCreateWindow((screenW-600)/2,(screenH-500)/2,600,500,"Panel",false)
tabpanel = guiCreateTabPanel(10,30,580,460,false,window)
v_tab = guiCreateTab("Vehicles",tabpanel)
v_gridlist = guiCreateGridList(10,45,560,341,false,v_tab)
v_editbox = guiCreateEdit(10,10,150,30,"",false,v_tab)
v_button1 = guiCreateButton(420,396,150,30,"Create",false,v_tab)
v_button2 = guiCreateButton(10,396,150,30,"Close",false,v_tab)
v_button3 = guiCreateButton(170,10,80,30,"Search",false,v_tab)

guiSetVisible(window,false)
guiWindowSetSizable(window,false)
guiGridListAddColumn(v_gridlist,"Vehicle",1.0)
guiSetProperty(v_gridlist,"ColumnsSizable","False")
guiSetProperty(v_gridlist,"SortSettingEnabled","False")

for _,vehicle in ipairs(vehicles) do
  local vehicleName = getVehicleNameFromModel(vehicle[1])
  local v_row = guiGridListAddRow(v_gridlist)
  guiGridListSetItemText(v_gridlist,v_row,1,vehicleName,false,false)
end

addEventHandler("onClientGUIClick",root,
function()
  if source == v_button1 and guiGridListGetSelectedItem(v_gridlist) >= 0 then
    local getVehicle = guiGridListGetItemText(v_gridlist,guiGridListGetSelectedItem(v_gridlist),1)
    local vehicleID =  getVehicleModelFromName(getVehicle)
    triggerServerEvent("createVehicle",localPlayer,vehicleID)
  elseif source == v_button2 then
    guiSetVisible(window,false)
    showCursor(false)
  end
end
)

bindKey("F2","down",
function()
  if guiGetVisible(window) == false then
    guiSetVisible(window,true)
    showCursor(true)
  else
    guiSetVisible(window,false)
    showCursor(false)
  end
end
)
-- SERVER --

addEvent("createVehicle",true)
addEventHandler("createVehicle",root,
function(vehicleID)
  local x,y,z = getElementPosition(source)
  local rx,ry,rz = getElementRotation(source)
  local vehicle = createVehicle(vehicleID,x,y,z,rx,ry,rz)
  warpPedIntoVehicle(source,vehicle)
end
)

Hello guys, how can I search for vehicles from the gridlist?

I have created an editbox for the vehicle name (v_editbox) and a button to search for the vehicle (v_button3).

So if I search for, for example, "Infernus" and press the search button is only Infernus in the gridlist.

Can someone help me please?

Edited by ÆBKV
Link to comment

This works fine, if you want it to be updated when writing something in the editbox, use onClientGUIChanged

addEventHandler("onClientGUIClick",root,
function()
   if source == v_button1 and guiGridListGetSelectedItem(v_gridlist) >= 0 then
      local getVehicle = guiGridListGetItemText(v_gridlist,guiGridListGetSelectedItem(v_gridlist),1)
     	local vehicleID =  getVehicleModelFromName(getVehicle)
     	triggerServerEvent("createVehicle",localPlayer,vehicleID)
   elseif source == v_button2 then
     	guiSetVisible(window,false)
     	showCursor(false)
   elseif source == v_button3 then 
   		local text = guiGetText(v_editbox) 
	    guiGridListClear(v_gridlist) 
	    for i, v in ipairs( vehicles ) do 
		    if getVehicleNameFromModel(v[1]):find(text) then 
		        local row = guiGridListAddRow(v_gridlist) 
		        guiGridListSetItemText (v_gridlist,row,1,getVehicleNameFromModel(v[1]),false,false) 
		    end 
		end
   end
end
)

 

Link to comment
11 hours ago, ÆBKV said:

it does not work so well with small and capital letters, do you have an idea why?

You can make both the input text and the thing you're searching for (the result to be found) in small letters by using this: http://www.lua.org/manual/5.1/manual.html#pdf-string.lower

That way it won't matter if you type "Infernus", "infernus", "INFERNUS", "iNfeRnuS" and so on. It will still work and show you the vehicle. 

Edited by koragg
Link to comment
1 hour ago, ÆBKV said:

string.find(string.upper(getVehicleNameFromModel(v[1])),string.upper(guiGetText(v_editbox)))

But why is it the same with string.upper?

Well it won't matter which one you use to be honest. Important thing is that both the vehicle name and the inputted text are the same case. Lowercase or uppercase, your choice.

Link to comment

Last question, Is it possible to search the first letter of the vehicle?

For example, if I search for "A" then all vehicles with the first letter "A" are in the gridlist.

Right now it's like this if I search for "A" then there are L(a)ndstalker, Br(a)vura etc. in the gridlist

 

Edited by ÆBKV
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...