From the reference libraryRedM

examples

jo_libs/modules/menu/autodoc/slots/examples.md

examples

Example

-- Create a menu
local menu = jo.menu.create("shopMenu", {
  title = "General Store",
  subtitle = "Available Items"
})

-- Add items using single parameter (adds to the end of the list)
menu:addItem({
  title = "Hunting Knife",
  description = "A sharp knife for skinning animals",
  prefix = "knife", -- Icon before the title
  price = { money = 12.50 },
  statistics = {
    { label = "Damage", type = "bar", value = { 7, 10 } },
    { label = "Durability", type = "bar", value = { 8, 10 } }
  },
  onClick = function(currentData)
    print("Purchasing Hunting Knife")
    -- Code to purchase the item would go here
  end
})

-- Add item at specific position (position 1 - at the beginning)
menu:addItem(1, {
  title = "Horse Brush",
  description = "Keep your horse clean and happy",
  price = { money = 5.75 },
  icon = "brush",     -- Left icon
  disabled = false,
  textRight = "New!", -- Text shown on the right
  onClick = function(currentData)
    print("Purchasing Horse Brush")
    -- Code to purchase the item would go here
  end
})

-- Add another item with sliders
menu:addItem({
  title = "Hunting Outfit",
  description = "Camouflage clothing for hunting",
  price = { money = 45.00, gold = 5 },
  sliders = {
    {
      title = "Colors",
      current = 1,
      values = { "Brown", "Green", "Black" }
    }
  },
  onChange = function(currentData)
    local selectedColor = currentData.item.sliders[1].value.label
    print("Selected color: " .. selectedColor)
  end
})

-- Add a child menu item (opens another menu when selected)
menu:addItem({
  title = "Weapons →",
  child = "weaponsMenu", -- This will open the "weaponsMenu" when activated
  icon = "revolver"
})

-- Send the menu to the NUI layer
menu:send()

-- Set this menu as the current menu and show it
jo.menu.setCurrentMenu("shopMenu")
jo.menu.show(true)

Example

local menu = jo.menu.create(id, data)
menu:addItem({ title = "Item 1" })
menu:send()
menu:addItem({ title = "Item 2" })
menu:refresh()

Example

menu:reset()

Example

local menu = jo.menu.create(id, data)
menu:send()

Example

-- Sort all menu items alphabetically by title
menu:sort()

-- Sort only items 2 through 5
menu:sort(2, 5)

Example

-- Update the title of the second item
menu:updateItem(2, "title", "New Title")

-- Disable the third item
menu:updateItem(3, "disabled", true)

Example

-- Set this menu as the current active menu
-- Keep navigation history and reset the cursor position
menu:use(true, true)

-- Set this menu as the current active menu
-- Don't keep navigation history
menu:use(false)

Example

-- Add a new item to a menu by its ID
local menuId = "mainMenu"
jo.menu.addItem(menuId, {
  title = "New Option",
  description = "This is a new menu option",
  onClick = function(currentData)
    print("New option clicked")
  end
})

-- Add a new item at a specific position (3rd position)
jo.menu.addItem(menuId, 3, {
  title = "Insert at position 3",
  description = "This item will be inserted at position 3"
})

Example

--The unique ID of the menu
local id = "UniqueId1"
--The menu data
local data = {
  title = "Menu",        --The big title of the menu
  subtitle = "Elements", --The subtitle of the menu
  numberOnScreen = 8,    --The number of item display before add a scroll
  onEnter = function(currentData)
    print("Enter in menu " .. id)
  end,
  onBack = function(currentData)
    print("Backspace/Esc pressed in menu " .. id)
  end,
  onExit = function(currentData)
    print("Exit menu " .. id)
  end,
}
local menu = jo.menu.create(id, data)

Example

local id = "UniqueId1"
jo.menu.delete(id)

Example

-- Check if the selected button has changed since last update
if jo.menu.doesActiveButtonChange() then
  print("Player selected a different button")
end

Example

-- Fire a custom event across all menu levels
jo.menu.fireAllLevelsEvent("onCustomEvent", "Parameter 1", 42, { data = "Extra data" })

Example

-- Get the current item and fire a custom event on it
local currentItem = jo.menu.getCurrentItem()
jo.menu.fireEvent(currentItem, "onClick")

-- Fire a custom event on a specific menu
local menu = jo.menu.get("mainMenu")
jo.menu.fireEvent(menu, "onExit")

Example

-- Force the menu to go back to the previous menu
jo.menu.forceBack()

Example

-- Get a menu instance by its ID
local menuId = "mainMenu"
local menu = jo.menu.get(menuId)

-- Now you can work with this menu instance
if menu then
  print("Menu title: " .. menu.title)
  print("Number of items: " .. #menu.items)
end

Example

-- Get data about the current menu state
local currentData = jo.menu.getCurrentData()
print("Current menu ID: " .. tostring(currentData.menu))
print("Current item index: " .. tostring(currentData.index))

Example

-- Get the currently selected menu item
local currentItem = jo.menu.getCurrentItem()

-- Access properties of the current item
if currentItem then
  print("Selected item: " .. currentItem.title)
  if currentItem.description then
    print("Description: " .. currentItem.description)
  end
end

Example

-- Get the currently active menu
local currentMenu = jo.menu.getCurrentMenu()

-- Access properties of the current menu
if currentMenu then
  print("Active menu title: " .. currentMenu.title)
  print("Number of items: " .. #currentMenu.items)
end

Example

-- Get data about the previous menu state
local previousData = jo.menu.getPreviousData()
print("Previous menu ID: " .. tostring(previousData.menu))
print("Previous item index: " .. tostring(previousData.index))

Example

local isOpen = jo.menu.isOpen()
print(isOpen)

Example

-- Register a callback function for menu change events
jo.menu.onChange(function(currentData)
  print("Menu changed to: " .. tostring(currentData.menu))
  print("Selected item: " .. tostring(currentData.item.title))

  -- You can handle different menus here
  if currentData.menu == "mainMenu" then
    -- Do something specific for the main menu
  elseif currentData.menu == "settingsMenu" then
    -- Do something specific for the settings menu
  end
end)

Example

-- Refresh a menu by its ID to update its display
local menuId = "mainMenu"
jo.menu.refresh(menuId)

Example

-- Reset a menu by its ID
-- This moves the cursor back to the first item
local menuId = "mainMenu"
jo.menu.reset(menuId)

Example

-- Send a menu to the NUI layer by its ID
local menuId = "mainMenu"
jo.menu.send(menuId)

-- Send without resetting cursor position
jo.menu.send(menuId, false)

Example

-- Create a new menu instance
local newMenu = {
  id = "customMenu",
  title = "Custom Menu",
  subtitle = "Created with jo.menu.set",
  items = {
    { title = "Option 1" },
    { title = "Option 2" }
  }
}

-- Set this as a menu by its ID
jo.menu.set("customMenu", newMenu)

Example

local id = "UniqueId1"
local keepHistoric = true
local resetMenu = true
jo.menu.setCurrentMenu(id, keepHistoric, resetMenu)

Example

local visible = true
local keepInput = true
local hideRadar = true
jo.menu.show(visible, keepInput, hideRadar)

Example

-- Sort all items in a menu alphabetically by title
local menuId = "mainMenu"
jo.menu.sort(menuId)

-- Sort only items 2 through 5
jo.menu.sort(menuId, 2, 5)

Example

-- Update the title of the second item in a menu
local menuId = "mainMenu"
jo.menu.updateItem(menuId, 2, "title", "New Title")

-- Disable the third item
jo.menu.updateItem(menuId, 3, "disabled", true)

Example

-- Update menu language text
jo.menu.updateLang({
  of = "%1 of %2",
  selection = "Selection",
  devise = "$",
  number = "Number %1",
  free = "Free",
  variation = "Variation"
})

Example

-- Set the volume level for menu sound effects
-- Volume range is 0.0 to 1.0
jo.menu.updateVolume(0.8)

Example

local menu = jo.menu.create(id, data)

menu:addItem({
  title = "Item 1",
})

menu:addItem({
  title = "Item 2",
})

menu:addItem({
  title = "Item 3",
})

menu:setCurrentIndex(2) -- set item 2 as active

Example

jo.menu.playAudio('coins')

Example

menu:addItem({
  title = "Title",
  onClick = function(currentData)
    jo.menu.softHide(function()
      local title = jo.input.native("Enter the new title", currentData.item.title, 100)
      if title then
        currentData.item.title = title
        menu:refresh()
      end
    end)
  end
})

Example

local menuId = "home"
local isExist = jo.menu.isExist(menuId)
-- Expected output: `true` if the menu was created before, else `false`

Example

jo.menu.missingMenuHandler('home', function()
  print('home menu is missing')
  local menu = jo.menu.create('home', {
    title = "home"
  })
  menu:addItem({title = "item"})
  menu:send()
  menu:use()
end)

Example

menu:deleteValue("key")

Example

-- Update the title of the 3rd item in a menu
menu:updateValue({"items",3, "title"}, "New Title")
-- Update the NUI
menu:push()

Example

-- Remove the 2nd slider from the 1st item
menu:removeItem({"items", 1, "sliders", 2})
-- Update the NUI
menu:push()

Example

-- Remove the 2nd slider of the item
item:deleteValue({"sliders", 2})
-- Update the NUI
menu:push()

Example

local parentMenu = item:getParentMenu()
print(parentMenu.title)

Example

-- Update the title of the 3rd slider of the item
item:updateValue({"sliders",3, "title"}, "New Title")
-- Update the NUI
menu:push()

Example

jo.menu.displayLoader()
Wait(3000)
jo.menu.hideLoader()

Example

local currentIndex = jo.menu.getCurrentIndex()
print(currentIndex)

Example

local currentMenuId = jo.menu.getCurrentMenuId()
print(currentMenuId)

Example

local menuId = "home"
local isCurrentMenu = jo.menu.isCurrentMenu(menuId)
print(isCurrentMenu)
-- Expected output: `true` if the "home" menu is active, else `false`

Example

local fireMenuEvents = false
local fireItemEvents = true
jo.menu.runRefreshEvents(fireMenuEvents, fireItemEvents)

Example

local price = 10
local formattedPrice = jo.menu.formatPrice(price)
log(formattedPrice) -- Expected output: `{money = 10}`

Example

local prices = {money = 10, operator = "and", gold = 20}
local formattedPrices = jo.menu.formatPrices(prices)
log(formattedPrices) -- Expected output: `{money = 10, gold = 20}`

Example

local price = 10
local isFree = jo.menu.isPriceFree(price)
print(isFree) -- Expected output: `false`
price = {money = 0, bank = 0}
isFree = jo.menu.isPriceFree(price)
print(isFree) -- Expected output: `true`
Back to documentation