Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The element doesn't want to be selected #6186

Closed
ghost opened this issue Feb 22, 2023 · 1 comment
Closed

The element doesn't want to be selected #6186

ghost opened this issue Feb 22, 2023 · 1 comment
Labels
label/id and id stack implicit identifiers, pushid(), id stack

Comments

@ghost
Copy link

ghost commented Feb 22, 2023

Hello.
I am trying to select item from table but it's not selecting. When i change the text, all works.
https://user-images.githubusercontent.com/126063817/220577306-297fc741-b451-45b8-87f9-d48530200867.mp4
Here is a code:

bool openned = false;

struct n {
	string first;
	string second;
};

vector<n> items;
int current_index = 0;

void gui::Render() noexcept
{
	ImGui::ShowDemoWindow();
	if (!openned) {
		ImGui::SetNextWindowSize(ImVec2(800, 600));
		ImGui::SetNextWindowPos(ImVec2(10, 10));
		for (int i = 0; i < 5; i++) {
			n newdata = {"blah", "blah"};
			items.push_back(newdata);
		}
		openned = true;
	}

	ImGui::Begin("bug?");

	char first[128];
	strcpy_s(first, items[current_index].first.c_str());
	if (ImGui::InputText("First Name", first, IM_ARRAYSIZE(first))) {
		items[current_index].first = (string)first;
	}

	char second[128];
	strcpy_s(second, items[current_index].second.c_str());
	if (ImGui::InputText("Last Name", second, IM_ARRAYSIZE(second))) {
		items[current_index].second = (string)second;
	}

	if (ImGui::BeginTable("##table", 2, ImGuiTableFlags_SizingFixedFit | ImGuiTableFlags_Borders)) {
		ImGui::TableNextColumn(); ImGui::TableHeader("First Name");
		ImGui::TableNextColumn(); ImGui::TableHeader("Last Name");
		for (size_t i = 0; i < items.size(); i++) {
			ImGui::TableNextRow();
			ImGui::TableNextColumn();
			bool is_selected = (current_index == i);
			if (ImGui::Selectable(items[i].first.c_str(), is_selected, ImGuiSelectableFlags_SpanAllColumns)) {                            
				current_index = i;
			}   	
			ImGui::TableNextColumn();	
			ImGui::Text(items[i].second.c_str());
		}
		ImGui::EndTable();
	}

	ImGui::End();
}

Is this a bug or did I make an error in the code? Thank you

@ocornut ocornut added the label/id and id stack implicit identifiers, pushid(), id stack label Feb 22, 2023
@ocornut
Copy link
Owner

ocornut commented Feb 22, 2023

Hello,

You have an ID conflict. Read about our ID system in the FAQ:
https://github.com/ocornut/imgui/blob/master/docs/FAQ.md#q-about-the-id-stack-system

Most of the times in a loop submitting items you'd want to surround your item submission with PushID(index); ... PopID();.
This is also demonstrated in the various tables demo we have in imgui_demo.cpp.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
label/id and id stack implicit identifiers, pushid(), id stack
Projects
None yet
Development

No branches or pull requests

1 participant