Space in fields.

Space in fields.

MickBMickB Posts: 103Questions: 25Answers: 2

Hi,

Our product codes have two spaces in:

eg WH MCL 1

When rendered in the Datatable I get WH MCL 1, so one of the spaces is dropped.
When I view the source, the two spaces are there.

Any idea where this is happening?

Mick

This question has an accepted answers - jump to answer

Answers

  • tangerinetangerine Posts: 2,296Questions: 18Answers: 255

    I get WH MCL 1, so one of the spaces is dropped.

    Looks like two spaces to me.

    When I view the source

    What "source"? HTML? Database?

  • MickBMickB Posts: 103Questions: 25Answers: 2
    edited June 2017

    No, it is one space:

    WH MCL 1
    

    The HTML source which has two spaces:

      <td>
            WH  MCL 1 - WH  SB  1
       </td>
    
    

    Mick

  • rduncecbrduncecb Posts: 125Questions: 2Answers: 28
    Answer ✓

    Umm, spaces are compacted in HTML, how HTML is formatted and how it should be rendered are not connected. If you need to retain multiple spaces you'd need to write a datatables column renderer that either puts the value in pre tags or changes the spaces to no break spaces.

  • MickBMickB Posts: 103Questions: 25Answers: 2

    That unfortunately sounds like the correct answer.

  • rduncecbrduncecb Posts: 125Questions: 2Answers: 28
    edited June 2017

    In your column definition, add:

    render: function(data, type, row, meta) {
        if(type !== 'display')
            return data;
        
        return data.replace(/\s/g, '&nbsp;');
    }
    

    Per docs: https://datatables.net/reference/option/columns.render

  • allanallan Posts: 49,501Questions: 1Answers: 7,286 Site admin

    Another option is use to the white-space CSS property. Set it to pre and your double spaces should be shown as expected.

    Allan

Sign In or Register to comment.