how to filter grid records based on checkbox

filter

i have two check boxes male and female if i check male only male records should show same for female checkbox also,how can i achieve this.i have taken form controls for check boxes in design node

You need to modify the query of the data source used for the grid.
You can override executeQuery() method of the data source. As the first thing, get a reference to the corresponding QueryBuildDataSource object:

QueryBuildDataSource qbds = this.queryBuildDataSource();

Then you can use addRange() method to add a filter. For example:

QueryBuildRange genderRange = qbds.addRange(fieldNum(MyTable, Gender));

And set its value:

genderRange.value(queryValue(GenderMaleFemale::Female));

When you’re able to do these basic things, it’s time to implement your actual logic.
Because we don’t want to add more and more ranges every time, let’s remove existing ranges at the beginning:

qbds.clearRanges();

And than add ranges conditionally.

if (MyMaleCheckbox.checked())
{
    qbds.addRange(fieldNum(MyTable, Gender)).value(queryValue(GenderMaleFemale::Male));
}

if (MyFemaleCheckbox.checked())
{
    qbds.addRange(fieldNum(MyTable, Gender)).value(queryValue(GenderMaleFemale::Female));
}

Then you just need to execute the query when the value of a checkbox changes. Override modified() method and call executeQuery() of the data source:

myTable_ds.executeQuery();

By the way, no such logic is usually needed, because users can use standard features to filter the grid.

2 Likes

I try your code but, I need some help
Thanks for Guide.

image
added a executequery highlight in yellow.

and then add code:

public void executeQuery()
{
QueryBuildDataSource qbds = this.queryBuildDataSource();
QueryBuildRange genderRange = qbds.addRange(fieldNum(MyTable, FieldName));
genderRange.value(queryValue(GridNameadd));

if (WorkFlowActive.checked())

{
qbds.addRange(fieldNum(MyTable, FieldName)).value(queryValue(GridNameadd));
}

//super();

}

Is it okay? please check it and help me,

Thanks!

There are several issues:

  1. You removed super(), therefore the query won’t ever execute. If you try running your form, you’ll see that it doesn’t work.
  2. If WorkFlowActive is checked, you add the same range twice.
  3. Even if WorkFlowActive isn’t checked, you add one range (above the if condition).
  4. You keep adding ranges. If the query gets executed ten times, you’ll end up with 10-20 ranges (and wrong behaviour, most likely). Pay attention to the query used by the form to detect these bugs, if you miss them in code.

Let’s change your code to this:

public void executeQuery()
{
	QueryBuildDataSource qbds = this.queryBuildDataSource();

	// Remove existing range
	// Alternatively, we could find an existing range and set its value to unlimited
	qbds.clearRange(fieldNum(MyTable, FieldName));

	if (WorkFlowActive.checked())
	{
		QueryBuildRange genderRange = qbds.addRange(fieldNum(MyTable, FieldName));
		// Using GridNameAdd, although I have no idea what it means
		genderRange.value(queryValue(GridNameAdd));
		// Optionally, we can lock (or hide) the range
		genderRange.status(RangeStatus::Locked);
	}

	// Execute the modified query
	super();
}
1 Like

qbds.addRange(fieldNum(MyTable, Gender)).value(queryValue(GenderMaleFemale::Male));

sir you add this GenderMaleFemale please tell me what mean?

GenderMaleFemale this is table name??

X++ contains so-called Enums (enumerated types). When you want to a field that can have several distincts values (known at design-time), you can create (or use an existing) enum with these values. If you want a field that can contain either Male or Female, you need an enum with two elements (Male and Female). There already is such an enum: GenderMaleFemale. That’s why I use it. Of course, I don’t know which type you used for Gender field in your MyTable, because you didn’t tell us. If you used a different type than GenderMaleFemale, this part of my code isn’t applicable to you. But then maybe you should rather fix your table.

1 Like

okay understand Thank you

after complete executequery() method, then override modified() method and call executeQuery() of the data source??