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

aehrenwoTechnology AnalystAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

zephyr_hex (Megan)DeveloperCommented:
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.
0
aehrenwoTechnology AnalystAuthor Commented:
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
0
zephyr_hex (Megan)DeveloperCommented:
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.
0
10 Tips to Protect Your Business from Ransomware

Did you know that ransomware is the most widespread, destructive malware in the world today? It accounts for 39% of all security breaches, with ransomware gangsters projected to make $11.5B in profits from online extortion by 2019.

zephyr_hex (Megan)DeveloperCommented:
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.
0
aehrenwoTechnology AnalystAuthor Commented:
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

0
aehrenwoTechnology AnalystAuthor Commented:
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?
0
zephyr_hex (Megan)DeveloperCommented:
DataTables requires proper HTML syntax for the header row:

<table>
   <thead>
      <tr><th>Header</th></tr>
   </thead>
   <tbody>
      <tr><td>Table Row</td></tr>
   </tbody>
</table>

Open in new window


If you want a header row, you should be adding the row to thead, and use <th> tags instead of <td>

Here's an updated Fiddle Demo.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
aehrenwoTechnology AnalystAuthor Commented:
thanks I got this to work by adding the required thead tag back into the prepend.

THANKS!
0
zephyr_hex (Megan)DeveloperCommented:
wahoo!  glad you got it working.

you can close the question by selecting the solution that answered you question.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
jQuery

From novice to tech pro — start learning today.