Avatar of Adam Ehrenworth
Adam Ehrenworth
Flag for United States of America asked on

DataView Web Part - Datatables JQuery. Removing filtering/sorting from SharePoint

We moved to SharePoint Online and I am trying to use some code I put together to pull in a Dataview web part and then have the table search able using Datatables.js

I was having trouble figuring out how to turn off the filtering and the sorting that is native to the SharePoint webpart so I opted to hide that row and replace it was a new row that is generated by script.

However, it appears that when Datatables is staging the table to display it is ignoring this new row that was added programatically.

I had to find the table based on a custom attribute and then force an ID attribute and that is all working. I just can't get the new row to become part of the DataTable display.

This is the code.  Any suggestions?

$(document).ready(function() 
{

$('table[summary="2CIA4_Rules "]').attr('id','ruletable');
$('#ruletable > tbody').prepend('<tr><td>Rule</td><td>Owner</td><td>Audience</td><td>Curriculum</td><td>Target Area</td><td>Leader</td><td>Leader_WWID</td><td>MRC</td><td>Legal</td><td>JobFunction</td><td>SubFunction</td><td>Exclusions</td><td>Year 4 Notes</td><td>Comments</td><td>ChangeNotes</td></tr>');

$( ".ms-listviewtable" ).DataTable( {
		dom: 'Tf<"clear">rtip',
        "scrollY":        "600px",
		"scrollCollapse": true,
		"paging":         false
} );


var table = $( ".ms-listviewtable" ).DataTable();

//table.column(13).visible( false );
//table.column(14).visible( false );
 
} );

Open in new window

jQueryJavaScriptMicrosoft SharePoint

Avatar of undefined
Last Comment
zephyr_hex (Megan)

8/22/2022 - Mon
zephyr_hex (Megan)

My first guess is that this is a timing thing.  JavaScript does not wait for the prepend to complete before moving on to initializing the datatable.

One way to test is to put an alert('whatever') just after the prepend line.  That will force JS to wait until you click OK before initializing the datatable.

If the new row now appears in the datatable, the permanent solution would be to use a promise on the line that prepends the row.

I'm not sure if this is going down the right track so I'll stop here until you've had a chance to test the theory using an alert.
Adam Ehrenworth

ASKER
When the alert triggers I see the column headers, when I click OK datatables loads and the same behavior occurs and the create row disappears. I have some other CSS affecting the display of the original table header from SharePoint but that class should have nothing to do with the newly create row.

Any ideas? Do you need to see anything else from my code?

Regards,

Adam
zephyr_hex (Megan)

OK, that doesn't sounds like a timing issue then.  I'll play around will what you've posted so far and see if I can come up with any other ideas.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
zephyr_hex (Megan)

New theory.

You are attempting to init the datatable twice, which throws an error.  datatables only allows one init, unless the first table is destroyed before the second one is created.  In the code you posted above, line 7 and 15 both init the datatable.  You can consolidate the two lines by keeping line 7 but add var table = before it.

When I remove the second init on line 15, the added row shows up fine in the table.
Here is a Fiddle Demo.  Click the button to make the magic happen.
Adam Ehrenworth

ASKER
zephyr,

Thank you for looking into this further. However, my scenario is a little different as the row that I am appending is actually the header row. I am hiding the SharePoint header row because it has the filtering and sorting function that is competing with DataTables.

I am not sure if this is what is causing my problem because I removed the 2nd init and I am still not getting the new header to persist.


 $(document).ready(function() 
{

$('table[summary="2CIA4_Rules "]').attr('id','ruletable');
$('#ruletable > tbody').prepend('<tr class="ruleheader"><td>Rule</td><td>Owner</td><td>Audience</td><td>Curriculum</td><td>Target Area</td><td>Leader</td><td>Leader_WWID</td><td>MRC</td><td>Legal</td><td>JobFunction</td><td>SubFunction</td><td>Exclusions</td><td>Year 4 Notes</td><td>Comments</td><td>ChangeNotes</td></tr>');

var table = $( ".ms-listviewtable" ).DataTable( {
		dom: 'Tf<"clear">rtip',
        "scrollY":        "600px",
		"scrollCollapse": true,
		"paging":         false
} ); 
} );

Open in new window


I am doing this to hide the SharePoint specific header row.  Should I use JavaScript to kill the row instead of hiding it?

.ms-viewheadertr {

display:none;	
	
}

Open in new window

Adam Ehrenworth

ASKER
Actually I just double checked and it prepended the header but it actually is now at the bottom!

Any idea why it is prepending there?
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
zephyr_hex (Megan)

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Adam Ehrenworth

ASKER
thanks I got this to work by adding the required thead tag back into the prepend.

THANKS!
zephyr_hex (Megan)

wahoo!  glad you got it working.

you can close the question by selecting the solution that answered you question.