pageLength dropdown and search box do not display
pageLength dropdown and search box do not display
jdadwilson
Posts: 142Questions: 31Answers: 1
I have two tables which are basically structured alike. One displays the pageLength dropdown and search box but one does not.
This config code works...
const BRANDS_CONFIG = {
fadeInDuration: CONFIG.web_site.fadeInDuration || 2000,
ajaxUrl: "assets/ajax/ajax-brands.php",
pageLength: 14,
select: false,
layout: { topStart: { pageLength: { menu: [10, 20, 30, 50] } },
topEnd: { search: { placeholder: 'Search' } },
},
columnIndex: {
name: 0,
regNum: 1,
page: 2,
remark: 3,
id: 4,
},
};
The dataTables implementation...
oTable = $("#mainTable").DataTable({
ajax: BRANDS_CONFIG.ajaxUrl + params,
columns: getTableColumns(),
layout: BRANDS_CONFIG.layout,
pageLength: BRANDS_CONFIG.pageLength,
select: BRANDS_CONFIG.select,
search: { search: searchParam },
stateSave: searchParam ? false : undefined,
initComplete: function () {
hideAlphaLetters(this.api());
},
});
This config code for the that doesn't work...
const MEMORIALS_CONFIG = {
fadeInDuration: CONFIG.web_site.fadeInDuration || 2000,
ajaxUrl: "assets/ajax/ajax-memorials.php",
pageLength: 10,
lengthMenu: [10, 20, 30, 50],
columnIndex: {
name: 0,
birthDate: 1,
deathDate: 2,
available: 3,
cemetery: 4,
id: 5,
},
};
And the implementation that doesn't work...
oTable = $("#mainTable").DataTable({
ajax: ajaxUrl,
columns: getTableColumns(),
layout: {
topStart: 'pageLength',
topEnd: 'search',
top2: 'alphabetSearch',
top1: 'paging',
bottom: 'info',
},
pageLength: MEMORIALS_CONFIG.pageLength,
lengthMenu: MEMORIALS_CONFIG.lengthMenu,
lengthChange: true,
searching: true,
ordering: false,
stateSave: false,
stateSaveParams: function (settings, data) {
data.selected = $("span.active").index();
},
initComplete: function () {
handleTableInitComplete(this.api());
},
});
// Attach event handlers after table initialization
attachTableEventHandlers();
}
I also use a global dataTables extend as follows...
$.extend($.fn.dataTable.defaults, {
layout: {
top2: "alphabetSearch",
top1: "paging",
bottom: "info",
},
paging: {
type: "full_numbers",
},
language: {
paginate: {
first: "First",
previous: "Prev",
next: "Next",
last: "Last",
},
lengthMenu: "Display _MENU_ records",
},
ordering: true,
order: [],
pageLength: 12,
seaching: false,
select: { style: "single", info: false },
stateSave: true,
visible: true,
initComplete: function () {
hideAlphaLetters(this.api());
},
drawCallback: function () {
hidePagingAreas(this.api());
},
});
Here are links to the two modules...
https://txdelta.org/brands - this displays the pageLength and search box
https://txdelta.org/memorials/033/pecan-gap - this does not display the pageLength and search box
I spent over 45 minutes on a chat with Claude Sonnet 4.5 via vsCode and could not get a resolution. Hope somebody here might have an answer.
TIA for any assistance
James A Wilson
Replies
Hi,
When I load the
pecan-gappage to take a look and see if I can debug it, I get an alert with:It would appear that is because an Ajax request is being rejected with
Reason: CORS Multiple Origin Not Allowed.I assume that is not the error that you were expecting on the page?
Also, be careful with
$.extend($.fn.dataTable.defaults, {- that will not extent deep into objects. i.e. the new defaultlanguageobject is only what you've provided - it doesn't include any of the other default values, e.g. for sorting etc. You should probably use$.extend(true, $.fn.dataTable.defaults, {to make it a deep extend.If you could let me know about the first point, I'll take a further look and see what help I can offer.
Allan
I looked at your second test case and see this when inspecting the page:
The paging, page length and search elements are in the document but each have
style="display: none;"applied to theirdivtags. I'm not sure without being able to experiment with the code but I would look at these items:style="display: none;"from these elementsstylesection. Probably wouldn't affect thestyle="display: none;".Doesn't look like
vendor-datatables-2.0.8.min.cssis concatenated with the extensions you are loading with this:Which is loading these extensions:
This may or may not fix your issues but I would recommend loading the Bootstrap 3 style integration files. Go to the link in the datatables-2.0.8.js file and update it to include BS 3. Use the generated files for both the CSS and JS includes.
Possibly you can comment out the
ajaxoption to remove the error to load an empty table so we can help debug further if the above suggestions don't help.Kevin
My bad... ajax file was missing the cors headers. Fixed. Try it again.
James A Wilson
I cleared my browser's cache and still get that error. Looks like we cross posted, please see my above comments.
Kevin
Kevin, I replaced the datatables-2.0.8 files with the newly regenerated datattables files as you suggested. This is a screenshot of the results.
You can see that the paging is not as previously displayed. Also note that the pageLength dropdown and search box do not display.
Any further suggestions?
James A Wilson
Still getting the
alert()on load, but dug into the code a bit and:In
_allFunctionsJavas.jsis being called. You don't have an override for that inmainTable's initialisation, therefore it is executing and hidding the search and paging elements. The function comment says:But it does more than that. It is hiding the containers for the search controls as well. You could try changing:
to:
which would make it operate as the comment says.
Allan
Allan, Thanks for your input. I adjusted the const pagingElements in the hidepagingAreas function. I added the dt-start, dt-length, dt-end, and dt-search items.
The length and search items now display but the two items appear on separate lines. I noted in the inspect of the elements that the dt-start has display: none but is lined through and then has an element-style of display: block. This causes the dt-start to extend the width of the table. Unchecking the element-style causes the two items to appear on the same line as they should.
Any thoughts on how the element-style display: block is getting added to the element.
BTW: are you still getting the CORS error? This should be corrected.
TIA for your assistance.
James A Wilson
Reference the CORS issue. I found a conflicting statement in the htaccess file. Hope this fixed the issue. Let me know.
James A Wilson
Yes, still getting the CORS error I'm afraid:
The
pagingElementsarray appears to have been expanded:As an experiment, add
drawCallback: () => {}to your initialisation object for#mainTable. That will stop thehidePagingAreasfunction from running.I'd actually suggest that you use
layoutto control if table control items should be shown or not, rather than attempting to show / hide them using a function such ashidePagingAreas.Allan
Allan,
The use of the hidePagingAreas function is a UI feature. This project has 20+ dataTables. A few have over 1000 records, most of the others in the 100+ range.
On table initialization all records are loaded and the alphabet, paging, length, and search elements display. But, if a user selects a alphabet letter to filter the records and the recordsDisplay is less than (or equal to) the length, then elements like paging, length and search are functionally not needed, thus not displayed.
I commented out the call to the hidePagingAreas function which is included in the drawCallback: in the dataTables global extend. Yes, the paging items displayed, but all paging element are non-functional, which is the purpose of the hidePagingAreas function.
You mentioned using the layout: options? I'll admit, I am a long time user of dataTables and absolutely love the product.. Across the 10+ projects that I've written I probably have 100+ instances of a dataTable. But, because they all work as desired I haven't needed to update some of the features. Can what I desire with the hidePagingAreas function be accomplished via the layout alone? If so, let me know how and I'll make it happen.
James A Wilson
Allan,
I had Claude Sonnet 4.5 do some investigating into the CORS issue. It changed some header code I was using. Let me know if you still get an error.
James A Wilson
Still getting the Ajax error.
The
layoutconfig will only be applied during initialization. Sounds like you will still need to use the hidePagingAreas function to dynamically hide the desired elements based on the number of rows displayed.Your page is rather difficult to debug without making changes so I copied what I think are the pertinent parts into this test case:
https://live.datatables.net/yuwebodu/1/edit
Assuming you want to remove the info and paging elements the code change Allan recommended is in the test case:
Does the test case behave as per your requirements?
Maybe you can update this test case to replicate the issue you are having.
Kevin
Just for fun I updated the test case to use
ajaxto make sure something unexpected didn't happen withdrawCallbackdue to the asyncajaxrequesthttps://live.datatables.net/daqofuza/1/edit
The behavior is still the same.
Again let us know if you expect something different.
Kevin
I adjusted the following...
This worked as desired. I now need to make the other adjustments to the js code. I'll let you know how that goes.
Note that I removed the dt-info (so it will display) as it is something that is informational and has no functional characteristic.
Thanks for your support. Great as always.
James A Wilson
I re-read your comments and it sounds like the issue now is with the length menu and search are on two lines. This will be the case on a small screen but, at least in my test case, they appear on the same like with a full page. For example:
I may be loading an old version of your page but I don't see where you updated to use the Datatables BS 3 style integration. If you haven't updated that possibly could be the problem.
Kevin
Allan & Kevin,
I spoke with my technical guru regarding the CORS issue. His questions were...
I'll look into adding some header into if you are outside the US.
James A Wilson
Here is a screenshot of the errors I get from the first link you posted:
I'm using Chrome on a Mac and live in the US. Allan is in the UK - not sure of the browser.
Possibly for you can remove the ajax request and load the table via Javascript, ie
data, or create an HTML table. Might be easier than trying to debug the CORS issue as the problem is not specific to the data nor data source.Kevin
Ah - I just spotted the error in the error messages that Kevin posted.
www.txdelta.orgis not the same astxdelta.org. Thewww.is usually aliased to the main domain, but it doesn't have to be. And in this case the CORS policy is only setup to work withwww.txdelta.org. It needs to be configured to work with both.If I load https://www.txdelta.org/memorials/033/pecan-gap then the data will load. That's not a location or browser specific issue.
Allan
I'll see about fixing that.
Thanks,
James A Wilson
I updated the htaccess file to allow both versions. Let me know if this worked.
James A Wilson
More on the display issue. I added comments to the hidePagingAreas function. Noticed that on page initialization the function is called 3 times.
Console.log
Is this normal?
James A Wilson
No. Here are the new errors:
However, as Allan mentioned, using this does work:
The length menu and search input are on separate lines as you mentioned. It looks like you are still using the Datatables default styling:
The test case I created uses the Datatables BS 3 style integration libraries. Note that
layoutuses the BS row and column classes to arrange the elements:You can see the length change and search each use
col-6-smto place thetopStartandtopEndproperly in therowdefined in the enclosingdiv.This is why I suggested using the Datatables BS 3 style integration and suggested it might fix the issue with the length change and search being on separate lines.
Kevin
It's common for
drawCallbackto be called multiple times during initialization. The number of times will be determined by the config. I updated my ajax test case to show an example - look at the console:https://live.datatables.net/daqofuza/3/edit
Kevin
Allan & Kevin;
Gentlemen, I think the dataTable issue can be laid to rest. It now functions as desired. Let me know about the CORS issue.
Is there a way that I might provide you a token of my appreciation with a donation to your favorite beverage fund? I greatly appreciate all the help you have provided recently and over the years.
If you prefer, send me a private email with a connection to your favorite beverage fund.
James A Wilson.
Glad you got it working! I would be happy for you to donate to Allan's project at the supporters page.
Kevin
Glad to do so!
Many thanks fellas - much appreciated
.
Great to hear we are able to get to the bottom of the issue.
Allan